Amit Kumar
Amit Kumar

Reputation: 417

Get same date in different timezone

I have a epochmilli sec time. Created a date object from this epoch time.

Date date = new Date(epochMilli);

Suppose date is "23 Nov 2019 00:00:00"

Now, I just want to get this same date in different timezones like :]

Japan time : 23 Nov 2019 00:00:00

US time : 23 Nov 2019 00:00:00

I am currently using LocalDateTime or ZonedDateTime.

But when I am converting into different zone, time also changes. But I don't want this time to change.

Thanks in Advance.

Upvotes: 0

Views: 2260

Answers (2)

deHaar
deHaar

Reputation: 18568

Unfortunately, you didn't show us how you used ZonedDateTime, so the following examples may cover more than you just wanted by showing how to parse millis, convert the resulting date time from one zone to others and how to parse a date time using different zones:

public static void main(String[] args) {

    long epochMillis = 1574208000000L;
    
    // define a formatter to be used
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss",
                                                        Locale.ENGLISH);

    // create an instant from the milliseconds
    Instant instant = Instant.ofEpochMilli(epochMillis);
    // and create some example time zones for later use
    ZoneId utc = ZoneId.of("UTC");
    ZoneId tokyo = ZoneId.of("Asia/Tokyo");
    ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
    ZoneId chicago = ZoneId.of("America/Chicago");
    
    /*
     * Part 1: Getting the date time converted to different time zones
     */
    
    // use the instant to create a ZonedDateTime at a specific time zone, here: UTC
    ZonedDateTime utcZdt = instant.atZone(utc);

    // then take the UTC-ZonedDateTime as base for conversion to other time zones 
    ZonedDateTime asiaTokyoConvertedfromUtc = utcZdt.withZoneSameInstant(tokyo);
    ZonedDateTime americaLosAngelesConvertedfromUtc = utcZdt.withZoneSameInstant(losAngeles);
    ZonedDateTime americaChicagoConvertedfromUtc = utcZdt.withZoneSameInstant(chicago);

    // print the results
    System.out.println("#### 1574208000000L at UTC, converted to other zones ####");
    System.out.println("UTC time zone:\t\t\t\t" + utcZdt.format(dtf));
    System.out.println("JST (Japan/Tokyo) time zone:\t\t"
                        + asiaTokyoConvertedfromUtc.format(dtf));
    System.out.println("PST (USA/Los Angeles) time zone:\t"
                        + americaLosAngelesConvertedfromUtc.format(dtf));
    System.out.println("CST (USA/Chicago) time zone:\t\t"
                        + americaChicagoConvertedfromUtc.format(dtf));
    
    System.out.println();

    /*
     * Part 2: Getting the date time in different time zones
     */
    
    // use the instant to create a ZonedDateTime at Asia/Tokyo
    ZonedDateTime asiaTokyoFromMillis = instant.atZone(tokyo);

    // use the instant to create a ZonedDateTime at America/Los Angeles
    ZonedDateTime americaLosAngelesFromMillis = instant.atZone(losAngeles);

    // use the instant to create a ZonedDateTime at America/Chicago
    ZonedDateTime americaChicagoFromMillis = instant.atZone(chicago);

    // print the (expected) results, same as converted date times...
    System.out.println("#### 1574208000000L at different zones ####");
    System.out.println("UTC time zone:\t\t\t\t" + utcZdt.format(dtf));
    System.out.println("JST (Asia/Tokyo) time zone:\t\t"
                        + asiaTokyoFromMillis.format(dtf));
    System.out.println("PST (USA/Los Angeles) time zone:\t"
                        + americaLosAngelesFromMillis.format(dtf));
    System.out.println("CST (USA/Chicago) time zone:\t\t"
                        + americaChicagoFromMillis.format(dtf));
    
    System.out.println();
    
    /*
     * Part 3: How to parse the date time instead of millis
     */
    
    // provide a parseable date time String
    String dateTime = "23 Nov 2019 00:00:00";
    
    // parse it in each desired time zone
    ZonedDateTime utc23Nov2019 = LocalDateTime.parse(dateTime, dtf).atZone(utc);
    ZonedDateTime asiaTokyo23Nov2019 = LocalDateTime.parse(dateTime, dtf)
                                                    .atZone(tokyo);
    ZonedDateTime americaChicago23Nov2019 = LocalDateTime.parse(dateTime, dtf)
                                                         .atZone(losAngeles);
    ZonedDateTime americaLosAngeles23Nov2019 = LocalDateTime.parse(dateTime, dtf)
                                                            .atZone(chicago);

    // print the results, now you have the 23. Nov 2019 at 00:00:00 at each time zone
    System.out.println("#### \"23 Nov 2019 00:00:00\" at different zones ####");
    System.out.println("UTC time zone:\t\t\t\t" + utc23Nov2019.format(dtf));
    System.out.println("JST (Asia/Tokyo) time zone:\t\t"
                        + asiaTokyo23Nov2019.format(dtf));
    System.out.println("PST (USA/Los Angeles) time zone:\t"
                        + americaChicago23Nov2019.format(dtf));
    System.out.println("CST (USA/Chicago) time zone:\t\t"
                        + americaLosAngeles23Nov2019.format(dtf));
}

The output of this is

#### 1574208000000L at UTC, converted to other zones ####
UTC time zone:                      20 Nov 2019 00:00:00
JST (Japan/Tokyo) time zone:        20 Nov 2019 09:00:00
PST (USA/Los Angeles) time zone:    19 Nov 2019 16:00:00
CST (USA/Chicago) time zone:        19 Nov 2019 18:00:00

#### 1574208000000L at different zones ####
UTC time zone:                      20 Nov 2019 00:00:00
JST (Asia/Tokyo) time zone:         20 Nov 2019 09:00:00
PST (USA/Los Angeles) time zone:    19 Nov 2019 16:00:00
CST (USA/Chicago) time zone:        19 Nov 2019 18:00:00

#### "23 Nov 2019 00:00:00" at different zones ####
UTC time zone:                      23 Nov 2019 00:00:00
JST (Asia/Tokyo) time zone:         23 Nov 2019 00:00:00
PST (USA/Los Angeles) time zone:    23 Nov 2019 00:00:00
CST (USA/Chicago) time zone:        23 Nov 2019 00:00:00

Upvotes: 7

Jochen Reinhardt
Jochen Reinhardt

Reputation: 843

The legacy java.util.Date is not time-zone-aware. It is basically a point in time measured in milliseconds since 1970-1-1 0:00. Nothing else.

When a date is print, it is formatted using your system's default locale and time zone. In order to get a java.util.Date object formatted for a different time zone you have to configure your DateFormat accordingly.

Date pointInTime = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance();
dateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(dateFormat.format(pointInTime));
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormat.format(pointInTime));

With the newer LocalDate API, things are different. The previous answer gives a rich example.

Upvotes: 1

Related Questions