Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date time from one time zone to another time zone

The records are getting saved according to time zone of US but if I want to show the same record back to user it should convert the server date time with(US Time Zone) to user's date time with user's Time Zone

like image 365
ShaileshI Avatar asked Nov 23 '11 07:11

ShaileshI


People also ask

How do you convert time from one zone to another?

To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

Which function changes date from one timezone to another?

setTimeZone() method to convert the date time to the given timezone.


2 Answers

If you type in google "Java date change timezone" or "Javascript date change timezone". You will have one of your results:

In Java (source: http://www.coderanch.com/t/417443/java/java/Convert-Date-one-timezone-another )

Date date = new Date();  

DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");  
formatter.setTimeZone(TimeZone.getTimeZone("CET"));  

// Prints the date in the CET timezone  
System.out.println(formatter.format(date));  

// Set the formatter to use a different timezone  
formatter.setTimeZone(TimeZone.getTimeZone("IST"));  

// Prints the date in the IST timezone  
System.out.println(formatter.format(date));  

Javascript (source: http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329 )

// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {

    // create Date object for current location
    d = new Date();

    // convert to msec
    // add local time zone offset
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();

}

// get Bombay time
alert(calcTime('Bombay', '+5.5'));
like image 121
Niels Avatar answered Nov 02 '22 04:11

Niels


java.time

The old date-time classes are poorly designed, confusing, and troublesome. Avoid them.

Use modern classes: the java.time framework built into Java 8 and later. Find back-ports for earlier Java 6 & 7 and for Android.

An Instant is a moment on the timeline in UTC.

Instant now = Instant.now();

Apply a time zone (ZoneId) to get a ZonedDateTime.

Never use the 3-4 letter zone abbreviations such as EST or IST. They are neither standardized nor unique(!). Use proper time zone names, built in a continent/region format such as Asia/Kolkata, Pacific/Auckland, America/Los_Angeles.

ZoneId zoneId_Montreal = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt_Montreal = ZonedDateTime.ofInstant( instant , zoneId_Montreal );

Apply a different time zone to generate another ZonedDateTime adjusted to that time zone. Call withZoneSameInstant.

ZoneId zoneId_Paris = ZoneId.of( "Europe/Paris" ); // Or "Asia/Kolkata", etc.
ZonedDateTime zdt_Paris = zdt_Montreal.withZoneSameInstant( zoneId_Paris );

If you want to go back to UTC, ask for an Instant.

Instant instant = zdt_Paris.toInstant(); 
like image 28
Basil Bourque Avatar answered Nov 02 '22 06:11

Basil Bourque