教程 > java8 教程 > 阅读:30

java8 新的 date/time api——迹忆客-ag捕鱼王app官网

使用java 8,引入了新的日期时间api,以涵盖以下旧日期api的缺点。

  • 不是线程安全 - java.util.date不是线程安全,因此开发人员必须在使用日期时处理并发问题。 新的日期时间api是不变的,没有设置器方法。
  • 设计不良 - 默认日期从1900年开始,月是从1开始,一天从0开始,因此没有统一性。 旧的api对日期操作的直接方法较少。 新的api为此类操作提供了许多实用方法。
  • 困难的时区处理 - 开发人员必须编写大量代码来处理时区问题。 新的api已开发出牢记特定于域的设计。

java 8 在 java.time 包下引入了一个新的日期时间 api。 以下是 java.time 包中引入的一些重要类。

  • local - 简化的日期时间 api,没有时区处理的复杂性。
  • zoned - 处理各种时区的专用日期时间 api。

local(本地) date-time api

localdate/localtimelocaldatetime 类简化了不需要时区的开发。 让我们看看他们的作用。

import java.time.localdate;
import java.time.localtime;
import java.time.localdatetime;
import java.time.month;
public class java8tester {
   public static void main(string args[]) {
      java8tester java8tester = new java8tester();
      java8tester.testlocaldatetime();
   }
    
   public void testlocaldatetime() {
      // 获取当前日期和时间
      localdatetime currenttime = localdatetime.now();
      system.out.println("current datetime: "   currenttime);
        
      localdate date1 = currenttime.tolocaldate();
      system.out.println("date1: "   date1);
        
      month month = currenttime.getmonth();
      int day = currenttime.getdayofmonth();
      int seconds = currenttime.getsecond();
        
      system.out.println("month: "   month  "day: "   day  "seconds: "   seconds);
        
      localdatetime date2 = currenttime.withdayofmonth(10).withyear(2012);
      system.out.println("date2: "   date2);
        
      //12 december 2021
      localdate date3 = localdate.of(2021, month.december, 12);
      system.out.println("date3: "   date3);
        
      //22 hour 15 minutes
      localtime date4 = localtime.of(22, 15);
      system.out.println("date4: "   date4);
        
      //解析字符串
      localtime date5 = localtime.parse("20:15:30");
      system.out.println("date5: "   date5);
   }
}

上述代码运行结果如下

java8 本地日期和时间api示例


zoned(分区) date-time api

在考虑时区时,将使用分区日期时间 api。 让我们看看他们的作用。

import java.time.zoneddatetime;
import java.time.zoneid;
public class java8tester {
   public static void main(string args[]) {
      java8tester java8tester = new java8tester();
      java8tester.testzoneddatetime();
   }
    
   public void testzoneddatetime() {
      // get the current date and time
      zoneddatetime date1 = zoneddatetime.parse("2021-12-03t10:15:30 05:30[asia/karachi]");
      system.out.println("date1: "   date1);
        
      zoneid id = zoneid.of("europe/paris");
      system.out.println("zoneid: "   id);
        
      zoneid currentzone = zoneid.systemdefault();
      system.out.println("currentzone: "   currentzone);
   }
}

上述代码运行结果如下

java8 分区 日期和时间 api


计时单位枚举

java 8 中添加了 java.time.temporal.chronounit 枚举,以替换旧 api 中用于表示日、月等的整数值。让我们看看它们的实际应用。

import java.time.localdate;
import java.time.temporal.chronounit;
public class java8tester {
   public static void main(string args[]) {
      java8tester java8tester = new java8tester();
      java8tester.testchromounits();
   }
    
   public void testchromounits() {
      // 获取当前日期
      localdate today = localdate.now();
      system.out.println("current date: "   today);
        
      // 在当前日期上增加一周
      localdate nextweek = today.plus(1, chronounit.weeks);
      system.out.println("next week: "   nextweek);
        
      // 在当前日期上增加一个月
      localdate nextmonth = today.plus(1, chronounit.months);
      system.out.println("next month: "   nextmonth);
        
      // 在当前日期上增加一年
      localdate nextyear = today.plus(1, chronounit.years);
      system.out.println("next year: "   nextyear);
        
      // 在当前日期上增加十年
      localdate nextdecade = today.plus(1, chronounit.decades);
      system.out.println("date after ten year: "   nextdecade);
   }
}

