LocalDate类使用ISO日历表示年,月,日
LocalDate.now():
获取系统当前日期 :2016-06-24LocalDate.of(int year,int month,int dayofMonth)
按指定日期创建LocalDate对象。
LocalDate date=LocalDate.of(2015,5,30); 2015-05-30date.getYear(); //返回当前日期年
date.getMonthValue(); //返回当前日期月份
date.getDayOfMonth(); //返回当前日期天
LocalDate dateNow = LocalDate.now();
LocalDate dateTemp = LocalDate.of(2015, 6, 30);
int year = dateNow.getYear();//返回当前日期年
int monthValue = dateNow.getMonthValue();//返回当前日期月份
int dayOfMonth = dateNow.getDayOfMonth();//返回当前日期天
LocalTiem类用于表示一天中的时间
LocalTime.now()
获得当前系统的时间:17:47:22.905LocalTime.of(int hour,int minute,int second)
按指定时间创建LocalTime对象getHour()
getMinute()
getSecond()
LocalDateTime类用于表示日期和时间
LocalDateTime类的常用方法:
- LocalDateTime.now():获取系统当前时间。
- LocalDateTime.of(int year,int month,int dayOfMonth
int hour,int minute,int second)
按指定日期和时间创建LocalDateTime对象。 - getYear():返回日期中的年份。
- getMonth():返回日期中的月份。
- getDayOfMonth():返回月份中的日。
- getHour():返回小时。
- getMinute():返回分钟。
- getSecond():返回秒。
DateTimeFormatter类用于将字符串解析为日期
常用方法:
1、static ofPattern(String pattern);
作用:按pattern字符串指定的格式创建DateTimeFormatter对象。
DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
2、 LocalDateTime.parse(strDate, formatter);
作用:按指定日期模板和该模版字符串创建LocalDateTime对象。
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time=LocalDateTime.parse("2016-06-23 18:12:30",formatter);
System.out.println(time);