How to convert any TimeZone dateTime to UTC Date in JAVA

1.4k views Asked by At

I have to use the date time which I receive by user, and convert that time to UTC from the given time. Right now, all the solutions are working, but not as I needed. First, it converts to the time zone, but I don't want to initially convert. I have to set that timezone on my date and then convert that time to UTC. Right now, each and every solution shows that I have to pass date and time, and then I am getting updated date and time with the time zone which I provide, and then converts to the UTC. This was not my question.

My user will send me date time of any timezone, and my server will be running on UTC time zone. I will get the zone id from the front end. I want to convert that date time to UTC and verify with my other conditions. It might be possible that I will get UTC-7 time in one request, and then after in next request I will get UTC+5:30. So, all the time should be converted to the UTC and I am not able to set timezone on date.

I wanted to set the specific timezone to date, and I am getting the date and time of that timezone, but I'm not able to set the exact timezone. Here, I am adding my code please tell me where I am wrong:

Calendar laCalendar = Calendar.getInstance();
laCalendar.set(2020, 8, 14, 00, 00);
Date losAngelesDate = laCalendar.getTime();
System.out.println("LA Date1===>" + losAngelesDate);

SimpleDateFormat simpleTimeFormatForUSA = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
simpleTimeFormatForUSA.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String americaDateString = simpleTimeFormatForUSA.format(laCalendar.getTime());
SimpleDateFormat dateFormatUSA = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
losAngelesDate = dateFormatUSA.parse(americaDateString);

System.out.println("LA Date2===>" + losAngelesDate);
Date utcDate = losAngelesDate;
Calendar calendar = Calendar.getInstance();
calendar.setTime(utcDate);
TimeZone timeZone = TimeZone.getTimeZone("UTC");
SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
simpleTimeFormat.setTimeZone(timeZone);
String utcTime = simpleTimeFormat.format(calendar.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
utcDate = dateFormat.parse(utcTime);
System.out.println("UTC date====>" + utcDate);

And my output is

LA Date1===>Mon Sep 14 00:00:56 UTC 2020
LA Date2===>Sun Sep 13 17:00:00 UTC 2020
UTC date====>Sun Sep 13 17:00:00 UTC 2020

Here you can see that my DateTime is converted, but time zone is still UTC and I wanted to update that.

2

There are 2 answers

2
Spectric On

Before you convert the Calendar to a Date, set the Timezone to your preference.

For example:

import java.util.Calendar;
import java.util.TimeZone;

public class Timezone{

    public static void main(String []args){
       Calendar laCalendar = Calendar.getInstance();
       TimeZone timezone = TimeZone.getTimeZone("UTC");
       laCalendar.set(2020, 8, 14, 00, 00);
       laCalendar.setTimeZone(timezone);
       java.util.Date losAngelesDate = laCalendar.getTime();
       System.out.println(losAngelesDate.toString());
    }
}
2
Sanket Parikh On

Here, is how I would write it using Java 8 Date/Time APIs.

    public static void main(String[] args)
    {
        System.out.println("UTC");
        toTimeZone(new Date(), TimeZone.getTimeZone(ZoneId.of("UTC")));
        
        System.out.println("America/Los_Angeles");
        toTimeZone(new Date(), TimeZone.getTimeZone(ZoneId.of("America/Los_Angeles")));
    }

    public static void toTimeZone(Date date, TimeZone timeZone)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Instant epochMillisInstant = Instant.ofEpochMilli(calendar.getTimeInMillis());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(epochMillisInstant, ZoneId.systemDefault());
        ZonedDateTime dateTimeToConvert = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
        ZonedDateTime dateTimeWithTimeZone = dateTimeToConvert.withZoneSameInstant(timeZone.toZoneId());
        String formattedDateForNewTimeZone = dateTimeWithTimeZone.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a"));
        System.out.println(formattedDateForNewTimeZone);
    }

Using Just Java 8 Date/Time APIs - Shorter Version

public static void main(String[] args) {
        System.out.println("UTC");
        toTimeZone(LocalDateTime.now(),"UTC");
        System.out.println("America/Los_Angeles");
        toTimeZone(LocalDateTime.now(),"America/Los_Angeles");
    }

    private static void toTimeZone(LocalDateTime localDateTime, String timeZone) {
        ZonedDateTime localDateTimeToConvert = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
        ZonedDateTime zonedDateTime = localDateTimeToConvert.withZoneSameInstant(ZoneId.of(timeZone));
        String formattedDateForNewTimeZone = zonedDateTime.format(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a"));
        System.out.println(formattedDateForNewTimeZone);
    }