LocalDate类使用ISO日历表示年、月、日。
常用方法:
1、LocalDate.now():获取系统当前日期。
2、LocalDate.of(int year,int month,int dayOfMonth)
按指定日期创建LocalDate对象。
3、getYear():返回日期当中的年份。
4、getMonth():返回日期中的月份。
5、getDayOfMonth():返回月份中的日。
LocalTime类的常用方法:
1、LocalTime.now():获取系统当前时间。
2、LocalTime.of(int hour,int minute,int second)
按指定时间创建LocalTime对象。
3、getHour():返回小时
4、getMinute():返回分钟
5、getSecond():返回秒
LocalDateTime类用于表示日期和时间
常用方法:
1、LocalDateTime.now():获取系统当前时间。
2、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对象
2、LocalDateTime.parse(strDate,formatter);
ZonedDateTime类
ZonedDateTime处理日期和时间与相应的时区。
1、ZonedDateTime.now()
获取系统当前日期和时间
2、String format(DateTimeFormatter formatter)。
按指定模板将日期对象格式化为一个字符串。
测试案例:
package com.jk.streamtest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Test06_LocalDate {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.print(date.getYear()+"年");
System.out.print(date.getMonthValue()+"月");
System.out.println(date.getDayOfMonth()+"日");
System.out.println(date.toString());
getTime();
getLocalDateTime();
getTimeFormatter();
testzonedDateTime();
}
public static void getTime() {
LocalTime time = LocalTime.now();
System.out.print(time.getHour()+"时");
System.out.print(time.getMinute()+"分");
System.out.println(time.getSecond()+"秒");
System.out.println(time.toString());
}
public static void getLocalDateTime() {
LocalDateTime ltime = LocalDateTime.now();
System.out.println("========================================");
System.out.print(ltime.getYear()+"年");
System.out.print(ltime.getMonthValue()+"月");
System.out.println(ltime.getDayOfMonth()+"日");
System.out.print(ltime.getHour()+"时");
System.out.print(ltime.getMinute()+"分");
System.out.println(ltime.getSecond()+"秒");
System.out.println(ltime.toString());
}
public static void getTimeFormatter() {
DateTimeFormatter dm = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss");
LocalDateTime date = LocalDateTime.parse("2014-04-01:13:24:01",dm);
System.out.println(date.toString());
}
public static void testzonedDateTime() {
ZonedDateTime date = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy:HH:mm:ss");
String strDate = date.format(formatter);
System.out.println("=======================================");
System.out.println(strDate);
}
}