LocalDate类
生成LocalDate的几种方式
LocalDate localDate=LocalDate.now();
System.out.println(localDate);
LocalDate now =LocalDate.of(2020, 3, 9);
System.out.println(now);
//有个坑 ,如果是2020-3-9表示或者2020-03-9 或者2020-3-09表示就不行必须yyyy-mm-dd格式
LocalDate localDate1=LocalDate.parse("2020-03-09");
System.out.println(localDate1);
LocalTime类
LocalTime now=LocalTime.now();
System.out.println(now);
LocalTime now1=LocalTime.of(11, 28);
System.out.println(now1);
//默认格式为yyyy-mm-dd hh:mm:ss
LocalTime now2=LocalTime.parse("11:28");
System.out.println(now2);
MonthDay (用来判断是否是生日)
public static Boolean isBirthDay(int year,int month,int day){
LocalDate currentDate=LocalDate.of(year, month, day);
MonthDay birthDay=MonthDay.from(currentDate);
LocalDate now=LocalDate.now();
MonthDay currentMonthDay=MonthDay.from(now);
if (birthDay.equals(currentMonthDay)){
return true;
}
return false;
}
对LocalDate的加减(一年后,一年后,一天后)
LocalDate now=LocalDate.now();
LocalDate oneWeekLater=now.plusWeeks(1);
System.out.println(oneWeekLater);
LocalDate oneDayLater=now.plusDays(1);
System.out.println(oneDayLater);
LocalDate oneMonthLater=now.plusMonths(1);
System.out.println(oneMonthLater);
LocalDate oneYearLater=now.plusYears(1);
System.out.println(oneYearLater);
获取LocalDate中的year month day
LocalDate now=LocalDate.now();
int year=now.getYear();
/**
* getMonth()返回的是英语的月份
*/
int month=now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
System.out.println(year);
System.out.println(month);
System.out.println(dayOfMonth);
日期的比较(早,晚 或者相等)
LocalDate now=LocalDate.now();
LocalDate now1=LocalDate.of(2020,03,09);
System.out.println(now.isBefore(now1));
System.out.println(now.isAfter(now1));
System.out.println(now.isEqual(now1));
Instant 类(常用类 获取当前时间戳)
Instant instant=Instant.now();
System.out.println(instant.toEpochMilli());
Period类 (表示时间段 2个LocalDate之间的差)
LocalDate now=LocalDate.now();
LocalDate now1=LocalDate.of(2020,03,09);
Period period=Period.between(now,now1);
System.out.println(period.getYears());
System.out.println(period.getDays());
System.out.println(period.getMonths());
Duration类(表示时间差 比如2个LocalTime之间的时间差)
LocalTime localTimeNow=LocalTime.now();
LocalTime localTime=LocalTime.of(11, 03);
Duration duration=Duration.between(localTime, localTimeNow);
System.out.println(duration.getSeconds());
YearMonth类
与 MonthDay检查重复事件的例子相似,YearMonth是另一个组合类,用于表示信用卡到期日、FD到期日、期货期权到期日等。还可以用这个类得到 当月共有多少天,YearMonth实例的lengthOfMonth()方法可以返回当月的天数,在判断2月有28天还是29天时非常有用。
YearMonth currentYearMonth = YearMonth.now();
System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());
YearMonth creditCardExpiry = YearMonth.of(2019, Month.FEBRUARY);
System.out.printf("Your credit card expires on %s %n", creditCardExpiry);
日期字符串和日期互相转换以及自定义日期格式
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//日期转字符串
String str = date.format(format1);
System.out.println("日期转换为字符串:"+str);
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//字符串转日期
LocalDate date2 = LocalDate.parse(str,format2);
System.out.println("日期类型:"+date2);
判断是否闰年(isLeapYear())
LocalDate today = LocalDate.now();
if(today.isLeapYear()){
System.out.println("This year is Leap year");
}else {
System.out.println("2018 is not a Leap year");
}
LocalDateTime 是LocalDate和LocalTime的集合体
Clock类(用来获取当前时间戳)
Clock clock = Clock.systemUTC();
System.out.println("Clock : " + clock.millis());
// Returns time based on system clock zone
Clock defaultClock = Clock.systemDefaultZone();
System.out.println("Clock : " + defaultClock.millis());