Java LocalDateTime class is an immutable class that represents the date and time without the timezone information, such as 2017-10-25T11:20:55. In this guide we will see the methods of LocalDateTime class and will see few java programs of various methods of LocalDateTIme
class to understand how to use this class.
Java LocalDateTime class:
public final class LocalDateTime extends Object implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
Java LocalDateTime class – Method Summary
Let’s see the summary of some of the important methods of this class. There are multiple variants of each method and we will try to cover those variants in the examples.
Method | Description |
---|---|
ZonedDateTime atZone(ZoneId zone) | This method combines the date time with the timezone to create an instance of ZonedDateTime which represents the date & time with timezone information. |
int compareTo(LocalDateTime other) | It compares this date-time to another date-time. Returns negative if this date-time is before another date-time and returns positive if this date-time is after than another date-time. |
boolean equals(Object obj) | Checks if this date-time is equal to another date-time and returns true or false. |
static LocalDateTime now() | It returns current date-time from the System in default time-zone. |
static LocalDateTime of(LocalDate date, LocalTime time) | It returns an instance of LocalDateTime from the given date and time. |
int get(TemporalField field) | It returns the value of the specified field from this date-time. |
String format(DateTimeFormatter formatter) | Formats the LocalDateTime using the specified formatter. |
LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) | It returns an instance of LocalDateTime from the given year, month, day, hour and minute. Non-specified field such as second and nanosecond are set to zero. |
LocalDateTime plusHours(long hours) | Returns the copy of LocalDateTime after adding the specified hours to it. |
LocalDateTime minusHours(long hours) | Returns the copy of LocalDateTime after subtracting the specified hours from it. |
LocalDate toLocalDate() | Returns the LocalDate part of the LocalDateTime. |
Let’s start with a simple example of LocalDateTime.
1. Simple Example of LocalDateTime in Java
In this example, we are displaying the current date and time using LocalDateTime
class. We can also format the output of date and time using DateTimeFormatter
class which we have shown in the following example.
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Example { public static void main(String[] args) { //getting current date and time using now() method LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("Current Date and Time: "+currentDateTime); //displaying the current date time in different format DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm a"); String newFormat = currentDateTime.format(format); System.out.println("Current Date Time in specified format: "+newFormat); } }
Output:
Current Date and Time: 2017-10-31T13:57:06.290 Current Date Time in specified format: 31/10/2017 13:57 PM
2. Java – LocalDateTime of() method example
We can create the instance of LocalDateTime
from the given date and time information by using of() method of LocalDateTime
class.
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Example { public static void main(String[] args) { /* given date time info is passed in of() method * the sequence of parameters are: Year, Month, Day, Hour * , Minutes, Seconds, NanoSeconds */ LocalDateTime localDateTime = LocalDateTime.of(2017, 10, 21, 11, 32, 55, 678); System.out.println("Given Date Time in default format: "+localDateTime); //formatting the output DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm a"); String newFormat = localDateTime.format(format); System.out.println("Given Date Time in new format: "+newFormat); } }
Output:
Given Date Time in default format: 2017-10-21T11:32:55.000000678 Given Date Time in new format: 21/10/2017 11:32 AM
3. Java – LocalDateTime get Methods Example
There are several get methods available in this class. We will first see a brief description about these methods and then we will take an example to understand how these methods work.
int getYear() – Returns year
int getDayOfYear() – Returns day of year as integer value, from 1 to 365, or 366 in a leap year
Month getMonth() – Returns month.
int getDayOfMonth() – Returns day of the month as integer value, from 1 to 31.
DayOfWeek getDayOfWeek() – Returns day of the week.
int getHour() – Returns hour of the day, from 0 to 23
int getMinute() – Returns minute of the hour, from 0 to 59
int getSecond() – Returns second of the minute, from 0 to 59
int getNano() – Returns nanosecond, from 0 to 999,999,999
import java.time.LocalDateTime; public class Example { public static void main(String[] args) { /* given date time info is passed in of() method * the sequence of parameters are: Year, Month, Day, Hour * , Minutes, Seconds, NanoSeconds */ LocalDateTime localDateTime = LocalDateTime.of(2017, 10, 21, 11, 32, 55, 678); System.out.println("Given Date Time: "+localDateTime); System.out.println("getYear(): "+localDateTime.getYear()); System.out.println("getDayOfYear(): "+localDateTime.getDayOfYear()); System.out.println("getMonth(): "+localDateTime.getMonth()); System.out.println("getDayOfMonth(): "+localDateTime.getDayOfMonth()); System.out.println("getDayOfWeek(): "+localDateTime.getDayOfWeek()); System.out.println("getHour(): "+localDateTime.getHour()); System.out.println("getMinute(): "+localDateTime.getMinute()); System.out.println("getSecond(): "+localDateTime.getSecond()); System.out.println("getNano(): "+localDateTime.getNano()); } }
Output:
Given Date Time: 2017-10-21T11:32:55.000000678 getYear(): 2017 getDayOfYear(): 294 getMonth(): OCTOBER getDayOfMonth(): 21 getDayOfWeek(): SATURDAY getHour(): 11 getMinute(): 32 getSecond(): 55 getNano(): 678
4. Java – LocalDateTime plus Methods
plusYears() – Returns the copy of this LocalDateTime with the specified years added.
plusMonths() – Returns the copy of this LocalDateTime with the specified months added.
plusDays() – Returns the copy of this LocalDateTime with the specified days added.
plusHours() – Returns the copy of this LocalDateTime with the specified hours added.
plusMinutes() – Returns the copy of this LocalDateTime with the specified minutes added.
plusSeconds() – Returns the copy of this LocalDateTime with the specified seconds added.
plusNanos() – Returns the copy of this LocalDateTime with the specified nanoseconds added.
import java.time.LocalDateTime; public class Example { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("Current Date Time: "+localDateTime); System.out.println("Added 2 Years: "+localDateTime.plusYears(2)); System.out.println("Added 1 Month: "+localDateTime.plusMonths(1)); System.out.println("Added 5 Days: "+localDateTime.plusDays(5)); System.out.println("Added 7 Hours: "+localDateTime.plusHours(7)); System.out.println("Added 15 Minutes: "+localDateTime.plusMinutes(15)); System.out.println("Added 10 Seconds: "+localDateTime.plusSeconds(10)); System.out.println("Added 1000 nano Seconds: "+localDateTime.plusNanos(1000)); } }
Output:
Current Date Time: 2017-10-31T14:24:23.233 Added 2 Years: 2019-10-31T14:24:23.233 Added 1 Month: 2017-11-30T14:24:23.233 Added 5 Days: 2017-11-05T14:24:23.233 Added 7 Hours: 2017-10-31T21:24:23.233 Added 15 Minutes: 2017-10-31T14:39:23.233 Added 10 Seconds: 2017-10-31T14:24:33.233 Added 1000 nano Seconds: 2017-10-31T14:24:23.233001
5. Java LocalDateTime calculation using minus Methods
These methods returns an altered copy of LocalDateTime
by subtracting the specified amount from the LocalDateTime
. This means that the actual instance of LocalDateTime doesn’t get changed, rather a new copy of it is returned with the altered value.minusYears()
minusMonths()
minusDays()
minusHours()
minusMinutes()
minusSeconds()
minusNanos()
import java.time.LocalDateTime; public class Example { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("Current Date Time: "+localDateTime); System.out.println("Subtracted 3 Years: "+localDateTime.minusYears(3)); System.out.println("Subtracted 2 Month: "+localDateTime.minusMonths(2)); System.out.println("Subtracted 3 Days: "+localDateTime.minusDays(3)); System.out.println("Subtracted 6 Hours: "+localDateTime.minusHours(6)); System.out.println("Subtracted 30 Minutes: "+localDateTime.minusMinutes(30)); System.out.println("Subtracted 15 Seconds: "+localDateTime.minusSeconds(15)); System.out.println("Subtracted 2000 nano Seconds: "+localDateTime.minusNanos(2000)); } }
Output:
Current Date Time: 2017-10-31T14:31:04.695 Subtracted 3 Years: 2014-10-31T14:31:04.695 Subtracted 2 Month: 2017-08-31T14:31:04.695 Subtracted 3 Days: 2017-10-28T14:31:04.695 Subtracted 6 Hours: 2017-10-31T08:31:04.695 Subtracted 30 Minutes: 2017-10-31T14:01:04.695 Subtracted 15 Seconds: 2017-10-31T14:30:49.695 Subtracted 2000 nano Seconds: 2017-10-31T14:31:04.694998
6. Java LocalDateTime Example – isBefore(), isAfter() and isEqual() methods
The methods isBefore()
and isAfter()
check if the date-time is before or after the specified date-time. Method isEqual()
checks if this date-time equals to another date-time.
import java.time.LocalDateTime; public class Example { public static void main(String[] args) { //current date time LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("Current Date Time: "+currentDateTime); //given date time LocalDateTime localDateTime = LocalDateTime.of(2017, 9, 30, 11, 20, 45, 999); System.out.println("Given Date Time: "+localDateTime); System.out.println("Is current date/time before given date/time? "+ currentDateTime.isBefore(localDateTime)); System.out.println("Is current date/time after given date/time? "+ currentDateTime.isAfter(localDateTime)); System.out.println("Is current date/time equal to given date/time? "+ currentDateTime.isEqual(localDateTime)); } }
Output:
Current Date Time: 2017-10-31T14:37:18.128 Given Date Time: 2017-09-30T11:20:45.000000999 Is current date/time before given date/time? false Is current date/time after given date/time? true Is current date/time equal to given date/time? false
7. LocalDateTime with() Methods Example
The with methods such as withYear()
, withMonth()
etc. returns a copy of LocalDateTime
after replacing a particular value of date or time (based on the method) with the specified value. For example withYear(2016)
would replace the year in the date-time with the 2016
and the remaining fields in the date-time will remain unchanged.
import java.time.LocalDateTime; public class Example { public static void main(String[] args) { //current date time LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("Current Date Time: "+currentDateTime); LocalDateTime localDateTime2 = currentDateTime.withYear(2016); System.out.println("Year Changed: "+localDateTime2); LocalDateTime localDateTime3 = currentDateTime.withHour(3); System.out.println("Hour Changed: "+localDateTime3); } }
Output:
Current Date Time: 2017-10-31T14:42:13.319 Year Changed: 2016-10-31T14:42:13.319 Hour Changed: 2017-10-31T03:42:13.319
In the above example, we can see the working of withYear()
and withHour()
methods. Similarly the other methods such as withMonth()
, withMinute()
, withSecond
(), withNano()
etc. replaces the month, minute, second and nanosecond in the date-time respectively.
Leave a Reply