各自努力,最高处见!加油!
日期类LocalDate类(第三代)
JDK8后加入的类。
import java.time.LocalDate;
import java.time.LocalDateTime;
public class LocalDate_ {
public static void main(String[] args) {
LocalDateTime localDateTime=LocalDateTime.now();
System.out.println(localDateTime);//2021-09-14T00:02:02.637340300
System.out.println(localDateTime.getYear()+"-"+localDateTime.getMonth()+"-"+localDateTime.getDayOfMonth());
//2021-SEPTEMBER-14
System.out.println(localDateTime.getYear()+"-"+localDateTime.getMonthValue()+"-"+localDateTime.getDayOfMonth());
//2021-9-14
}
}
格式化方法
使用DateTimeFormatter对象格式化日期。
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分SS秒");
String format=dateTimeFormatter.format(localDateTime);
System.out.println(format);
LocalDateTime ldt=localDateTime.plusDays(569);
System.out.println(ldt.getYear()+"-"+ldt.getMonth()+"-"+ldt.getDayOfMonth());//569天后
Instant时间戳
提供了一系列和Date类转换的方式
Instant----->Date:
Date date=Date.from(instant);
Date----->Instant:
Instant instant=date.toInstant();
import java.time.Instant;
import java.util.Date;
public class Instant_ {
public static void main(String[] args) {
Instant now=Instant.now();
System.out.println(now);
Date date= Date.from(now);//Instant的from方法将Instant转成Date对象
System.out.println(date);
Instant instant=date.toInstant();//Date的toInstant方法将date转换成Instant对象
System.out.println(instant);
}
}