1z0-809 Oracle Java SE 8 Programmer II – Use Java SE 8 Date/Time API
January 27, 2023

1. 7.1 Create and manage date-based and time-based events

Hello everyone. In this lesson, we will focus on the first item of the Use Java Se Eight Date Time API topic and the Javas Eight Programmer Two Exam Syllabus that is creating and managing date based and time based events, including a combination of date and time into a single object using local date, local time, local date time, instant period and duration. The support for date and time in Java se Seven and earlier versions was not very good due to many serious problems such as its ambiguity or concurrency. In Java Se eight, a new date time API was introduced to solve those issues. This API came with some great features, including immutability, fluency, and extensibility. Most of the classes and the Date Time API are immutable. This means that the API is thread safe, allowing you to get rid of concurrency handling. Due to this characteristic, most date and time objects must be created using factory methods rather than constructors. The date time API provides a fluent interface. This feature means that method invocations can be chained together, making the code more concise and easier to understand. The Date Time API enables you to work with the ISO 8601 chronology as well as nonstandard calendar systems such as Buddhist or Hebrew calendars.

You can even build your own calendar. The Date Time API provides a rich set of methods with consistent naming styles across core classes. Here are the name or name prefix of commonly used methods of the API a method starting with of creates an instance of the class using input values. A method named from converts the input value to an instance of the class. A method beginning with returns a copy of the object with the specified field modified. A method that starts with plus or minus returns a copy of the object with the specified amount added or subtracted. A method beginning with at combines the object with an instance of another type to form an object of a more complex class. A method called parse parses the input string to create an instance of the class. A method starting with Get retrieves the value of the specified field of the object. A method that begins with Is queries the state of the object. You will see demonstrations of these methods momentarily. A local date instance represents a date without a time zone in the ISO 8601 calendar system. Here is a code fragment that illustrates methods of the local date class. This static factory method obtains a local date instance representing the 1 January 2018. The value of that object is copied to a second local date, with the month field set to six. That second date is moved 100 days forward, then 50 days backward.

The next statement checks if the year containing the date referenced by variable date four is a leap year, and the last statement gets the value of the day of month field of that date. The value of all variables in this example are shown over here. A local time instance represents a time without a time zone in the ISO 8601 calendar system. In this example, the time one variable references an object representing 20 hours 30 minutes 40 seconds, which is parsed from this string. The minute of our field of that object is then set to ten. So time two refers to 20 hours, ten minutes and 40 seconds. The comparison over here checks if time two is after time one. Of course, 20 hours ten minutes 40 seconds comes before 20 hours 30 minutes 40 seconds. So this expression evaluates to false. The last statement retrieves the value of the second of minute field of time two, which is the same as that of time one.

So the value of this variable is 40. A local date time instance represents a date time without a time zone in the ISO 8601 calendar system. In this code fragment, a local date and a local time are created, then combined to construct a local date time instance which refers to 12 hours 45 minutes 30 seconds. On the 15 March 2018 after its creation, that local date time is copied with two months added and 12 hours subtracted. This method chaining is a representation of the Fluent interface that comes with the date time API over here. Another local date and local time are obtained from the second local date time. An instant object represents an instantaneous point on the timeline. Here is a code snippet demonstrating methods of the instant class. This method obtains the current instant from the system clock. The method over here retrieves an instant indicating 1. 5 billion second after the epic, which is a point in time in 2017. At the time of this recording, that 1. 5 billion second instant has passed. So this method returns true.

A period instance indicates a date based amount of time in the ISO 8601 calendar system. In this example, period one refers to a period of one year, two months and three days. Using the Fluent interface, you can copy that period instance with some modifications. Like this, such a period can be used for a date calculation. A duration instance indicates a time based amount of time in the ISO 8601 calendar system. In this code example over here, the duration one variable references an object parsed from this string, indicating a duration of 2 hours and 30 minutes. Notice that a textual representation of a period or duration starts with an optional sign symbol which is plus or minus, followed by the letter p in either upper or lower case. In the case of duration, if a time section exists, it must be preceded by the letter t. Two local time instances are created here and the duration between them is then computed. It is easy to see that the span between time one and time two is also 2 hours and 30 minutes. Therefore, duration two and duration one are equal and the value of the is equal variable is true. Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. Like other classes in the date time API. The local date time class is immutable. When you alter an instance of it like this, the alteration returns a new object rather than making changes to the existing one.

