3
3
Concepts
Representing Date
Representing Time
Introduction
java.time package which provides various classes to work with date and time.
LocalDate class
LocalTime class
LocalDateTime class
DateTimeFormatter class
Period class
Duration class
1. Working with LocalDate class
Java
LocalDate class allows us to create a date object and represent a valid date
(year, month and day).
The
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
Code
PYTHON
Output
2019-04-13
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
In the above code, we have provided 31 days for the month of February. As the
February month doesn't have 31 days, the
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
Method Description
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
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
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
Code
PYTHON
Output
11:34:56
11
34
56
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
Code
PYTHON
Output
2018
11
10
15
The
mm/dd/yyyy
hh/mm/ss
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
The classes
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
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
The
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
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
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