Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 Adding Hours To LocalDateTime Not Working

I tried like below, but in both the cases it is showing same time? What i am doing wrong.

    LocalDateTime currentTime = LocalDateTime.now(ZoneId.of("UTC"));     Instant instant = currentTime.toInstant(ZoneOffset.UTC);     Date currentDate = Date.from(instant);     System.out.println("Current Date = " + currentDate);     currentTime.plusHours(12);     Instant instant2 = currentTime.toInstant(ZoneOffset.UTC);     Date expiryDate = Date.from(instant2);     System.out.println("After 12 Hours = " + expiryDate); 

"Current Date" Time is showing Same as "After 12 Hours"...

like image 828
Rajkishan Swami Avatar asked Jun 23 '15 13:06

Rajkishan Swami


People also ask

How do I add hours to LocalDateTime?

Adding hours to localdatetime in Java If you have a date in String then first parse it using the parse() method to get the localdatetime object. After that use the plusHours() method to add hours to it.

How do I get hours from LocalDateTime?

The getHour() method of an LocalDateTime class is used to return the hour-of-day field. This method returns an integer value ranging from 0 to 23, i.e. the Hours of a day.

Is Java time LocalDateTime immutable?

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second.

Should I use LocalDateTime or instant?

Instant and LocalDateTime are two entirely different animals: One represents a moment, the other does not. Instant represents a moment, a specific point in the timeline. LocalDateTime represents a date and a time-of-day. But lacking a time zone or offset-from-UTC, this class cannot represent a moment.


2 Answers

The documentation of LocalDateTime specifies the instance of LocalDateTime is immutable, for example plusHours

public LocalDateTime plusHours(long hours)

Returns a copy of this LocalDateTime with the specified number of hours added.

This instance is immutable and unaffected by this method call.

Parameters:
hours - the hours to add, may be negative
Returns:
a LocalDateTime based on this date-time with the hours added, not null
Throws:
DateTimeException - if the result exceeds the supported date range

So, you create a new instance of LocalDateTime when you execute plus operation, you need to assign this value as follows:

LocalDateTime nextTime = currentTime.plusHours(12); Instant instant2 = nextTime.toInstant(ZoneOffset.UTC); Date expiryDate = Date.from(instant2); System.out.println("After 12 Hours = " + expiryDate); 

I hope it can be helpful for you.

like image 110
lavenderx Avatar answered Oct 13 '22 09:10

lavenderx


From the java.time package Javadoc (emphasis mine):

The classes defined here represent the principal date-time concepts, including instants, durations, dates, times, time-zones and periods. They are based on the ISO calendar system, which is the de facto world calendar following the proleptic Gregorian rules. All the classes are immutable and thread-safe.

Since every class in the java.time package is immutable, you need to capture the result:

LocalDateTime after = currentTime.plusHours(12); ... 
like image 41
mkobit Avatar answered Oct 13 '22 10:10

mkobit