0% found this document useful (0 votes)
18 views

Date and Time

Uploaded by

dhayananthb07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Date and Time

Uploaded by

dhayananthb07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Java Date Time

by
Giridhara R M.E.,M.B.A.,
Assistant Professor,
Dept of CSE,
Sri Eshwar College of Engineering

25/11/23 GIRI - DATE & TIME 1


Java Date Time

The Date-Time API uses the calendar system defined in ISO-8601 as the
default calendar.

This calendar is based on the Gregorian calendar system and is used
globally as the de facto standard for representing date and time.

The core classes in the Date-Time API have names such as
LocalDateTime, ZonedDateTime, and OffsetDateTime.

The Date-Time package, java.time, introduced in the Java SE 8.

The Date-Time API uses the
Unicode Common Locale Data Repository (CLDR).

The Date-Time API also uses the Time-Zone Database (TZDB).

25/11/23 GIRI - DATE & TIME 2


Date-Time Design Principles

The Date-Time API was developed using several design principles.
– Clear
– Fluent
– Immutable
– Extensible

25/11/23 GIRI - DATE & TIME 3


The Date-Time Packages

The Date-Time API consists of the primary package, java.time, and four
subpackages:
– java.time - The core of the API for representing date and time.
– java.time.chrono - The API for representing calendar systems other than
the default ISO-8601.
– java.time.format - Classes for formatting and parsing dates and times.
– java.time.temporal - Extended API, primarily for framework and library
writers, allowing interoperations between the date and time classes,
querying, and adjustment.
– java.time.zone - Classes that support time zones, offsets from time
zones, and time zone rules.
25/11/23 GIRI - DATE & TIME 4
Method Naming Conventions

The Date-Time API offers a rich set of methods within a rich set of classes.

There is also standardization regarding the method name prefixes.

Because most of the classes in the Date-Time API are immutable, the API
does not include set methods.

The immutable equivalent of a set method is with.

The following table lists the commonly used prefixes:

25/11/23 GIRI - DATE & TIME 5


Method Naming Conventions

Prefix Method Type Use

Creates an instance where the factory is primarily validating


of static factory
the input parameters, not converting them.

Converts the input parameters to an instance of the target


from static factory
class, which may involve losing information from the input.

Parses the input string to produce an instance of the target


parse static factory
class.

25/11/23 GIRI - DATE & TIME 6


Method Naming Conventions

Prefix Method Type Use


Uses the specified formatter to format the values in
format instance
the temporal object to produce a string.
get instance Returns a part of the state of the target object.
is instance Queries the state of the target object.
Returns a copy of the target object with one
with instance
element changed.
Returns a copy of the target object with an amount
plus instance
of time added.
Returns a copy of the target object with an amount
minus instance
of time subtracted.

25/11/23 GIRI - DATE & TIME 7


Standard Calendar

There are two basic ways to represent time.
– human terms - such as year, month, day, hour, minute and second.
– machine time - measures time continuously along a timeline from an
origin, called the epoch, in nanosecond resolution.

The Date-Time package provides a rich array of classes for representing
date and time which supports both way of representations.

The following table summarizes the temporal-based classes in the java.time
package that store date and/or time information, or that can be used to
measure an amount of time.

25/11/23 GIRI - DATE & TIME 8


Standard Calendar

25/11/23 GIRI - DATE & TIME 9


DayOfWeek and Month Enums

DayOfWeek
– The DayOfWeek enum consists of seven constants that describe the
days of the week: MONDAY through SUNDAY.
– The integer values of the DayOfWeek constants range from 1 (Monday)
through 7 (Sunday).
– he following code adds 3 days to "Monday" and prints the result. The
output is "THURSDAY":

System.out.printf("%s%n", DayOfWeek.MONDAY.plus(3));

25/11/23 GIRI - DATE & TIME 10


DayOfWeek

By using the getDisplayName(TextStyle, Locale) method, you can retrieve a
string to identify the day of the week in the user's locale.

The TextStyle enum enables you to specify what sort of string you want to
display: FULL, NARROW (typically a single letter), or SHORT (an
abbreviation).

Example Program :
DayOfWeek dow = DayOfWeek.MONDAY; Output :
Locale locale = Locale.getDefault(); Monday
System.out.println(dow.getDisplayName(TextStyle.FULL, locale)); M
System.out.println(dow.getDisplayName(TextStyle.NARROW, locale)); Mon
System.out.println(dow.getDisplayName(TextStyle.SHORT, locale));

25/11/23 GIRI - DATE & TIME 11


Month

The Month enum includes constants for the twelve months, JANUARY
through DECEMBER.

the integer value of each constant corresponds to the ISO range from 1
(January) through 12 (December).

The Month enum also includes a number of methods.

The following line of code uses the maxLength method to print the
maximum possible number of days in the month of February.

The output is "29":