上述代码运行结果如下所示

java8 计时单位枚举


period 和 duration

在 java 8 中,引入了两个专门的类来处理时间差异。

  • period - 它处理基于日期的时间量。
  • duration - 它处理基于时间的时间量。

让我们看看他们的应用。

import java.time.temporal.chronounit;
import java.time.localdate;
import java.time.localtime;
import java.time.duration;
import java.time.period;
public class java8tester {
   public static void main(string args[]) {
      java8tester java8tester = new java8tester();
      java8tester.testperiod();
      java8tester.testduration();
   }
    
   public void testperiod() {
      // 获取当前日期
      localdate date1 = localdate.now();
      system.out.println("current date: "   date1);
        
      // 在当前日期上添加一个月
      localdate date2 = date1.plus(1, chronounit.months);
      system.out.println("next month: "   date2);
      
      period period = period.between(date2, date1);
      system.out.println("period: "   period);
   }
    
   public void testduration() {
      localtime time1 = localtime.now();
      duration twohours = duration.ofhours(2);
        
      localtime time2 = time1.plus(twohours);
      duration duration = duration.between(time1, time2);
        
      system.out.println("duration: "   duration);
   }
}

上述代码运行结果如下

java8 period 和 duration示例


时间调节器

temporaladjuster 用于执行日期数学运算。 例如,获取“每月的第二个星期六”或“下星期二”。 让我们看看他们的应用。

import java.time.localdate;
import java.time.temporal.temporaladjusters;
import java.time.dayofweek;
public class java8tester {
   public static void main(string args[]) {
      java8tester java8tester = new java8tester();
      java8tester.testadjusters();
   }
    
   public void testadjusters() {
      // 获取当前日期
      localdate date1 = localdate.now();
      system.out.println("current date: "   date1);
        
      //下个星期二
      localdate nexttuesday = date1.with(temporaladjusters.next(dayofweek.tuesday));
      system.out.println("next tuesday on : "   nexttuesday);
        
      //得到下个月的第二个星期六
      localdate firstinyear = localdate.of(date1.getyear(),date1.getmonth(), 1);
      localdate secondsaturday = firstinyear.with(temporaladjusters.nextorsame(
         dayofweek.saturday)).with(temporaladjusters.next(dayofweek.saturday));
      system.out.println("second saturday on : "   secondsaturday);
   }
}

上述代码运行结果如下所示

java8 时间调节器


向后兼容

toinstant() 方法被添加到原始的 datecalendar 对象中,可用于将它们转换为新的 date-time api。 使用 ofinstant(insant,zoneid) 方法获取 localdatetimezoneddatetime 对象。 让我们看看他们的应用。

import java.time.localdatetime;
import java.time.zoneddatetime;
import java.util.date;
import java.time.instant;
import java.time.zoneid;
public class java8tester {
   public static void main(string args[]) {
      java8tester java8tester = new java8tester();
      java8tester.testbackwardcompatability();
   }
    
   public void testbackwardcompatability() {
      // 获取当前日期
      date currentdate = new date();
      system.out.println("current date: "   currentdate);
        
      // 以毫秒为单位获取当前日期的瞬间
      instant now = currentdate.toinstant();
      zoneid currentzone = zoneid.systemdefault();
        
      localdatetime localdatetime = localdatetime.ofinstant(now, currentzone);
      system.out.println("local date: "   localdatetime);
        
      zoneddatetime zoneddatetime = zoneddatetime.ofinstant(now, currentzone);
      system.out.println("zoned date: "   zoneddatetime);
   }
}

上述代码运行结果如下

java8 向后兼容

查看笔记

扫码一下
查看教程更方便
网站地图