Java

How to handle dates and times in Java?

2 Mins read

It is a known fact that most of the Java developers are getting trouble with dates in Java. Sometimes it is hard to remember how to handle the date processes while programming. Even if you are a Senior Java Developer, it might be difficult to remember that.

google-search-java-date

Here are the some examples for Java dates:

We will use LocalDate object which is imported from java.time.LocalDate.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String currentDate = LocalDate.now().format(formatter);
System.out.println(currentDate);
Output:
17.07.2020

How to add days, weeks, months, years to date in Java?

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate currentDate = LocalDate.now();

String currentDatePlusOneDay = currentDate.plusDays(1).format(formatter);
String currentDatePlusOneWeek = currentDate.plusWeeks(1).format(formatter);
String currentDatePlusOneMonth = currentDate.plusMonths(1).format(formatter);
String currentDatePlusOneYear = currentDate.plusYears(1).format(formatter);

System.out.println(currentDate.format(formatter));
System.out.println(currentDatePlusOneDay);
System.out.println(currentDatePlusOneWeek);
System.out.println(currentDatePlusOneMonth);
System.out.println(currentDatePlusOneYear);
Output:
17.07.2020
18.07.2020
24.07.2020
17.08.2020
17.07.2021

How to extract days, weeks, months, years from date in Java?

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate currentDate = LocalDate.now();

String currentDateMinusOneDay = currentDate.minusDays(1).format(formatter);
String currentDateMinusOneWeek = currentDate.minusWeeks(1).format(formatter);
String currentDateMinusOneMonth = currentDate.minusMonths(1).format(formatter);
String currentDateMinusOneYear = currentDate.minusYears(1).format(formatter);

System.out.println(currentDate.format(formatter));
System.out.println(currentDateMinusOneDay);
System.out.println(currentDateMinusOneWeek);
System.out.println(currentDateMinusOneMonth);
System.out.println(currentDateMinusOneYear);
Output:
17.07.2020
16.07.2020
10.07.2020
17.06.2020
17.07.2019

How to get first day or last day for dates in Java?

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate currentDate = LocalDate.now();

String firstDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth()).format(formatter);
String lastDayOfMonth = currentDate.with(TemporalAdjusters.lastDayOfMonth()).format(formatter);
String firstDayOfYear = currentDate.with(TemporalAdjusters.firstDayOfYear()).format(formatter);
String lastDayOfYear = currentDate.with(TemporalAdjusters.lastDayOfYear()).format(formatter);

System.out.println(firstDayOfMonth);
System.out.println(lastDayOfMonth);
System.out.println(firstDayOfYear);
System.out.println(lastDayOfYear);
Output:
01.07.2020
31.07.2020
01.01.2020
31.12.2020

How to create date object with specific date?

LocalDate date = LocalDate.of(2020, 7, 17); // year, month, day
System.out.println(date.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")));
Output:
17.07.2020

How to handle DateTime in Java?

We have seen example codes for dates. Now, it is time to learn how to process DateTime operations. Please notice that, our new object type is LocalDateTime instead of LocalDate. We have also added the HH:mm:ss for the DateTimeFormatter.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
String currentDateTime = LocalDateTime.now().format(formatter);
System.out.println(currentDateTime);
Output:
17.07.2020 00:46:48

Rest of the examples would be same as LocalDate. You can add or extract hours, minutes, seconds and even nanos.

What are format patterns for the dates and times?

There are a lot of combination for the format patterns. Some examples are as follows;

"dd.MM.yyyy" // day, month, year | 17.07.2020
"yyyy-MM-dd" // year, month, day | 2020-07-17
"yy" // year | 20
"HH:mm:ss" // hour, minute, second | 12:24:36

Here is the table for all letters and examples for them. This table can be accessed on official JavaDoc page.

LetterDate or Time ComponentExamples
GEra designatorAD
yYear199696
YWeek year200909
MMonth in yearJulyJul07
wWeek in year27
WWeek in month2
DDay in year189
dDay in month10
FDay of week in month2
EDay name in weekTuesdayTue
uDay number of week (1 = Monday, …, 7 = Sunday)1
aAm/pm markerPM
HHour in day (0-23)0
kHour in day (1-24)24
KHour in am/pm (0-11)0
hHour in am/pm (1-12)12
mMinute in hour30
sSecond in minute55
SMillisecond978
zTime zonePacific Standard TimePSTGMT-08:00
ZTime zone-0800
XTime zone-08-0800-08:00
Recep Duman
26 posts

About author
I am a fan of programming, innovation, and web development. I’m also interested in technology and entrepreneurship.
Articles
Subscribe to Newsletter

To get the latest news, subscribe our channel.

2 Comments

  • Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *

×
Testing

What are Bug, Defect and Error? What are the Differences?