JodaTime - how to get current time in UTC

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

DateTime.now(DateTimeZone.UTC)

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.

Tags:

Java

Jodatime