-2

Suppose I change the system date and time. When I use Date or Calendar objects in Java, I get the changed/modified time which is not correct.

I need the exact local time, not the modified time.

long millis = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");         
Date date = new Date(millis);
System.out.println(dateFormat.format(date));
aUserHimself
  • 1,589
  • 2
  • 17
  • 26
Sudeep
  • 153
  • 1
  • 1
  • 12
  • 3
    Then don't modify the time. – Kayaman Sep 20 '17 at 10:54
  • Download it from a server as done in this question: https://stackoverflow.com/questions/7081262/getting-time-from-public-time-server-using-timetcpclient – Francesco Serra Sep 20 '17 at 11:01
  • @FrancescoSerra, this works fine with specific servers. i think servers should support NTP to get the above code working. Whereas i have a server, gave that id but i'm not getting anything in return. What should i do to get time from my server ? – Sudeep Sep 21 '17 at 07:05
  • @Sudeep I don't know how your server is done! I think you have to implement a service on your server and then consume it from your java application. – Francesco Serra Sep 21 '17 at 10:15

1 Answers1

0

Well, I suppose you could query some remote server/service to get the correct time and then convert it to your local zone (if needed).

But since System.currentTimeMillis() uses the system's date and time, what you get when using this piece of code is correct according to the specifications. Read this SO post for a more detailed explanation on how it works internally.

aUserHimself
  • 1,589
  • 2
  • 17
  • 26