服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - jdk8的datetime时间函数使用示例

jdk8的datetime时间函数使用示例

2019-11-16 14:06java教程网 JAVA教程

这篇文章主要介绍了jdk8的datetime时间函数使用示例,需要的朋友可以参考下

JDK8已发布,写了一个datetime时间函数使用方法的小示例
 

复制代码代码如下:


package datetime;

 

import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.previousOrSame;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class DataTimes {

 public static void main(String[] args) {
  // 创建时间对象
  LocalDateTime timePoint = LocalDateTime.now(); // 当前时间
  System.out.println("--当前时间----");
  System.out.println(timePoint);
  System.out.println("");

  System.out.println("--自定义时间----");
  System.out.println(LocalDate.of(2012, Month.DECEMBER, 12)); // from
                 // values
  System.out.println(LocalDate.ofEpochDay(150)); // middle of 1970
  System.out.println(LocalTime.of(17, 18)); // the train I took home today
  System.out.println(LocalTime.parse("10:15:30")); // From a String
  System.out.println("");

  System.out.println("--获取时间的各个部分----");
  System.out.println(timePoint.toLocalDate());
  System.out.println(timePoint.getMonth());
  System.out.println(timePoint.getDayOfMonth());
  System.out.println(timePoint.getSecond());
  System.out.println("");

  System.out.println("---设置并返回新的时间对象---");
  LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
  System.out.println(thePast);
  System.out.println("---再加3周---");
  LocalDateTime yetAnother = thePast.plusWeeks(3).plus(3,
    ChronoUnit.WEEKS);
  System.out.println(yetAnother);
  System.out.println("");

  System.out.println("---使用时间调整函数---");
  System.out.println(timePoint);
  System.out.println(timePoint.with(lastDayOfMonth()));
  System.out.println(timePoint.with(previousOrSame(DayOfWeek.WEDNESDAY)));
  System.out.println(timePoint.with(LocalTime.now()));
  System.out.println("");

  System.out.println("---截断时间精确位--");
  System.out.println(timePoint);
  LocalDateTime truncatedTimeToMinutes = timePoint
    .truncatedTo(ChronoUnit.MINUTES);
  System.out.println(truncatedTimeToMinutes);
  LocalDateTime truncatedTimeToSeconds = timePoint
    .truncatedTo(ChronoUnit.SECONDS);
  System.out.println(truncatedTimeToSeconds);
  System.out.println("");

  System.out.println("---使用时区---");
  ZonedDateTime zoned_now = ZonedDateTime.of(LocalDateTime.now(),
    ZoneId.of("UTC+08:00"));
  System.out
    .println(zoned_now.withZoneSameInstant(ZoneId.of("UTC+00:00")));
  System.out.println(zoned_now.getOffset());
  System.out.println("");

  System.out.println("---时间上使用时区偏移---");
  OffsetTime time = OffsetTime.now();
  ZoneOffset offset = ZoneOffset.of("+02:00");
  System.out.println(time);
  System.out.println(time.withOffsetSameInstant(offset));
  System.out.println("");

  System.out.println("---时间加减---");
  timePoint = LocalDateTime.now();
  System.out.println(timePoint);
  // 3 years, 2 months, 1 day
  Period period1 = Period.of(3, 2, 1);
  System.out.println(timePoint.plus(period1));
  Duration duration = Duration.ofSeconds(3, 5);
  System.out.println(timePoint.plus(duration));
  Duration sixHours = Duration.between(
    ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+08:00")),
    ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+02:00")));
  System.out.println(timePoint.plus(sixHours));
  System.out.println("");

 }
}

 

运行结果

 

复制代码代码如下:


--当前时间----
2014-03-25T17:03:40.553

 

--自定义时间----
2012-12-12
1970-05-31
17:18
10:15:30

--获取时间的各个部分----
2014-03-25
MARCH
25
40

---设置并返回新的时间对象---
2010-03-10T17:03:40.553
---再加3周---
2010-04-21T17:03:40.553

---使用时间调整函数---
2014-03-25T17:03:40.553
2014-03-31T17:03:40.553
2014-03-19T17:03:40.553
2014-03-25T17:03:40.583

---截断时间精确位--
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40

---使用时区---
2014-03-25T09:03:40.583Z[UTC]
+08:00

---时间上使用时区偏移---
17:03:40.585+08:00
11:03:40.585+02:00

---时间加减---
2014-03-25T17:03:40.585
2017-05-26T17:03:40.585
2014-03-25T17:03:43.585000005
2014-03-25T23:03:40.586

 

延伸 · 阅读

精彩推荐