LocalDat类
LocalDate.of(int year, int month, int dayOfMonth) 按指定日期创建LocalDate对象
getYear:返回日期中的年份
getMonth:返回日期中的月份
getDayOfMonth:返回月份中的日
LocalDate date = LocalDate.now();
System.out.println(date);
System.out.println(date.getYear()+">>>>>"+date.getMonth()+">>>>>"+date.getDayOfMonth());
结果:
LocalTime.now():获取系统当前时间
LocalTime.of(int hour, int minute, int second)按指定时间创建LocalTime对象
getHour():返回小时
getMinute():返回分钟
getSecond():返回秒
LocalTime time = LocalTime.now();
System.out.println(time);
System.out.println(time.getHour()+"时>>>>>"+time.getMinute()+"分>>>>>"+time.getSecond()+"秒>>>>");
结果:
* LocalDateTime.now():获取系统当前时间
*
* LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second)指定日期和时间创建LocalDateTime对象。
*
* getYear:返回日期中的年份
*
* getMonth:返回日期中的月份
*
* getDayOfMonth:返回月份中的日
*
* getHour():返回小时
*
* getMinute():返回分钟
*
* getSecond():返回秒
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getYear()+"年"+now.getMonth()+"月"+now.getDayOfMonth()+"日"+now.getHour()+"时"+now.getMinute()+"分"+now.getSecond()+"秒");
结果:
* DateTimeFormatter类
* (1)ofPatter(String pattern):按pattern字符串指定的格式创建
*
* (2)LcalDateTime.parse(strDate, formatter):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2022-02-22 22:22:22", formatter);
System.out.println(parse + ">>>class:"+parse.getClass());
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy:HH:mm:ss");
String strDate = now.format(formatter2);
System.out.println(strDate + ">>>class:"+strDate.getClass());
结果: