Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JodaTime - how to get current time in UTC

Tags:

java

jodatime

I want to get the current time in UTC. What I do so far is following (just for testing purposes):

    DateTime dt = new DateTime();
    DateTimeZone tz = DateTimeZone.getDefault();
    LocalDateTime nowLocal = new LocalDateTime();
    DateTime nowUTC = nowLocal.toDateTime(DateTimeZone.UTC);

    Date d1 = nowLocal.toDate();
    Date d2 = nowUTC.toDate();

    L.d("tz: " + tz.toString());
    L.d("local: " + d1.toString());
    L.d("utc: " + d2.toString());
  • d1 is my local time, that's fine
  • d2 is my local time + 1, but should be local time - 1...

My local time zone is UTC+1 (according to the debug output and the list here: https://www.joda.org/joda-time/timezones.html)...

How do I correctly convert from one time zone to another (inclusive the millisecond representation)?

EDIT

I need the date/milliseconds... It's NOT about displaying the time correctly....

EDIT 2

Now, with the help of a comment and an answer, I tried following:

    DateTimeZone tz = DateTimeZone.getDefault();
    DateTime nowLocal = new DateTime();
    LocalDateTime nowUTC = nowLocal.withZone(DateTimeZone.UTC).toLocalDateTime();
    DateTime nowUTC2 = nowLocal.withZone(DateTimeZone.UTC);

    Date dLocal = nowLocal.toDate();
    Date dUTC = nowUTC.toDate();
    Date dUTC2 = nowUTC2.toDate();

    L.d(Temp.class, "------------------------");
    L.d(Temp.class, "tz    : " + tz.toString());
    L.d(Temp.class, "local : " + nowLocal +     " | " + dLocal.toString());
    L.d(Temp.class, "utc   : " + nowUTC +       " | " + dUTC.toString()); // <= WORKING SOLUTION
    L.d(Temp.class, "utc2  : " + nowUTC2 +      " | " + dUTC2.toString());

OUTPUT

tz    : Europe/Belgrade
local : 2015-01-02T15:31:38.241+01:00 | Fri Jan 02 15:31:38 MEZ 2015
utc   : 2015-01-02T14:31:38.241 | Fri Jan 02 14:31:38 MEZ 2015
utc2  : 2015-01-02T14:31:38.241Z | Fri Jan 02 15:31:38 MEZ 2015

What I wanted was, that the local date displays 15 o'clock and utc date displays 14 o'clock... For now, this seems to work...

----- EDIT3 - Final solution -----

Hopefully, this is a good solution... I think, i respects all tipps i got...

    DateTimeZone tz = DateTimeZone.getDefault();
    DateTime nowUTC = new DateTime(DateTimeZone.UTC);
    DateTime nowLocal = nowUTC.withZone(tz);

    // This will generate DIFFERENT Dates!!! As I want it!
    Date dLocal = nowLocal.toLocalDateTime().toDate();
    Date dUTC = nowUTC.toLocalDateTime().toDate();

    L.d("tz    : " + tz.toString());
    L.d("local : " + nowLocal +     " | " + dLocal.toString());
    L.d("utc   : " + nowUTC +       " | " + dUTC.toString());

Output:

tz    : Europe/Belgrade
local : 2015-01-03T21:15:35.170+01:00 | Sat Jan 03 21:15:35 MEZ 2015
utc   : 2015-01-03T20:15:35.170Z | Sat Jan 03 20:15:35 MEZ 2015
like image 520
prom85 Avatar asked Jan 02 '15 11:01

prom85


People also ask

How do I print the current time in UTC?

Getting the UTC timestampUse the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.

How do I find my UTC date?

The GETUTCDATE() function returns the current database system UTC date and time, in a 'YYYY-MM-DD hh:mm:ss.

How do you code UTC for time?

Current UTC Time - Java8 OffsetDateTime OffsetDateTime is an immutable representation of a date-time with an offset that is mainly used for storing date-time fields into the precision of nanoseconds. d1 = OffsetDateTime. now(ZoneOffset. UTC);

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 17:15:11Z.


3 Answers

You're making it far more complicated than you need to:

DateTime dt = new DateTime(DateTimeZone.UTC); 

No conversion required at all. If you find you actually need to convert, you can use withZone. I'd suggest you avoid going via LocalDateTime, however, as that way you can lose information due to time zone transitions (two different instants can have the same local time in the same time zone, because clocks go back and repeat local time.

Having said all of this, for the sake of testability I personally like using a Clock interface which allows me to get the current time (e.g. as an Instant). You can then use dependency injection to inject a real system clock when running in production, and a fake clock with a preset time for tests. Java 8's java.time package has this idea built into it, btw.

like image 55
Jon Skeet Avatar answered Sep 24 '22 13:09

Jon Skeet


You can also use the static method now which makes it even more readable

DateTime.now(DateTimeZone.UTC)
like image 43
Kobynet Avatar answered Sep 22 '22 13:09

Kobynet


Use this

DateTime.now().withZone(DateTimeZone.UTC)

and if you want to format, you can use

DateTime.now().withZone(DateTimeZone.UTC).toString("yyyyMMddHHmmss")
like image 35
Naidu Avatar answered Sep 22 '22 13:09

Naidu