JDK8中,新增了三个类,用以处理时间。
LocalDate专门处理日期,LocalTime专门处理时间,LocalDateTime包含了日期和时间,而且对于很多复杂的问题,都提供了现成的方法,比如:获取2017年12月的第一个周一等。
package test;import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;/*** Created by lightClouds917* Date 2017/11/6* Description:Java8中处理时间和日期的类*SQL -> Java--------------------------date -> LocalDatetime -> LocalTimetimestamp -> LocalDateTime*/
public class DateTest2 {public static void main(String[] args){test1();test2();Calendar calendar = Calendar.getInstance();System.out.println(calendar.getTime());}/*** 处理日期 LocalDate*/public static void test1(){//获取当前日期 2017-11-06LocalDate today = LocalDate.now();System.out.println(today);//构造日期 2017-10-08LocalDate today2 = LocalDate.of(2017,10,8);System.out.println(today2);//构造日期 2017-02-22 字符串严格按照yyyy-MM-ddLocalDate today3 = LocalDate.parse("2017-02-22");System.out.println(today3);//本月第一天 2017-11-01LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());System.out.println(firstDayOfMonth);//本月第二天 2017-11-02 第n天LocalDate secondDayOfMonth = today.withDayOfMonth(2);System.out.println(secondDayOfMonth);//本月最后一天 2017-02-28 方便解决任何年份的二月份多少天LocalDate lastDayOfMonth = today3.with(TemporalAdjusters.lastDayOfMonth());System.out.println(lastDayOfMonth);//获取2017年12月的第一个周一 2017-12-04LocalDate firstDayOf201712 = LocalDate.parse("2017-12-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));System.out.println(firstDayOf201712);}/*** 处理时间 LocalTime*/public static void test2(){//获取当前时间 含有毫秒值 17:18:41.571LocalTime now = LocalTime.now();System.out.println(now);//获取当前时间 去掉毫秒值 17:45:41LocalTime now1 = LocalTime.now().withNano(0);System.out.println(now1);//00:46:46.651 提供了把时分秒都设为0的方法LocalTime now2 = LocalTime.now().withHour(0);System.out.println(now2);//构造时间 00:20:55LocalTime time1 = LocalTime.of(0,20,55);System.out.println(time1);//构造时间 05:43:22LocalTime time2 = LocalTime.parse("05:43:22");System.out.println(time2);//标准时间 2017-11-06T17:53:15.930LocalDateTime lt = LocalDateTime.now();System.out.println(lt);}
}
及:
public class TimeTest {@Testpublic void testTime() {LocalDateTime time = LocalDateTime.now();System.out.println(time.toString()); //字符串表示System.out.println(time.toLocalTime()); //获取时间(LocalTime)System.out.println(time.toLocalDate()); //获取日期(LocalDate)System.out.println(time.getDayOfMonth()); //获取当前时间月份的第几天System.out.println(time.getDayOfWeek()); //获取当前周的第几天System.out.println(time.getDayOfYear()); //获取当前时间在该年属于第几天System.out.println(time.getHour());System.out.println(time.getMinute());System.out.println(time.getMonthValue());System.out.println(time.getMonth());System.out.println("-----------------------------------");//格式化输出DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");System.out.println(time.format(formatter));//构造时间LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);LocalDateTime endTime = LocalDateTime.of(2018, 1, 3, 20, 31, 20);//比较时间System.out.println(time.isAfter(startTime));System.out.println(time.isBefore(endTime));//时间运算,相加相减System.out.println(time.plusYears(2)); //加2年System.out.println(time.plusDays(2)); //加两天System.out.println(time.minusYears(2)); //减两年System.out.println(time.minusDays(2)); //减两天//获取毫秒数(使用Instant)System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//获取秒数(使用Instant)System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());}}