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

3

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)
3 views

3

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/ 16

Working with Dates & Times

Concepts
Representing Date

Representing Time

Representing Date & Time

Formatting Date & Time

Difference between Dates & Times

Introduction

Java has a built-in

java.time package which provides various classes to work with date and time.

Commonly used classes in the

java.time package are:

LocalDate class
LocalTime class
LocalDateTime class
DateTimeFormatter class
Period class
Duration class
1. Working with LocalDate class

1.1 Representing Date

Java

LocalDate class allows us to create a date object and represent a valid date
(year, month and day).

The

of() method of LocalDate class is used to create an instance of


LocalDate from the given year, month and day.

Code
JAVA

1 import java.time.LocalDate;
2
3 class Main {
4 public static void main(String[] arg
5 LocalDate dateObj = LocalDate.of
6 System.out.println(dateObj);
7 }
8 }

Output

2019-04-13

Comparison with Python

Code
PYTHON

1 from datetime import date


2
3 date_obj = date(2019, 4, 13)
4 print(date_obj)

Output

2019-04-13

Providing invalid inputs to the

of() method leads to an error.

Code
JAVA

1 import java.time.LocalDate;
2
3 class Main {
4 public static void main(String[] arg
5 LocalDate dateObj = LocalDate.of
6 System.out.println(dateObj);
7 }
8 }

Output

Exception in thread "main" java.time.Da

In the above code, we have provided 31 days for the month of February. As the
February month doesn't have 31 days, the

DateTimeException has been thrown.


1.2 Today's Date

The

LocalDate class provides now() method which returns the date object with
today's date.

Code
JAVA

1 import java.time.LocalDate;
2
3 class Main {
4 public static void main(String[] arg
5 LocalDate dateObj = LocalDate.no
6 System.out.println(dateObj);
7 }
8 }

Output

2022-12-20

1.3 Commonly used methods of LocalDate Object

Method Description

getYear() Returns the year value (2000, 2022...)

Returns the month (AUGUST,


getMonth()
SEPTEMBER...)

getMonthValue() Returns the month value (1 to 12)

getDayOfMonth() Returns the day of the month (1, 2, 3, ...31)

Returns the day of the week (MONDAY,


getDayOfWeek()
TUESDAY...)
Returns the int value of the day of the week
getValue()
(1, 2...7)
Method Description

plusDays(days) Returns the date object with days added

plusMonths(months) Returns the date object with months added

plusYears(years) Returns the date object with years added

Returns the negative integer if the given date


compareTo(date) is less, positive integer if the given date is
greater

Example 1:

Code
JAVA

