Java8 Adding Hours To LocalDateTime Not Working

前端 未结 2 1788
深忆病人
深忆病人 2021-02-18 12:51

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\"));
         


        
2条回答
  •  孤街浪徒
    2021-02-18 13:23

    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);
    ...
    

提交回复
热议问题