Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda Time - different between timezones

I want to convert the current time to the time in a specific timezone with Joda time.

Is there a way to convert DateTime time = new DateTime() to a specific timezone, or perhaps to get the number of hours difference between time.getZone() and another DateTimeZone to then do time.minusHours or time.plusHours?

like image 345
Michael Avatar asked Jul 24 '12 11:07

Michael


People also ask

Does Joda datetime have time zones?

An interval in Joda-Time represents an interval of time from one instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone.

Is Joda deprecated?

Cause joda-time is deprecated.

What is Joda-Time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.

Is Joda-Time followed in Java 8?

Joda-Time provides a quality replacement for the Java date and time classes. Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java. time (JSR-310).


3 Answers

Check out DateTimeZone & Interval:

DateTime dt = new DateTime();
    // translate to London local time
    DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));

Interval:

Interval interval = new Interval(start, end); //start and end are two DateTimes
like image 137
ali haider Avatar answered Sep 17 '22 15:09

ali haider


java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // ZonedDateTime.now() is same as ZonedDateTime.now(ZoneId.systemDefault()). In
        // order to specify a specific timezone, use ZoneId.of(...) e.g.
        // ZonedDateTime.now(ZoneId.of("Europe/London"));
        ZonedDateTime zdtDefaultTz = ZonedDateTime.now();
        System.out.println(zdtDefaultTz);

        // Convert zdtDefaultTz to a ZonedDateTime in another timezone e.g.
        // to ZoneId.of("America/New_York")
        ZonedDateTime zdtNewYork = zdtDefaultTz.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println(zdtNewYork);
    }
}

Output from a sample run:

2021-07-25T15:48:10.584414+01:00[Europe/London]
2021-07-25T10:48:10.584414-04:00[America/New_York]

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

like image 33
Arvind Kumar Avinash Avatar answered Sep 18 '22 15:09

Arvind Kumar Avinash


I want to convert the current time to the time in a specific timezone with Joda time.

It's not really clear whether you've already got the current time or not. If you've already got it, you can use withZone:

DateTime zoned = original.withZone(zone);

If you're just fetching the current time, use the appropriate constructor:

DateTime zoned = new DateTime(zone);

or use DateTime.now:

DateTime zoned = DateTime.now(zone);
like image 41
Jon Skeet Avatar answered Sep 19 '22 15:09

Jon Skeet