更新时间:2022-07-18 10:42:29
LocalDate类使用ISO日历表示年、月、日。
package com.fantJ.JAVA_8;
import java.time.LocalDate;
public class LocalDate_Test {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println(date.getYear()+" "+date.getMonthValue()+" "+date.getDayOfMonth());
System.out.println(date.toString());
}
}
public class LocalTime_Test {
public static void main(String[] args) {
LocalTime time = LocalTime.now();
System.out.println(time.getHour()+" "+time.getMinute()+" "+time.getSecond());
System.out.println(time.toString());
System.out.println(time.toSecondOfDay());
}
}
public class LocalDateTime_Test {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime.getYear()+" "+dateTime.getMonthValue()+" "+dateTime.getDayOfMonth()+
dateTime.getHour()+" "+dateTime.getMinute()+" "+dateTime.getSecond());
System.out.println(dateTime.toString());
}
}
用于将字符串解析为日期
public class DateTimeFormatter_Test {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse("2017-12-15:19:15:01",formatter);
System.out.println(dateTime.toString());
}
}
public class ZonedDateTime_Test {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy:HH:mm:ss");
String date = zonedDateTime.format(formatter);
System.out.println(date);
}
}