I tried like below, but in both cases, it is showing the same time. What am I 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 the same as "After 12 Hours"...
The documentation of
LocalDateTime
specifies the instance ofLocalDateTime
is immutable, for exampleplusHours
So, you create a new instance of
LocalDateTime
when you execute plus operation, you need to assign this value as follows:I hope it can be helpful for you.