Tuesday, 26 July 2022

Convert date or time from one time zone to other

Below snippet convert the Date instance from one time zone to other.

public static final String convert(Date date, TimeZone timeZone) {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    format.setTimeZone(timeZone);
    return format.format(date);
}

Below snippet convert the Instant from one time zone to other.

public static final String convert(Instant instant, ZoneId zoneId) {
    ZonedDateTime zonedDateTime = instant.atZone(zoneId);
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    return zonedDateTime.format(dateTimeFormatter);
}

Find the below working application.

 


ZoneUtil.java

package com.sample.app.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

public class ZoneUtil {

    public static final String convert(Date date, TimeZone timeZone) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        format.setTimeZone(timeZone);
        return format.format(date);
    }

    public static final String convert(Instant instant, ZoneId zoneId) {
        ZonedDateTime zonedDateTime = instant.atZone(zoneId);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        return zonedDateTime.format(dateTimeFormatter);
    }
}

App.java

package com.sample.app;

import java.time.Instant;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

import com.sample.app.util.ZoneUtil;

public class App {

    public static void main(String[] args) {
        TimeZone defaultTimeZone = TimeZone.getDefault();
        System.out.println("defaultTimeZone : " + defaultTimeZone);

        Date date = new Date();
        Instant instant = Instant.now();

        String dateInUS = ZoneUtil.convert(date, TimeZone.getTimeZone("America/Los_Angeles"));
        String instantInUS = ZoneUtil.convert(instant, ZoneId.of("America/Los_Angeles"));

        System.out.println("\nTime in " + defaultTimeZone);
        System.out.println("date : " + date);
        System.out.println("instant : " + instant);

        System.out.println("\nTime in " + TimeZone.getTimeZone("America/Los_Angeles"));
        System.out.println("dateInUS : " + dateInUS);
        System.out.println("instantInUS : " + instantInUS);
    }
}

Sample Output

defaultTimeZone : sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]

Time in sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
date : Tue Jul 26 14:35:16 IST 2022
instant : 2022-07-26T09:05:16.590Z

Time in sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
dateInUS : 2022-07-26T02:05:16.589-0700
instantInUS : 2022-07-26T02:05:16.590-0700


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment