Design a site like this with WordPress.com
Get started

LocalDate class in java

Java 8 has introduced various classes to work with date and time objects. Lets see today LocalDate class in java 8 and how can we use it in our day to day coding.

Lets look at few of the useful methods provided and understand how it works

System.out.println("My Current TimeZone >> " + ZoneOffset.systemDefault());
System.out.println("Is same as zone >>"+LocalDate.now(Clock.system(ZoneOffset.of("+05:30"))));
		
		
System.out.println(LocalDate.now());
System.out.println(LocalDate.now(Clock.systemUTC()));
System.out.println(LocalDate.now(Clock.system(ZoneOffset.of("+10"))));
System.out.println(LocalDate.now(Clock.system(ZoneOffset.of("+0530"))));

line 1, will print my current timezone

line 2, will print my current timezone converted into UTC offset.

line 3, “LocalDate.now” will print my current date in my timezone.

line 4, will print current date in UTC

line 5, will print current date in time zone offset at +10 from UTC

line 6, will print current date in time zone offset at +05:30 from UTC

Lets look at the output

My Current TimeZone >> Asia/Calcutta
Is same as  >>+05:30
2021-08-06
2021-08-06
2021-08-07
2021-08-06

If you see, the date in time zone +10 is different than in UTC timezone or with offset +5:30. It is also possible to provide negative value of offset as well to the ZoneOffset method.

The “now” methods returns an object of type LocalDate. There are various other utility methods to find month, day of month, day of week, year etc.

Lets look at one useful method

System.out.println(LocalDate.now().get(ChronoField.DAY_OF_MONTH));
System.out.println(LocalDate.now().get(ChronoField.YEAR_OF_ERA));

So if you see above, one of the method accepts a class called TemporalField. ChronoField enum has various temporal fields that can be passed to this method. Output of the above is as below

6
2021

So day of month is 6 and year is 2021. If you pass any temporal field which the LocalDate does support then it will throw “UnsupportedTemporalTypeException”

So for a date object which does not have time, if you ask for temporalfield “ChronoField.AMPM_OF_DAY” then it will throw ” UnsupportedTemporalTypeException”

There are various other utility methods to add days, months, minutes to the current LocalDate object.

Lets look at how can you convert a simple string into a LocalDate object.

LocalDate.from(DateTimeFormatter.ofPattern("yyyy-MM-dd").parse("2021-01-01"));

Here I have used “DateTimeFormatter” to the utility class as formatter and passed the expected date string to the parse method of DateTimeFormatter. This will try to convert the string to the format and return us the LocalDate Object. It will throw Parsing exception if you do not provide correct string to the parse method.

I hope this will help you in some or other way. Thank you !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: