1

I have been using Java 11 LocalDateTime but realised that it uses the time of the system. Are there alternatives that allow me to use the system time of a server or web time in my country?

Edit: After trying Instant I am very close to result I am after.

LocalDateTime ldt = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("Europe/London"));

The Time is now correct but the date is still picking the system date? If I change the date to Christmas and the timezone of my system to UTC +1 Amsterdam the time will be ZoneId.of("Europe/London") UTC 0 but the date still shows as christmas. Any ideas?

@Ole V.V. I do not believe this is a duplicate question? similar, yes! duplicate no? If you can link me to what you believe is a duplicate that would be great.

Tom
  • 21
  • 1
  • 7
  • 1
    Pretty close to this question: https://stackoverflow.com/questions/4442192/how-to-use-an-internet-time-server-to-get-the-time – fabian Nov 30 '18 at 12:19
  • 1
    I don't believe LocalDateTime uses the system's time (as in, it uses the "local" system time without any information). It simply represents time without any information of timezone etc. I have asked a related question here: https://stackoverflow.com/questions/39451432/confusion-with-java-time-parsing-utc – pandaadb Nov 30 '18 at 12:20
  • 1
    I just tested local date time and changed my system time it was getting that value and not the actual real date and time. I think the link from fabian has more or less helped me to work towards getting the results I want. – Tom Nov 30 '18 at 12:31
  • @pandaadb The asker probably means that `LocalDateTime.now()` uses the system clock to obtain the current time. If the system clock is wrong, you get the wrong time. – Ole V.V. Nov 30 '18 at 12:52
  • fair enough - i may have misunderstood this :) In that case I'd recommend `Instant` and using a specific timezone instead – pandaadb Nov 30 '18 at 15:38

0 Answers0