JDK1.8 时间及格式化类
2020-04-03 22:44:17
针对 JDK1.8 以前的时间类设计的不足,JDK1.8 引入了 `java.time` 包来作为新的时间处理类。
下面为测试代码:
```
package test.demo;
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TimeZone;
/**
* JDK 1.8 新增的日期时间及格式化类(线程安全)
*/
public class TestDate {
/**
* JDK 1.8 提供的线程安全的时间类
*/
@Test
public void testDate() {
String separator = "========================";
// LocalDateTime 综合了 LocalDate 和 LocalTime 的方法。
// 获取当前日期
LocalDate nowDate = LocalDate.now();
System.out.println(nowDate);
System.out.println(nowDate.getYear());
System.out.println(nowDate.getMonthValue());
System.out.println(nowDate.getMonth());
System.out.println(nowDate.getMonth().getValue());
System.out.println(nowDate.getDayOfMonth());
System.out.println(nowDate.getDayOfWeek());
System.out.println(nowDate.getDayOfWeek().getValue());
System.out.println(nowDate.getDayOfYear());
System.out.println(separator);
// 获取当前时间
LocalTime nowTime = LocalTime.now();
System.out.println(nowTime);
System.out.println(nowTime.getHour());
System.out.println(nowTime.getMinute());
System.out.println(nowTime.getSecond());
System.out.println(nowTime.getNano());
System.out.println(separator);
// 获取当前日期及时间
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println(nowDateTime);
System.out.println(nowDateTime.getYear());
System.out.println(nowDateTime.getMonthValue());
System.out.println(nowDateTime.getDayOfMonth());
System.out.println(nowDateTime.getHour());
System.out.println(nowDateTime.getMinute());
System.out.println(nowDateTime.getSecond());
System.out.println(nowDateTime.getNano());
System.out.println(separator);
// 获取指定日期的对象
System.out.println(LocalDate.of(2020, 05, 15));
System.out.println(LocalDate.of(2020, Month.MAY, 15));
// 其他指定时间的方式
LocalDate day = LocalDate.of(2020, 05, 15);
// 第一个参数为年,第二个参数为当年的第多少天
System.out.println(LocalDate.ofYearDay(2020, day.getDayOfYear()));
// 参数为距离时间纪元(1970-01-01)的天数
System.out.println(LocalDate.ofEpochDay(day.toEpochDay()));
// 按格式解析
System.out.println(LocalDate.parse("2020-05-15"));
System.out.println(LocalDate.parse("20200515", DateTimeFormatter.ofPattern("yyyyMMdd")));
System.out.println(separator);
// 获取指定时间的对象
System.out.println(LocalTime.of(18, 30));
System.out.println(LocalTime.of(18, 30, 0));
System.out.println(LocalTime.of(18, 30, 0, 0));
LocalTime time = LocalTime.of(18, 30, 0, 0);
System.out.println(LocalTime.ofSecondOfDay(time.toSecondOfDay()));
System.out.println(LocalTime.ofNanoOfDay(time.toNanoOfDay()));
System.out.println(LocalTime.parse("18:30:00"));
System.out.println(LocalTime.parse("183000", DateTimeFormatter.ofPattern("HHmmss")));
System.out.println(separator);
// 获取指定日期时间的对象
System.out.println(LocalDateTime.of(day, time));
System.out.println(LocalDateTime.of(2020, 05, 15, 18, 30));
System.out.println(LocalDateTime.of(2020, Month.MAY, 15, 18, 30));
System.out.println(LocalDateTime.of(2020, 05, 15, 18, 30, 0));
System.out.println(LocalDateTime.of(2020, 05, 15, 18, 30, 0, 0));
System.out.println(LocalDateTime.parse("2020-05-15T18:30:00"));
System.out.println(LocalDateTime.parse("2020-05-15 18:30:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(separator);
// 时间日期的比较
LocalDate date1 = LocalDate.of(2020, 05, 15);
LocalDate date2 = LocalDate.of(2018, 05, 15);
System.out.println(date1.isBefore(date2));
System.out.println(date1.isAfter(date2));
System.out.println(date1.equals(date2));
System.out.println(separator);
// 仅月日 or 仅年月 的获取方式(可用来重复表示每年的某一天)
MonthDay monthDay = MonthDay.of(05, 15);
YearMonth yearMonth = YearMonth.of(2020, 05);
System.out.println(monthDay);
System.out.println(yearMonth);
System.out.println(yearMonth.lengthOfMonth()); // 这个月有多少天
System.out.println(separator);
// 对日期时间的修改
LocalDateTime nowDateTime1 = LocalDateTime.now();
System.out.println(nowDateTime1);
// 减去指定时间
System.out.println(nowDateTime1.minusYears(1));
System.out.println(nowDateTime1.minusMonths(1));
System.out.println(nowDateTime1.minusWeeks(1));
System.out.println(nowDateTime1.minusDays(1));
// 增加指定时间
System.out.println(nowDateTime1.plusYears(1));
System.out.println(nowDateTime1.plusMonths(1));
System.out.println(nowDateTime1.plusWeeks(1));
System.out.println(nowDateTime1.plusDays(1));
// 其他
System.out.println(nowDateTime1.minus(1, ChronoUnit.YEARS));
System.out.println(nowDateTime1.minus(1, ChronoUnit.HOURS));
System.out.println(nowDateTime1.plus(1, ChronoUnit.MONTHS));
System.out.println(separator);
// 时间戳(Instant 与 JDK8 以前的 Date 相似,可以使用对应方法在这两个类型之间转换)
Instant instant = Instant.now();
System.out.println(instant);
Date date = Date.from(instant);
System.out.println(date);
System.out.println(date.toInstant());
System.out.println(separator);
// 时间格式化(SimpleDateFormat 线程不安全,DateTimeFormatter 线程安全)
LocalDateTime nowDateTime2 = LocalDateTime.now();
System.out.println(nowDateTime2);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(nowDateTime2));
System.out.println(nowDateTime2.format(formatter));
System.out.println(separator);
// 计算时间差
// Period 表示日期之间得差(年月日)
LocalDate day1 = LocalDate.of(1990, 05, 15);
LocalDate nowDate1 = LocalDate.now();
Period period = Period.between(day1, nowDate1);
System.out.printf("年龄:%d 年 %d 月 %d 日", period.getYears(), period.getMonths(), period.getDays());
// Duration 表示时间差(日时分秒)
LocalDateTime nowDateTime3 = LocalDateTime.of(1990, 05, 15, 18, 30, 0);
LocalDateTime nowDateTime4 = LocalDateTime.now();
Duration duration = Duration.between(nowDateTime3, nowDateTime4);
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toMillis());
System.out.println(duration.toNanos());
// ChronoUnit 类可用于在单个时间单位内测量一段时间
LocalDateTime today = LocalDateTime.now();
LocalDateTime birthDate = LocalDateTime.of(1990, 05, 15, 18, 30, 0);
System.out.println(today);
System.out.println(birthDate);
System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));
System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));
System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));
System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));
System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));
System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));
System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));
System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));
System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));
System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));
System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));
System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));
}
/**
* LocalDateTime 不包含时区信息,ZonedDateTime 包含时区信息
*/
@Test
public void testZoneDateTime() {
// 未设置时区打印信息
System.out.println(LocalDateTime.now());
System.out.println(ZonedDateTime.now());
// 设置时区后打印信息
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));// 设置默认时区
System.out.println(LocalDateTime.now());
System.out.println(ZonedDateTime.now());
/*
打印结果:
2020-04-03T12:11:06.098
2020-04-03T12:11:06.100Z[UTC]
2020-04-03T20:11:06.203
2020-04-03T20:11:06.203+08:00[Asia/Shanghai]
*/
/*
与 LocalDateTime 的区别是,ZonedDateTime 内部记录了时区,而 LocalDateTime 没有。
因此 LocalDateTime 转 Instant 需要指定 ZoneOffSet 而 ZonedDateTime 不需要。
*/
Instant instant1 = LocalDateTime.now().toInstant(ZoneOffset.of("+08:00"));
Instant instant2 = ZonedDateTime.now().toInstant();
System.out.println(instant1);
System.out.println(instant2);
System.out.println(instant1.getEpochSecond());
System.out.println(instant2.getEpochSecond());
System.out.println(System.currentTimeMillis());
}
}
```
分类:编程 关键词:时间日期
评论(0) 浏览(966)