欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jdk8的datetime时间函数使用示例

程序员文章站 2024-02-24 23:54:34
jdk8已发布,写了一个datetime时间函数使用方法的小示例复制代码 代码如下:package datetime; import static java.time.t...

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