Java Date and time (15) Java.time.ZonedDateTime__Java

Source: Internet
Author: User
Tags time zones
Java date and time

Original link
author : Jakob Jenkov
Translator : Ah for
Table of Contents : http://blog.csdn.net/tjgykhulj/article/details/68952451
(All translators ' comments will appear in this form, and strikethrough lines indicate a disputed or ambiguous place)

The Zoneddatetime class is a class that represents date and time information with time zones in Java 8, a date-time feature. Can be used to indicate the start time of a real event, such as the launch time of a rocket, and so on.
The value of the Zoneddatetime class is immutable, so its method of calculation returns a new Zoneddatetime instance.

Create a Zoneddatetime instance
There are several ways to create a new Zoneddatetime instance. For example, use the current time as a value to create a new object:
Zoneddatetime dateTime = Zoneddatetime.now ();
Another way is to create a new object by using the specified year, hour, nanosecond, and time zone ID:

ZoneID ZoneID = Zoneid.of ("utc+1");
Zoneddatetime dateTime2 = Zoneddatetime.of (2015, all, 1234, ZoneID);

time to access the Zoneddatetime object
You can access its date time through these methods:

GetYear ()
getmonth () getdayofmonth () GetDayOfWeek ()
getdayofyear
() Gethour () Getminute ()
getsecond ()
Getnano ()

Some of these methods return int some return enum types, but you can get int values by using the GetValue () method in the enumeration type.

Calculation of Zoneddatetime
The Zoneddatetime class contains a series of methods that will help you to complete the time calculation:

Plusyears ()
plusmonths ()
plusdays ()
plushours (
) plusminutes ()
plusseconds () ( )
Minusyears ()
minusmonths () minusdays () minushours ()
minusminutes (
) minusseconds ()
Minusnanos ()

But pay attention to the calculation, if it happens to cross the daylight saving time (will fill an hour or minus one hour), may not get the desired results. An alternative correct approach is to use period:

Zoneddatetime zonedatetime = Previousdatetime.plus (Period.ofdays (3));

Time Zone
The time zone is represented by the ZoneID class, and you can instantiate it using Zoneid.now () or zoneid.of ("xxx"):

ZoneID ZoneID = Zoneid.of ("utc+1");

The parameter passed to the of () method is the ID of the time zone, such as "utc+1", which is an hourly difference from UTC (GMT), and you can use the jet lag you want to represent ZoneID (+1 and 5, etc.)
You can also use another way to represent the zone ID, that is, by using a region name:

ZoneID zoneId2 = Zoneid.of ("Europe/copenhagen");
ZoneID zoneId3 = Zoneid.of ("Europe/paris");

Next Chapter: Java.time.format.DateTimeFormatter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.