As such, the state of the object referenced by this Date Time variable is the same as when it was created. So the correct answer is A. Now for the second question. Notice that an instant object represents an instantaneous point on the timeline. It is not meant to be used for any time unit larger than second. Therefore, this operation is inappropriate and throws an unsupported temporal type exception. So the correct answer is C. Now for the third question. This string argument to the Parse method is completely valid. The Parsing result is used to create a new period instance with changes in the amount of month and day. That period is then subtracted from a Local Date instance. One year, three months and 15 days before the 15 March 2018 is the 30 November 2016, so the correct answer is B. Now for the fourth question. The string argument over here starts with the letter P and its time section follows the letter T. As such, this string is a valid textual representation of a duration instance. This duration is then added to 1230.

Since the given code fragment is valid, this statement prints 16 on the console when executed. Therefore, the correct answer is A. In this lesson, we have gone over six core classes and the date time API, including local date, local time, local date time, instant period and duration. To sum up a local date, local time and local date time represent a date time and date time, respectively. An instant indicates an instantaneous point on the timeline. A period and duration denote a date based and time based, respectively. Amount of Time all classes introduced in this lesson are unrelated to actual time zones.

2. 7.2 Work with dates and times across timezones and manage changes resulting from

Hello everyone. In this lesson, we will go over the second item of the Use Java Se Eight Date and Time API topic in the Java Se Eight Programmer two exam syllabus that is working with dates and times across time zones and managing changes resulting from daylight savings, including format, date and times value. In order to work across time zones, a date time instance must have a time zone component. In the Java. Date time API. A Zone ID instance represents the identity of a time zone, which is used to identify the rules to convert between an instant and a local date time. The main method used to create a Zone ID instance is the Static factory method over here. Its string argument has two valid forms indicating two types of zone ID. The first ID type of a Zone ID instance is fixed offset. An ID of this type indicates a fully resolved offset from Coordinated Universal Time or UTC for short. You can use one of the three prefixes for this ID, including UTC, GMT, and UT. Such a prefixed may be left off. The second ID type is Geographical Region, an area where a specific set of rules for finding the offset from UTC applies. You can retrieve a set of all available time zones in the form of region based IDs using this method. After the set is created, its elements are printed on the console when this statement is executed. Zone offset is a subclass of Zone ID.

An instance of this class represents the amount of time that a time zone differs from UTC. Like zone ID, an instance of the zone offset class can be created by passing a string, which is an offset ID to the of method. For type safety, you can use the of hours method, which accepts an int argument indicating the number of offset hours. You can also construct a zone offset in a higher level of precision using similar methods such as of hours minutes or of hours minutes seconds. These three statements create zone offset instances with the same value. The offset ID argument to the of method can be in the normalized form or very simple. Note that when the input string indicates a positive offset, the plus sign is required. In case the offset is zero, the letter Z is used instead of the number zero. A Zoned date time instance can be considered a composition of a local date time and a Zone ID instance. Let’s get started with offset time, the simplest class for handling times across time zones.

An offset time instance indicates a time with an offset from UTC in the ISO 8601 calendar system, storing all time fields to a precision of nanoseconds. In this example, an offset time instance is created from a local time and a zone offset object. That offset time instance is then copied with some fields changed. The value of the hour field over here is ten. Such a value is then added by three. After that, this expression moves the new offset time object to another time zone an hour ahead. Therefore, the hour value extracted here is 14. The offset date time class is similar to offset time with date fields added. An instance of this type indicates a date time with an offset from UTC in the ISO 8601 calendar system, storing all date and time fields to a precision of nanoseconds. In the code example over here, these statements construct two offset date time instances using local date time, local date local time, and zone offset instances. This local date time instance has an hour field with the value of 15. After this value is subtracted by four, it becomes eleven, even though eleven is greater than ten. The hour eleven in this case is in the zone offset plus three, while the hour ten is in the zone offset plus one.

