jdk8的datetime时间函数使用示例

时间:2021-07-03 16:42:37 

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

标签:jdk8,datetime,时间
0
投稿

猜你喜欢

  • 详解java开发webservice的几种方式

    2023-03-13 04:31:33
  • java实现置换密码加密解密

    2022-01-21 07:08:48
  • java synchronized的用法及原理详解

    2023-07-17 04:42:54
  • springboot 整合hbase的示例代码

    2023-02-11 19:35:20
  • GC调优实战之高分配速率High Allocation Rate

    2021-09-30 22:03:21
  • C#使用foreach遍历哈希表(hashtable)的方法

    2022-10-07 20:00:44
  • Android Gradle多渠道打包的实现方法

    2023-04-26 17:03:26
  • SWT(JFace)体验之ViewForm的使用

    2023-10-20 13:02:01
  • C#类中方法的执行顺序是什么

    2022-07-01 03:55:15
  • Java数据结构顺序表的详细讲解

    2023-11-20 23:01:35
  • Android 实现单线程轮循机制批量下载图片

    2022-11-05 11:03:45
  • 利用java实现中奖概率详情

    2021-08-10 06:56:58
  • C#实现图像锐化的方法

    2023-07-23 01:08:21
  • Spring Boot实现分布式系统中的服务发现和注册(最新推荐)

    2022-07-10 03:50:23
  • c#装箱和拆箱知识整理

    2023-03-14 00:42:02
  • 实例化JFileChooser对象报空指针异常问题的解决办法

    2023-10-05 11:36:18
  • Android APK文件在电脑(PC虚拟机)上面运行方法

    2023-10-05 08:44:06
  • SpringBoot基于Actuator远程关闭服务

    2022-06-24 21:36:37
  • 深入探究Spring底层核心原理

    2023-03-05 08:32:16
  • java返回json请求中文变成问号的问题及解决

    2023-11-01 02:56:34
  • asp之家 软件编程 m.aspxhome.com