Home  >  Article  >  Java  >  How to handle time zone date and time in Java8

How to handle time zone date and time in Java8

WBOY
WBOYforward
2023-05-09 16:49:081414browse

I also found that these three classes do not have any time zone related information, but it cannot be said that they do not handle time zones, but that they selectively hide the processing of time zones. Internally they use the operating system's current time zone.

At the same time, Java also provides several classes in the java.time package to handle date and time APIs that require attention to time zones. They are java.time.ZonedDateTime and java.time.ZoneId. The former is used to handle datetimes that require a time zone, and the latter is used to handle time zones.

ZonedDateTime is similar to LocalDateTime and has almost the same API. In some ways, ZonedLocalTime if no time zone information is passed, it will default to the operating system's time zone, so the result is actually similar to LocalDateTime.

For example, we can use the now() method of ZonedDateTime to return the date and time of the current time zone (operating system time zone) and call parse() Method can convert a date and time in string format containing time zone information into a ZonedDateTime instance.

Java8Tester.java

import java.time.ZonedDateTime;

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester tester = new Java8Tester();
      tester.run();
   }

   public void run() {

      ZonedDateTime now = ZonedDateTime.now();
      System.out.println("当前日期时间是:" + now);

      ZonedDateTime datetime = ZonedDateTime.parse("2012-10-10T21:58:00+08:00");
      System.out.println("日期时间是:" + datetime);
   }
}

The running results are as follows

The current date and time is: 2018-10-08T22:21:56.806597 08:00[Asia/Shanghai] The date and time is: 2012-10-10T21:58 08:00

We can also call the ZonedDateTime object’s toLocalDate() and toLocalTime () The method will obtain the local date and time of the instance

import java.time.LocalTime;
import java.time.LocalDate;
import java.time.ZonedDateTime;

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester tester = new Java8Tester();
      tester.run();
   }

   public void run() {

      ZonedDateTime now = ZonedDateTime.now();
      System.out.println("当前日期时间是:" + now);

      LocalDate date = now.toLocalDate();
      System.out.println("本地日期是:" + now);

      LocalTime time = now.toLocalTime();
      System.out.println("本地时间是:" + time);


   }
}

The running result is as follows

The current date and time is: 2022-10-08T22:28: 10.389487 08:00[Asia/Shanghai] The local date is: 2022-10-08T22:28:10.389487 08:00[Asia/Shanghai] The local time is: 22:28:10.389487

P.S This The return value is so weird. Why does toLocalDate() also return the time?

Processing time zone

Time zone related information, we can use the ZoneId class to process.

For example, you can call the static method systemDefault() of the ZoneId class to return the current time zone.

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester tester = new Java8Tester();
      tester.run();
   }

   public void run() {

      ZoneId currentZone = ZoneId.systemDefault();
      System.out.println("当前时区是: " + currentZone);
   }
}

The running results are as follows

The current time zone is: Asia/Shanghai

We can also call the ZonedDateTime instancegetZone() Method to get the time zone where the instance is located

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester tester = new Java8Tester();
      tester.run();
   }

   public void run() {

      ZonedDateTime now = ZonedDateTime.now();
      System.out.println("当前时区是: " + now.getZone());
   }
}

The running result is as follows

The current time zone is: Asia/Shanghai

The above is the detailed content of How to handle time zone date and time in Java8. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete