八、时间日期API
1.LocalDate 、LocalTime 、LocalDateTime
LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。
常用方法:
方法 | 描述 |
---|---|
now() | 静态方法,根据当前时间创建对象 |
of() | 静态方法,根据指定日期/时间创建对象 |
plusDays, plusWeeks,plusMonths, plusYears | 向当前 LocalDate 对象添加几天、几周、几个月、几年 |
minusDays, minusWeeks,minusMonths, minusYears | 从当前 LocalDate 对象减去几天、几周、几个月、几年 |
plus, minus | 添加或减少一个 Duration 或 Period |
withDayOfMonth,withDayOfYear,withMonth,withYear | 将月份天数、年份天数、月份、年份修改为指定 的值 并返回新的LocalDate 对象 |
getDayOfMonth | 获得月份天数(1-31) |
getDayOfYear | 获得年份天数(1-366) |
getDayOfWeek | 获得星期几(返回一个 DayOfWeek枚举值) |
getMonth | 获得月份, 返回一个 Month 枚举值 |
getMonthValue | 获得月份(1-12) |
getYear | 获得年份 |
until | 获得两个日期之间的 Period 对象,或者指定 ChronoUnits 的数字 |
isBefore, isAfter | 比较两个 LocalDate |
isLeapYear | 判断是否是闰年 |
举例:
/**
* LocalDate、LocalTime、LocalDateTime
*/
@Test
public void test1(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
LocalDateTime ldt1 = LocalDateTime.of(2019, 11, 7, 14, 41, 22,110000000);
System.out.println(ldt1);
LocalDateTime ldt2 = ldt.plusYears(1);
System.out.println(ldt2);
LocalDateTime ldt3 = ldt.minusMonths(1);
System.out.println(ldt3);
System.out.println(ldt.getYear());
System.out.println(ldt.getMonth());
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getHour());
System.out.println(ldt.getMinute());
System.out.println(ldt.getSecond());
}
2.Instant时间戳
用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算
/**
* Instant 时间戳
*/
@Test
public void test2() {
//默认获取UTC时区,不是本机
Instant instant1 = Instant.now();
System.out.println(instant1);
//时间偏移运算(与我们的时差为8)
OffsetDateTime offsetDateTime = instant1.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//以ms显示
System.out.println(instant1.toEpochMilli());
//当前时间戳代表时间
Instant instant2 = Instant.ofEpochMilli(10000);
System.out.println(instant2);
}
3.Duration 和 Period
Duration:用于计算两个“时间”间隔
/**
* Duration 计算两个时间之间的间隔
*/
@Test
public void test3() {
Instant instant1 = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Instant instant2 = Instant.now();
Duration duration1 = Duration.between(instant1,instant2);
System.out.println(duration1.toMillis());
System.out.println("------------------------");
LocalTime localTime1 = LocalTime.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalTime localTime2 = LocalTime.now();
Duration duration2 = Duration.between(localTime1,localTime2);
System.out.println(duration2.toMillis());
}
Period:用于计算两个“日期”间隔
/**
* Period 计算两个日期之间的间隔
*/
@Test
public void test4() {
LocalDate localDate1 = LocalDate.of(2020,10,1);
LocalDate localDate2 = LocalDate.now();//2019-11-7
Period period = Period.between(localDate2,localDate1);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
3.时间校正器TemporalAdjuster
TemporalAdjuster : 时间校正器。有时我们可能需要获取例如:将日期调整到“下个周日”等操作。
TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。
/**
* TemporalAdjuster : 时间校正器
*/
@Test
public void test5() {
LocalDateTime localDateTime1 = LocalDateTime.now();
System.out.println(localDateTime1);
//调整到本月10号
LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(10);
System.out.println(localDateTime2);
//调整到下个周日
LocalDateTime localDateTime3 = localDateTime1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(localDateTime3);
//自定义,下个工作日(3019-11-7下个工作日是8号)
LocalDateTime localDateTime = localDateTime1.with(l -> {
LocalDateTime localDateTime4 = (LocalDateTime) l;
DayOfWeek dow = localDateTime4.getDayOfWeek();
//周五加3天是工作日
if (dow.equals(DayOfWeek.FRIDAY)) {
return localDateTime4.plusDays(3);
//周六加1天是工作日
} else if (dow.equals(DayOfWeek.SATURDAY)) {
return localDateTime4.plusDays(2);
//其他都是加1天
} else {
return localDateTime4.plusDays(1);
}
});
System.out.println(localDateTime);
}
4.时间/日期格式化DateTimeFormatter
java.time.format.DateTimeFormatter 类,该类提供了三种格式化方法:
- 预定义的标准格式
- 语言环境相关的格式
- 自定义的格式
/**
* DateTimeFormatter : 时间/日期格式化
*/
@Test
public void test6() {
//按ISO_DATE对时间进行格式化
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
String date = ldt.format(dtf);
System.out.println(date);
System.out.println("-------------------------");
//按指定格式进行格式化
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String date2 = dtf2.format(ldt);
System.out.println(date2);
LocalDateTime parse = ldt.parse(date2, dtf2);
System.out.println(parse);
}
5.时区的处理
Java8 中加入了对时区的支持,带时区的时间为分别为:ZonedDate、ZonedTime、ZonedDateTime
其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式
例如 :Asia/Shanghai 等
ZoneId:该类中包含了所有的时区信息
getAvailableZoneIds() : 可以获取所有时区时区信息
of(id) : 用指定的时区信息获取 ZoneId 对象
@Test
public void test7() {
//所有支持的时区
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
availableZoneIds.forEach(System.out::println);
//指定时区的时间
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(ldt);
LocalDateTime ldt2 = LocalDateTime.now();
ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(zonedDateTime);
}
注:菜鸟一枚,才疏学浅,希望各位前辈能够批评指正,感谢感谢!!!