Therefore, the offset date time instance over here is earlier than this one, and this expression evaluates true. The zone date time class is similar to offset date time. An instance of this type indicates a date time with a time zone in the ISO 8601 calendar system, storing all date and time fields to a precision of nanoseconds. The time zone in this case may be either a fixed offset or a regionbased zone. Notice that instant offset date time and zoned date time objects all store an instant on the timeline to the nanosecond precision. An instant object is the simplest form of a time. Instant offset date time adds an offset from UTC to the instant zone.

Date time must be used when a regionbased time zone is needed. This code fragment demonstrates the zone date time class. The first object here is constructed from a local date time and a zone ID at 1520 February 2018. Los Angeles is not in daylight savings time, and the time offset there is minus eight. On March 15, that city is in daylight savings time, so the offset increases to minus seven. When working with date and time values across time zones, you must pay attention to daylight savings. In regions where daylight savings apply, time changes twice a year. During those changes, two scenarios happen. When daylight savings time starts, the clocks jump forward and a gap exists with no valid offset. When it ends, the clocks go backward and an overlap occurs with two valid offsets.

If a local date time falls into the middle of a gap, then the zoned date time will have a local date time shifted forward by the length of the gap, resulting in a date time in the later offset. If a local date time is in an overlap duration, then the previous offset is preserved by default. You can manage that behavior when an overlap happens using methods with earlier offset at overlap and with later offset at overlap of the zoned daytime class. This code snippet illustrates how you can manage daylight savings at 02:00 on the 25 March 2018. Daylight savings time starts in Paris. Therefore, 230 on that day is an invalid time, resulting in a time shift to the later offset. That is why this zoned date time instance indicates 330. Daylight Savings ends at 03:00 on the 28 October 2018. When a time is specified in the overlap duration like this, the earlier offset is preserved. You can change the default behavior using either of these methods. Let’s move on to formatting date and time values. The date time formatter class is used for parsing and formatting date and time objects.

This class defines many standard formatters, including these constants. Each of them is the default formatter of a date and time class shown over here. These predefined constants are not the only ones provided by the date time formatter class. You can find more of them in the Java API documentation. In addition to standard formatters, the date time formatters class allows you to create custom formatters using a given pattern or format style. The format style enum type is simple, consisting of four constants full, long, medium, and short. This type is used to format date and time values to strings with different detail levels. These are the most commonly used pattern letters for formatting, date and time values. The letter meaning is shown in this column. Here are examples of resulting formatted strings. Notice that the number of a pattern letter affects how an object is formatted. The parse method of a date time class obtains an instance of that class from an input string, while the format method formats the date time instance using the specified formatter.

In other words, the parse and format methods convert between a date time instance and its textual representation based on a given formatter. These methods are defined in all date and time classes that we have gone over so far. Here is a code example illustrating the formatting of date time values. This pattern is used to create a new date time formatter instance. Notice that if you want to use a string literal in a pattern, you need to wrap that literal in single quotes like this the created formatter in this case is then used to construct a local date time, which is in turn used to create a zoned date time. These are two other date time formatters. When using those formatters to format this zoned date time instance, the standard formatter produces this string, while the one created from the long format style generates this text representation. Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. This method produces an instance of zone offset, a subclass of zone ID. There is nothing wrong with this assignment, so no compilation errors occur. When a zone ID argument is passed to the of method defined in the zone offset class. It must start with a plus or minus sign. If the offset is zero, the zone ID must be represented by the letter z instead of the number zero.

