LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,不会存在线程安全的问题
分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
注:ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法
public class LocalDateTimeTest {
//1.LocalDate LocalTime LocalDateTime
@Test
public void test1(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
//2023-02-28T09:48:10.750273900
LocalDateTime ldt2 = LocalDateTime.of(2018,12,12,14,12,45);
System.out.println(ldt2);
//2018-12-12T14:12:45
LocalDateTime ldt3 = ldt.plusYears(2);
System.out.println(ldt3);
//2025-02-28T09:51:03.579968200
LocalDateTime ldt4 = ldt.minusMonths(2);
System.out.println(ldt4);
//2022-12-28T09:51:03.579968200
System.out.println(ldt.getYear());//2023
System.out.println(ldt.getMonthValue());//2
System.out.println(ldt.getDayOfMonth());//28
System.out.println(ldt.getHour());//9
System.out.println(ldt.getMinute());//53
System.out.println(ldt.getSecond());//56
}
//2.Instant : 时间戳(以Unix 元年:1970年1月1日 00:00:00到某个时间之间的毫秒值)
@Test
public void test2(){
Instant ins1 = Instant.now();//默认获取UTC 时区,世界协调时间,本初子午线
System.out.println(ins1);
//2023-02-28T02:55:32.182867600Z
OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8));//做偏移量运算,加8个时区
System.out.println(odt);
//2023-02-28T10:55:32.182867600+08:00
System.out.println(ins1.toEpochMilli());//获取毫秒时间戳
//1677552932182
Instant ins2 = Instant.ofEpochSecond(60);//获取1970年1月1日00:00:00 后60秒后时间戳
System.out.println(ins2);
//1970-01-01T00:01:00Z
}
/**
* 3.
* Duration:计算两个"时间"之间的间隔
*/
@Test
public void test3(){
Instant ins1 = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Instant ins2 = Instant.now();
Duration duration =Duration.between(ins1,ins2);
System.out.println(duration);//PT1.0110169S
System.out.println(duration.toMillis());//1006ms
System.out.println("---------------------");
LocalTime lt1 = LocalTime.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
LocalTime lt2 = LocalTime.now();
System.out.println(Duration.between(lt1,lt2).toMillis());//1007ms
}
/**
* 3.
* Period:计算两个“日期”之间的间隔
*/
@Test
public void test4(){
LocalDate ld1 = LocalDate.of(2018,1,4);
LocalDate ld2 = LocalDate.now();
Period period = Period.between(ld1,ld2);
System.out.println(period);//P5Y1M24D
System.out.println(period.getYears());//5
System.out.println(period.getMonths());//1
System.out.println(period.getDays());//24
}
/**
* 日期的操纵:
* TemporalAdjuster : 时间校正器。有时我们可能需要获
* 取例如:将日期调整到“下个周日”等操作。
* TemporalAdjusters: 该类通过静态方法提供了大量的常
* 用 TemporalAdjuster 的实现。
*/
@Test
public void test5(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);//2023-02-28T11:18:18.015613900
LocalDateTime ldt2 = ldt.withDayOfMonth(10);//将日期设为10号
System.out.println(ldt2);//2023-02-10T11:18:18.015613900
LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ldt3);//2023-03-06T11:22:37.082354700
System.out.println("-----------------");
//自定义:下一个工作日
LocalDateTime ldt5= ldt.with((l) -> {
LocalDateTime ldt4 = (LocalDateTime) l;
DayOfWeek dow = ldt4.getDayOfWeek();
if (dow.equals(DayOfWeek.FRIDAY)) {
return ldt4.plusDays(3);
} else if (dow.equals(DayOfWeek.SATURDAY)) {
return ldt4.plusDays(2);
} else {
return ldt4.plusDays(1);
}
});
System.out.println(ldt5);//2023-03-01T11:27:41.345977400
}
/**
* java.time.format.DateTimeFormatter类:该类提供了三种格式化方法:格式化时间/日期
* 预定义的标准格式
* 语言环境相关的格式
* 自定义的格式
*/
@Test
public void test6(){
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);//2023-02-28T12:58:16.260776800
String strDate = ldt.format(dtf);
System.out.println(strDate);//2023-02-28
System.out.println("------------------");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String strDate2 = dtf2.format(ldt);
System.out.println(strDate2);//2023年02月28日 13:01:20
LocalDateTime newDate = ldt.parse(strDate2,dtf2);
System.out.println(newDate);//2023-02-28T13:01:20
}
/**
* Java8 中加入了对时区的支持,带时区的时间为分别为:ZonedDate、ZonedTime、ZonedDateTime
* 其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式
* 例如 :Asia/Shanghai 等
* ZoneId:该类中包含了所有的时区信息
* getAvailableZoneIds() : 可以获取所有时区时区信息
* of(id) : 用指定的时区信息获取ZoneId 对象
*/
@Test
public void test7(){
Set<String> set = ZoneId.getAvailableZoneIds();
set.forEach(System.out::println);
// Asia/Aden
// America/Cuiaba ...
}
@Test
public void test8(){
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("America/Cuiaba"));
System.out.println(ldt);//2023-02-28T01:43:00.405054300
LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
ZonedDateTime zdt = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(zdt);//2023-02-28T13:43:00.406054+08:00[Asia/Shanghai]
}
}