LocalDate now = LocalDate.now();
System.out.println(now);
结果:
2019-01-22
创建日期为2013-09-10,参数为年、月、日
LocalDate date1 = LocalDate.of(2013, 9, 10);
System.out.println(date1);
结果:
2013-09-10
创建一个日期,1999年的第100天
LocalDate date2 = LocalDate.ofYearDay(1999, 100);
System.out.println(date2);
结果:
1999-04-10
创建一个日期,距离1970-1-1纪元10000天
LocalDate date3 = LocalDate.ofEpochDay(10000);
System.out.println(date3);
结果:
1997-05-19
字符串格式严格参照yyyy-MM-dd
LocalDate parse1 = LocalDate.parse("2015-01-01");
//LocalDate parse2 = LocalDate.parse("2015-1-01");//报错
//LocalDate parse3 = LocalDate.parse("2015-01-1");//报错
//LocalDate parse4 = LocalDate.parse("15-01-1");//报错
结果:
2015-01-01
LocalDate now = LocalDate.now();
int year = now.getYear();
System.out.println(year);
结果:
2019
LocalDate now = LocalDate.now();
int month = now.getMonthValue();
System.out.println(month);
结果:
1
LocalDate now = LocalDate.now();
int day = now.getDayOfMonth();
System.out.println(day);
结果:
22
LocalDate now = LocalDate.now();
int yearday = now.getDayOfYear();
System.out.println(yearday);
结果:恰巧是首月,和月中天数一天
22
LocalDate now = LocalDate.now();
DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.println(dayOfWeek);
结果:
TUESDAY
LocalDate now = LocalDate.now();
boolean leapYear = now.isLeapYear();
System.out.println(leapYear);
结果:
false
LocalDate now = LocalDate.now();
int lengthOfMonth = now.lengthOfMonth();
System.out.println(lengthOfMonth);
结果:
31
LocalDate now = LocalDate.now();
int lengthOfYear = now.lengthOfYear();
System.out.println(lengthOfYear);
结果:
365
LocalDate now = LocalDate.now();
LocalDate withYear = now.withYear(1999);
System.out.println(withYear);
结果:
1999-01-22
LocalDate now = LocalDate.now();
LocalDate withYear = now.withYear(1999);
System.out.println(withYear);
结果:
1999-01-22
LocalDate now = LocalDate.now();
LocalDate withMonth = now.withMonth(12);
System.out.println(withMonth);
结果:
2019-12-22
LocalDate now = LocalDate.now();
LocalDate withDayOfMonth = now.withDayOfMonth(31);
System.out.println(withDayOfMonth);
结果:
2019-01-31
LocalDate now = LocalDate.now();
LocalDate withDayOfYear = now.withDayOfYear(60);
System.out.println(withDayOfYear);
结果:
2019-03-01
在此日期基础上加年份
LocalDate now = LocalDate.now();
LocalDate plusYears = now.plusYears(200);
System.out.println(plusYears);
结果:2019年加200年=2219
2219-01-22
在此日期基础上加月份
LocalDate now = LocalDate.now();
LocalDate plusMonths = now.plusMonths(1);
System.out.println(plusMonths);
结果:2019-01-22加一个月
2019-02-22
在此日期上加天数
LocalDate now = LocalDate.now();
LocalDate plusDays = now.plusDays(8);
System.out.println(plusDays);
结果:2019-01-22加8天
2019-01-30
注意:我们在2019-01-30的基础上加一个月呢,结果是什么
LocalDate plusMonths2 = plusDays.plusMonths(1);
System.out.println(plusMonths2);
结果:是二月份的最后一天
2019-02-28
设置小时和分钟
LocalDate now = LocalDate.now();
LocalDateTime atTime = now.atTime(11, 11);
System.out.println(atTime);
结果:
2019-01-22T11:11
LocalDate now = LocalDate.now();
LocalDateTime atStartOfDay = now.atStartOfDay();
System.out.println(atStartOfDay);
结果:
2019-01-22T00:00
LocalDate now = LocalDate.now();
long epochDay = now.toEpochDay();
System.out.println(epochDay);
结果:
17918
public static void main(String[] args) {
//利用localDate实现一个月历
//获取当前日期
LocalDate now = LocalDate.now();//2019-01-23
//获取日期的年份,用于日历标题
int year = now.getYear();
//获取日期的月份,用于日历标题
int month = now.getMonthValue();
System.out.println(year+"年-"+month+"月的月历");//日历标题
//获取日期的月中日,用于定位月初日
int today = now.getDayOfMonth();
//获取月初日期
LocalDate monthFristDay = now.minusDays(today-1);//2019-01-01
//System.out.println(monthFristDay);
//判断月初日是星期几
DayOfWeek dayOfWeek = monthFristDay.getDayOfWeek();
//输出星期格式
System.out.println("MON TUE WED THU FRI SAT SUN");
//月初与星期格式对上
for(int i = 1;i