The argument in this scenario is invalid, resulting in an exception at runtime, so the correct answer is C. Now for the second question. The objects referenced by these variables are constructed from the same local date time instance, but with different time zones. Midnight in Paris in an hour earlier than midnight in London therefore this method returns a duration of -1 hour so the correct answer is c now for the third question. This with earlier offset at overlap method only takes effect when the object on which it is invoked indicates a point in the overlap duration when daylight savings time ends, it does not make any changes when daylight savings time starts. This time, 230 is in the middle of an offset gap. The general rule for this case is the local time moves forward an hour.

As a result, this object refers to 330 on the 11 March 2018, so the correct answer is B. Now for the fourth question. Notice that the letter m in lowercase denotes minutes, not months. With this pattern, the formatted string will then be 3015 2018, and the correct answer is C. In this lesson, we have gone over how to work with dates and times across time zones and how to format date and time values. In summary, a zone ID instance represents the identity of a time zone. A zoned date time instance indicates a date time with a time zone. The date time formatter class is used for parsing and formatting date and time values. The parse and format methods convert between a date time instance and its textual representation based on a given date time formatter.

3. 7.3 Define and create and manage date-based and time-based events using Instant

Hello everyone. In this lesson, we will go through the last item of the Using Javas Date Time API topic and the Java Sea Programmer two exam syllabus that is defining and creating and managing date based and time based events using instant period, duration and temporal unit. In the exam syllabus, four types are covered in this subtopic, including instant period, duration and temporal unit. However, these classes have been mentioned in previous videos, so this lesson will only focus on the temporal unit interface. The period and duration classes will also be revisited briefly to see how they work with Daylight Savings. Period and duration are a bit different in the way that they handle Daylight Savings. Instances of both of these classes can be converted to a number of days. A period of one day is considered a conceptual day, maintaining local time and ignoring Daylight Savings changes if they happen. Meanwhile, a duration of one day is always exactly 24 hours notice. That day is the only time unit that is supported by both period and duration classes.

In this example, both of these objects, a period and a duration indicate a time span of one day in the early morning on the 11 March 2018 in New York. Daylight Savings Time starts when you add a period of a day to a time shortly before the offset change. That time moves one day ahead, preserving the local time. On the other hand, a duration of one day adds exactly 24 hours to the previous time. Since the daylight Saving moves the offset 1 hour forward, 1 hour added to the local time. The measurement of time is built upon time units. Such units can be very small like the microsecond, and can be very big like the century. Implementations of the temporal unit interface represent those units of time.

The Java API provides a temporal unit implementation out of the box, which is Krono unit. This enum type provides a set of predefined time units including nanos, Forever and Everything in between. These are several noticeable methods defined in the temporal unit interface. This add to method returns a copy of the specified temporal object with the specified amount of time added. The between method computes the amount of time between two temporal objects, and each of these methods checks if the temporal unit represents a component of a date or time. Here is an example demonstrating the temporal unit interface. This interface is mainly used for computations of date and time values. Like this the local Date time object over here refers to twelve on the 30 June 2018. After this, time is subtracted by one decade, added by eleven years and 10 hours, then subtracted by 30 minutes. It becomes 21 30 on the 30 June 2019.

Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. When adding a duration to a particular time, the amount of time represented by that duration is always added, no matter whether daylight savings occurs in the given scenario, a duration of 2 hours is added to a point shortly before daylight savings time ends, resulting in the local time moving 2 hours forward and an hour back. So the correct answer is A. Now for the second question.

The between method defined in the temporal unit interface computes the amount of time between two temporal objects, returning a whole number that indicates the number of complete units. The span between temporal one and temporal two is 30 minutes less than a full hour, so this amount is rounded down to 0 hour. So the correct answer is A. In this lesson, we have gone over the temporal unit interface and revisited the period and duration classes. In short, a period of one day is a conceptual day, maintaining local time and ignoring daylight savings. A duration of one day is exactly 24 hours. The Chrono unit enum type, which is a temporal unit implementation, provides a set of predefined time units from nanos to forever.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!