Skip to main content

UTC Dates

1 reply [Last post]
pedromarques
Offline
Joined: 2010-04-01

Hello.

I already search by this topic in the forum searh engine , and i have found a lot of issues related with this topic but none of these have clarified me.
I read that when you create a Date object like this away Date d = new Date(); , that will contain the date in UTC(number of miliseconds since 01/01/1970), but when i simple
do System.out.println(d) this will print the date in my local machine timezone.
How can I hava a Date object with the UTC time ?

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter__lawrey
Offline
Joined: 2005-11-01

> I read that when you create a Date object like this
> away Date d = new Date(); , that will contain the
> date in UTC(number of miliseconds since 01/01/1970),
This is correct.
> but when i simple
> do System.out.println(d) this will print the date in
> my local machine timezone.
This is also correct.
> How can I hava a Date object with the UTC time ?
Please read your first statement again.
Date contains the time in UTC.

Perhaps you meant to ask: how do I print a Date in UTC time instead of the default local timezone?

Use SimpleDateFormat with setTimezone().