1 import java.time.LocalDate;
2
3 class Main {
4 public static void main(String[]
5 LocalDate dateObj = LocalDate
6 System.out.println(dateObj.ge
7 System.out.println(dateObj.ge
8 System.out.println(dateObj.ge
9 System.out.println(dateObj.ge
10 }
11 }
Expand

Output

2019
APRIL
SATURDAY
6

Example 2:

Code
JAVA

i j i l
1 import java.time.LocalDate;
2
3 class Main {
4 public static void main(String[] arg
5 LocalDate dateObj = LocalDate.of
6 System.out.println(dateObj.plusY
7 }
8 }

Output

2024-04-13

Example 3:

Code
JAVA

1 import java.time.LocalDate;
2
3 class Main {
4 public static void main(String[]
5 LocalDate dateObj1 = LocalDat
6 LocalDate dateObj2 = LocalDat
7 if (dateObj1.compareTo(dateO
8 System.out.println("dateO
9 } else if (dateObj1.compareTo
10 System.out.println("dateO
11 } else {
Expand

Output

dateObj1 < dateObj2


In the above example, the

dateObj1 is less than the dateObj2 . So, the expression


dateObj1.compareTo(dateObj2) < 0 evaluates to true and the dateObj1 <
dateObj2 is displayed on the console.

2. Working with LocalTime class

2.1 Representing Time

Java

LocalTime class allows us to create a time object and represent a valid time
(hours, minutes and seconds).

Code
JAVA

1 import java.time.LocalTime;
2
3 class Main {
4 public static void main(String[] arg
5 LocalTime timeObj = LocalTime.of
6 System.out.println(timeObj);
7 }
8 }

Output

11:34:56
2.2 Commonly used methods of LocalTime Object

Method Description

now() Returns the current time

getHour() Returns the hour value

getMinute() Returns the minute value

getSecond() Returns the Seconds value

Returns the time object with specified hours


plusHours(hours)
added
Returns the time object with specified
plusMinutes(minutes)
minutes added
Returns the time object with specified
plusSeconds(seconds)
seconds added
Returns the negative integer if the given
compareTo(time) time is less, positive integer if the given
time is greater

Code
JAVA

1 import java.time.LocalTime;
2
3 class Main {
4 public static void main(String[]
5 LocalTime timeObj = LocalTime
6 System.out.println(timeObj);
7 System.out.println(timeObj.ge
8 System.out.println(timeObj.ge
9 System.out.println(timeObj.ge
10 }
11 }
Expand

Output
11:34:56
11
34
56

Comparison with Python

Code
PYTHON

1 from datetime import time


2
3 time_object = time(11, 34, 56)
4 print(time_object)
5 print(time_object.hour)
6 print(time_object.minute)
7 print(time_object.second)

Output

11:34:56
11
34
56

3. Working with LocalDateTime class

3.1 Representing Date and Time


Java

LocalDateTime class allows us to create a date-time object and represent a valid


date and time together.

Code
JAVA

1 import java.time.LocalDateTime;
2
3 class Main {
4 public static void main(String[]
5 LocalDateTime dateTimeObj =
6 System.out.println(dateTimeO
7 System.out.println(dateTimeO
8 System.out.println(dateTimeO
9 System.out.println(dateTimeO
10 }
11 }
Expand

Output

2018
11
10
15

Comparison with Python

Code
PYTHON

1 from datetime import datetime


2
3 date_time_obj = datetime(2018, 11,
4 print(date_time_obj.year)
5 print(date_time_obj.month)
6 print(date_time_obj.hour)
7 print(date_time_obj.minute)

Output

2018
11
10
15

4. Working with DateTimeFormatter class

4.1 Formatting Date and Time

The

DateTimeFormatter class have a ofPattern() method that creates an instance


of the DateTimeFormatter for the given pattern of the date, time and date-time
like,

mm/dd/yyyy
hh/mm/ss

FormatSpecifier Meaning Example

yyyy year with century 2014, 2015


FormatSpecifier Meaning Example

yy year without century 14, 15

January,
MMMM month as full name
February
month as abbreviated
MMM Jan, Feb
name
month zero-padded
MM 01, 02, …, 12
decimal number
month without zero-
M 1, 2, …, 12
padded decimal number
date zero-padded
dd 01, 02, …, 30
decimal number
date without zero-
d 1, 2, …, 30
padded decimal number

a am, pm of the day AM, PM

hour with zero-padded


HH 01, 02 …, 24
decimal number (1 - 24)
hour without zero-
H padded decimal number 1, 2 …, 24
(1 - 24)
hour with zero-padded
hh 01, 02 …, 12
decimal number (1 - 12)
hour without zero-
h padded decimal number 1, 2 …, 12
(1 - 12)
minute with zero-
mm padded decimal number 01, 02 …, 60
(1 - 60)
minute without zero-
m padded decimal number 1, 2 …, 60
(1 - 60)
second with zero-
ss padded decimal number 01, 02 …, 60
(1 - 60)
second without zero-
s padded decimal number 1, 2 …, 60
(1 - 60)

The classes

LocalDateTime , LocalDate , etc have the format() method which accepts


the DateTimeFormatter object of the required pattern as an argument and returns
a string after formatting the given localDate, localTime, or the localDatetime object
to the specified pattern.

Example 1:
Code
JAVA

1 import java.time.LocalDateTime;
2 import java.time.format.DateTimeForm
3
4 class Main {
5 public static void main(String[]
6 LocalDateTime now = LocalDate
7 DateTimeFormatter format1 = D
8 String formattedDateTime = no
9 System.out.println(formattedD
10 }
11 }
Expand

Output

13 Sep 2022 01:23:46 PM

Example 2:

Code
JAVA

1 import java.time.LocalDate;
2 import java.time.format.DateTimeForm
3
4 class Main {
5 public static void main(String[]
6 LocalDate now = LocalDate.now
7 DateTimeFormatter format1 = D
8 String formattedDate = now.fo
9 System.out.println(formattedD
10 }
11 }
Expand

Output
13 September 2022

4.2 Parsing Date and Time

The

DateTimeFormatter class have a parse() method which creates the respective


object (date, time, or date-time) from the give string.

Example 1:

Code
JAVA

1 import java.time.LocalDate;
2 import java.time.format.DateTimeForm
3
4 class Main {
5 public static void main(String[]
6 String dateStr = "28 Novembe
7 DateTimeFormatter format1 = D
8 LocalDate date = LocalDate.p
9 System.out.println(date);
10 }
11 }
Expand

Output

2018-11-28
5. Difference Between Dates & Times

5.1 Difference Between Dates

In Java, we have a

Period class to find the difference between two dates in terms of years, months
and days.

We can get the period object as the difference between two dates using

between() method.

Code
JAVA

1 import java.time.Period;
2 import java.time.LocalDate;
3 import java.time.format.DateTimeForm
4
5 class Main {
6 public static void main(String[]
7 LocalDate startDate = LocalD
8 LocalDate endDate = LocalDate
9 Period period = Period.betwee
10 System.out.println("Years: "
11 System.out.println("Months:
Expand

Output

Years: 1
Months: 8
Days: 1

5.2 Difference Between Times


In Java, we have a

Duration class to find the difference between two times in seconds.

Code
JAVA

1 import java.time.Duration;
2 import java.time.LocalTime;
3 import java.time.format.DateTimeForm
4
5 class Main {
6 public static void main(String[]
7 LocalTime startTime = LocalT
8 LocalTime endTime = LocalTime
9 Duration duration = Duration
10 System.out.println(duration.g
11 }
Expand

Output

You might also like