My question might be trivial but I’m just looking for clarifications. I read somewhere in SO that Java’s Date() is actually always in UTC time, how come when I create a Date() object and print it using toString(), it displays the local time. If this is not the right way to print it, what should it be so I would get the UTC time?
My question might be trivial but I’m just looking for clarifications. I read somewhere
Share
For formatting, you should really use a
DateFormatimplementation (e.g.SimpleDateFormat). That will let you specify the time zone (and output format).Dateitself has no concept of time zones – it represents an instant in time, using the “milliseconds since the Unix epoch” as storage. ThetoString()method just converts whatever instant is represented into a local time using the system default time zone.Personally I’d advise moving away from the built-in date/time API entirely and using Joda Time as a far more sensible library.