public class DateTest {
public static void main(String... args) {
// 獲取當(dāng)前日期(YYYY-mm-dd 2018-03-06)
LocalDate nowDate = LocalDate.now();
System.out.println(nowDate);
// 獲取當(dāng)前時(shí)間(HH:mm:a 11:15:18)去掉了毫秒
LocalTime nowTime = LocalTime.now().withNano(0);
System.out.println(nowTime);
// 獲取當(dāng)前全時(shí)間(YYYY-mm-ddTHH:mm:a 2018-03-06T11:15:18)
LocalDateTime dateTime= LocalDateTime.now().withNano(0);
System.out.println(dateTime);
//標(biāo)準(zhǔn)格式:字符串轉(zhuǎn)全時(shí)間,可以去掉格式
LocalDateTime formDateTime1 = LocalDateTime.parse(dateTime.toString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
LocalDateTime formDateTime2 = LocalDateTime.parse(dateTime.toString());
System.out.println(formDateTime1);
//標(biāo)準(zhǔn)格式:字符串轉(zhuǎn)日期
LocalDate formDate1 = LocalDate.parse(nowDate.toString(), DateTimeFormatter.ISO_DATE);
System.out.println(formDate1);
//標(biāo)準(zhǔn)格式:字符串轉(zhuǎn)時(shí)間
LocalTime formTime1 = LocalTime.parse(nowTime.toString());
System.out.println(formTime1 );
//自定義格式
DateTimeFormatter myFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String myFormDateTime1= dateTime.format(myFormatter1);
System.out.println( myFormDateTime1);
//獲取時(shí)間戳
Instant now = Instant.now();
long nowTimestamp = Timestamp.from(now).getTime();
System.out.println( nowTimestamp);
//LocationDateTime 轉(zhuǎn)時(shí)間戳
long timestamp = Timestamp.valueOf(dateTime).getTime();
System.out.println(timestamp);
//時(shí)間戳與LocationDateTime互相轉(zhuǎn)換
Instant instant = Instant.ofEpochMilli(nowTimestamp);
LocalDateTime DateTime1 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).withNano(0);
System.out.println(DateTime1);
//LocationDateTime轉(zhuǎn)換為DATE
Instant instant1 = dateTime.atZone(ZoneId.systemDefault()).toInstant();
Date fromoLcationDateTime = Date.from(instant1);
System.out.println(fromoLcationDateTime);
//DATE轉(zhuǎn)換為L(zhǎng)ocationDateTime
Instant instant2 = fromoLcationDateTime.toInstant();
LocalDateTime DateTime2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault()).withNano(0);
System.out.println(DateTime2);
//比較時(shí)間
boolean before = dateTime.isBefore(formDateTime2);
boolean equals = dateTime.equals(formDateTime2);
boolean after = dateTime.isAfter(formDateTime2);
System.out.println(before+""+equals+""+after);
//日期加減
LocalDateTime localDateTime = dateTime.plusYears(1).plusHours(1).minusDays(1L);
System.out.println(localDateTime);
//日期間相差時(shí)長(zhǎng)(年月日分別比較)
Period between = Period.between(localDateTime.toLocalDate(), dateTime.toLocalDate());
System.out.println(between.getYears());
//日期間相差時(shí)長(zhǎng)(總數(shù)比較)
long days = dateTime.until(localDateTime, ChronoUnit.DAYS);
System.out.println(days );
// 4 得到年月 ----打印輸出----- 2018 3
int year = dateTime.getYear();int month = dateTime.getMonth().getValue();
System.out.println(year+""+month+"" );
}
}
打印結(jié)果
2018-03-08
14:57:05
2018-03-08T14:57:05
2018-03-08T14:57:05
2018-03-08
14:57:05
2018/03/08 14:57:05
1520492225981
1520492225000
2018-03-08T14:57:05
Thu Mar 08 14:57:05 CST 2018
2018-03-08T14:57:05
falsetruefalse
2019-03-07T15:57:05
0
364
20183