System.out.printf("%d%n", Month.FEBRUARY.maxLength());

25/11/23 GIRI - DATE & TIME 12


Month

The Month enum also implements the getDisplayName(TextStyle, Locale)
method to retrieve a string to identify the month in the user's locale using
the specified TextStyle.

Example Program :
Month month = Month.AUGUST; Output:
Locale locale = Locale.getDefault(); August
System.out.println(month.getDisplayName(TextStyle.FULL, locale)); A
System.out.println(month.getDisplayName(TextStyle.NARROW, locale)); Aug
System.out.println(month.getDisplayName(TextStyle.SHORT, locale));

25/11/23 GIRI - DATE & TIME 13


Date Classes

LocalDate

YearMonth

MonthDay

Year

25/11/23 GIRI - DATE & TIME 14


LocalDate

A LocalDate represents a year-month-day in the ISO calendar and is useful
for representing a date without a time.

The following examples use the of and with methods to create instances of
LocalDate:

LocalDate date = LocalDate.of(2000, Month.NOVEMBER, 20);
● LocalDate nextWed = date.with(TemporalAdjusters.next(
DayOfWeek.WEDNESDAY));


The getDayOfWeek method returns the day of the week that a particular
date falls on. the following line of code returns "MONDAY":
DayOfWeek dotw = LocalDate.of(2012, Month.JULY, 9).getDayOfWeek();

25/11/23 GIRI - DATE & TIME 15


LocalDate

The following example uses a TemporalAdjuster to retrieve the first
Wednesday after a specific date.

LocalDate date = LocalDate.of(2000, Month.NOVEMBER, 20);


TemporalAdjuster adj = TemporalAdjusters. Output:
next(DayOfWeek.WEDNESDAY); For the date of
LocalDate nextWed = date.with(adj); 2000-11-20,
System.out.printf("For the date of %s, the next
the next Wednesday is %s.%n", Wednesday is
date, nextWed); 2000-11-22.

25/11/23 GIRI - DATE & TIME 16


YearMonth

The YearMonth class represents the month of a specific year.

The following example uses the YearMonth.lengthOfMonth() method to
determine the number of days for several year and month combinations.

YearMonth date = YearMonth.now();


System.out.printf("%s: %d%n", date, date.lengthOfMonth());
Output:
YearMonth date2 = YearMonth.of(2010, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth()); 2013-06: 30
2010-02: 28
YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY); 2012-02: 29
System.out.printf("%s: %d%n", date3, date3.lengthOfMonth());

25/11/23 GIRI - DATE & TIME 17


MonthDay

The MonthDay class represents the day of a particular month, such as New
Year's Day on January 1.

The following example uses the MonthDay.isValidYear method to determine
if February 29 is valid for the year 2010.

The call returns false, confirming that 2010 is not a leap year.

MonthDay date = MonthDay.of(Month.FEBRUARY, 29);


boolean validLeapYear = date.isValidYear(2010);

25/11/23 GIRI - DATE & TIME 18


Year

The Year class represents a year.

The following example uses the Year.isLeap method to determine if the
given year is a leap year.

The call returns true, confirming that 2012 is a leap year.

boolean validLeapYear = Year.of(2012).isLeap();

25/11/23 GIRI - DATE & TIME 19


Date and Time Classes

LocalTime

LocalDateTime

Time Zone and Offset Classes
– ZoneId and ZoneOffset
– The Date-Time Classes
– ZonedDateTime
– OffsetDateTime
– OffsetTime

25/11/23 GIRI - DATE & TIME 20


Instant Class

One of the core classes of the Date-Time API is the Instant class, which
represents the start of a nanosecond on the timeline.

This class is useful for generating a time stamp to represent machine time.

A value returned from the Instant class counts time beginning from the first
second of January 1, 1970 (1970-01-01T00:00:00Z) also called the EPOCH.

An instant that occurs before the epoch has a negative value, and an instant
that occurs after the epoch has a positive value.

The other constants provided by the Instant class are MIN, representing the
smallest possible (far past) instant, and MAX, representing the largest (far
future) instant.

25/11/23 GIRI - DATE & TIME 21


Java date time classess

Parsing and Formatting
– Parsing
– Formatting

The Temporal Package
– Temporal and TemporalAccessor
– ChronoField and IsoFields
– ChronoUnit

Temporal Adjuster
– Predefined Adjusters
– Custom Adjusters
25/11/23 GIRI - DATE & TIME 22
Java date time classess

Temporal Query
– Predefined Queries
– Custom Queries

Period and Duration
– Duration
– ChronoUnit
– Period

Clock

Non-ISO Date Conversion
– Converting to a Non-ISO-Based Date
– Converting to an ISO-Based Date

25/11/23 GIRI - DATE & TIME 23


25/11/23 GIRI - DATE & TIME 24

You might also like