好用的時間工具
package com.zhoufudun.java8;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* LocalDate是線程安全的
* LocalTime 是線程安全的
* LocalDateTime是線程安全的
*/
public class DateTest {
public static void main(String[] args) {
testLocalDate();
testLocalTime();
testLocalDateTime();
testInstance();
testDuration();
testPeriod();
testDateFormat();
testDateParse();
}
private static void testLocalDate() {
LocalDate date = LocalDate.of(2020, 04, 19);
System.out.println(date.getYear());
System.out.println(date.getMonth());
System.out.println(date.getDayOfWeek());
System.out.println(date.getDayOfMonth());
System.out.println(date.getDayOfYear());
System.out.println(date.getEra());
System.out.println(date.getMonthValue());
LocalDate.now();
}
private static void testLocalTime() {
LocalTime time = LocalTime.now();
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
System.out.println(time.getNano());
}
private static void testLocalDateTime() {
// LocalTime time = LocalTime.now();
// LocalDate date = LocalDate.now();
// LocalDateTime of = LocalDateTime.of(date, time);
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime.getDayOfMonth());
System.out.println(dateTime.getDayOfWeek());
System.out.println(dateTime.getDayOfYear());
System.out.println(dateTime.getHour());
System.out.println(dateTime.getMinute());
System.out.println(dateTime.getMonth());
System.out.println(dateTime.getNano());
System.out.println(dateTime.getSecond());
System.out.println(dateTime.getYear());
}
private static void testInstance() {
try {
Instant start = Instant.now();
Thread.sleep(1000L);
Instant end = Instant.now();
Duration between = Duration.between(start, end);
System.out.println(between.toMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 時間段
*/
private static void testDuration() {
LocalTime start = LocalTime.now();
LocalTime before = start.minusHours(1);
Duration duration = Duration.between(start, before);
System.out.println(duration.toHours());
}
/**
* 時間范圍
*/
private static void testPeriod(){
Period between = Period.between(
LocalDate.of(2020, 4, 15),
LocalDate.of(2016, 2, 10)
);
System.out.println(between.getYears());
System.out.println(between.getDays());
System.out.println(between.getMonths());
}
private static void testDateFormat(){
LocalDate date = LocalDate.now();
String format1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
String format2 = date.format((DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(format1);
System.out.println(format2);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format3 = date.format(dateTimeFormatter);
System.out.println(format3);
}
private static void testDateParse(){
String date="20200420";
LocalDate parse = LocalDate.parse(date, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(parse);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date2="2020-01-09";
LocalDate parse2 = LocalDate.parse(date2, dateTimeFormatter);
System.out.println(parse2);
}
}
輸出結(jié)果:
獲取時間非常方便查近。