目录
时间戳 Instant
Instant是时间线上的一个点,表示一个时间戳。Instant可以精确到纳秒,这超过了long的最大表示范围,所以在Instant的实现中是分成了两部分来表示,一部分是
seconds
,表示从1970-01-01 00:00:00开始到现在的秒数,另一个部分是nanos
,表示纳秒部分。以下是创建Instant的两种方法:Instant now = Instant.now();
获取当前时刻的时间戳,结果为:2020-02-20T14:14:15.913Z;Instant instant = Instant.ofEpochSecond(60, 100000);
ofEpochSecond()方法的第一个参数为秒,第二个参数为纳秒,上面的代码表示从1970-01-01 00:00:00开始后一分钟的10万纳秒的时刻,其结果为:
1970-01-01T00:01:00.000100Z
。
持续时间,时间差 Duration
有了时间点,自然就衍生出时间段了,那就是Duration。Duration的内部实现与Instant类似,也是包含两部分:seconds表示秒,nanos表示纳秒。Duration是两个时间戳的差值,所以使用java.time中的时间戳类,例如Instant、LocalDateTime等实现了Temporal类的日期时间类为参数,通过Duration.between()方法创建Duration对象:
LocalDateTime from = LocalDateTime.of(2020, Month.JANUARY, 22, 16, 6, 0); // 2020-01-22 16:06:00
LocalDateTime to = LocalDateTime.of(2020, Month.FEBRUARY, 22, 16, 6, 0); // 2020-02-22 16:06:00
Duration duration = Duration.between(from, to); // 表示从 2020-01-22 16:06:00到 2020-02-22 16:06:00 这段时间
Duration对象还可以通过of()方法创建,该方法接受一个时间段长度,和一个时间单位作为参数:
Duration duration1 = Duration.of(5, ChronoUnit.DAYS); // 5天
Duration duration2 = Duration.of(1000, ChronoUnit.MILLIS); // 1000毫秒
本地日期 LocalDate
LocalDate birthday = LocalDate.of(1995, 6,11);
LocalDate today = LocalDate.now();
System.out.println(today.toEpochDay()-birthday.toEpochDay());
本地时间 LocalTime
LocalTime rightNow = LocalTime.now();
LocalTime bedTime = LocalTime.of(22,30);//22:00:00
LocalTime wakeUp = bedTime.plusHours(8);//6:30:00
日期格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
String format = dateTimeFormatter.format(localDateTime);
System.out.println(format);
一、Date转LocalDateTime
// 方法一
LocalDateTime localDateTime = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// 方法二
LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
二、LocalDateTime转Date
Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
三、时间字符串转LocalDateTime
LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
四、LocalDateTime 转 时间戳(秒级)
// 获得当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 将当前时间转为时间戳
long second = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
System.out.println(second);
// 获得当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 将当前时间转为时间戳
long second = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
System.out.println(second);
// 获得当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 将当前时间转为时间戳
long milliseconds = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println(milliseconds / 1000);
五、时间戳 转LocalDateTime
//获得时间戳
long second = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
// 将时间戳转为当前时间
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
System.out.println(localDateTime);
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(milliseconds / 1000, 0, ZoneOffset.ofHours(8));
System.out.println(localDateTime);
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
System.out.println(localDateTime);
六、时间戳与LocalDate互转
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDate localDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
System.out.println(localDate);
//获得时间戳
long seconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
// 将时间戳转为当前时间
LocalDate localDate = Instant.ofEpochSecond(seconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
System.out.println(localDate);
七、LocalDate 转 时间戳
LocalDate localDate = LocalDate.now();
//获得时间戳
long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().getEpochSecond();
System.out.println(seconds);
LocalDate localDate = LocalDate.now();
//获得时间戳
long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
System.out.println(seconds);
举个栗子
//当前周星期一的时间 LocalDate start = LocalDate.parse("2020-09-02", DateTimeFormatter.ofPattern("yyyy-MM-dd")).with(DayOfWeek.MONDAY); System.out.println(start); //LocalDate 字符串格式化 LocalDate start1 = LocalDate.parse("2020-09-02", DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println(start1); //Date转LocalDateTime LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()); System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))); //LocalDateTime转Date Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); System.out.println(date); //Date格式化 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(date); System.out.println(format); //String转Date Date parse = sdf.parse("2020-09-07 16:11:53"); System.out.println(parse);