如何将日期时间从一个时区转换为另一个时区

时间:2011-11-23 07:39:06

标签: java datetime timezone

记录是根据美国时区保存的,但如果我想向用户显示相同记录,则应将服务器日期时间转换为(美国时区)至用户日期时间与用户时区

5 个答案:

答案 0 :(得分:12)

如果您输入谷歌“Java日期更改时区”或“Javascript日期更改时区”。您将得到一个结果:

在Java中(来源: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(来源: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'));

答案 1 :(得分:8)

java.time

旧的日期时间类设计糟糕,令人困惑且麻烦。避免它们。

使用现代类:Java 8及更高版本中内置的java.time框架。找到后端for earlier Java 6 & 7for Android

InstantUTC中时间轴上的一个时刻。

Instant now = Instant.now();

应用时区(ZoneId)以获得ZonedDateTime

切勿使用3-4个字母区域缩写,例如ESTIST。它们既不标准也不独特(!)。使用proper time zone names格式,continent/region格式,例如Asia/KolkataPacific/AucklandAmerica/Los_Angeles

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

应用其他时区生成另一个ZonedDateTime调整到该时区。致电withZoneSameInstant

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

如果您想返回UTC,请提出Instant

Instant instant = zdt_Paris.toInstant(); 

答案 2 :(得分:2)

TimeZone fromTimezone =TimeZone.getTimeZone(from);
TimeZone toTimezone=TimeZone.getTimeZone(to);

long fromOffset = fromTimezone.getOffset(calendar.getTimeInMillis());
long toOffset = toTimezone.getOffset(calendar.getTimeInMillis());

long convertedTime = calendar.getTimeInMillis() - (fromOffset - toOffset);

答案 3 :(得分:0)

//Convert date from one zone to another
/* 
$zone_from='Asia/Kolkata';

$zone_to='America/Phoenix';

date_default_timezone_set($zone_from);

$convert_date="2016-02-26 10:35:00";

echo $finalDate=zone_conversion_date($convert_date, $zone_from, $zone_to);

*/

function zone_conversion_date($time, $cur_zone, $req_zone)
{   
    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d H:i:s");

    date_default_timezone_set($cur_zone);
    $local = date("Y-m-d H:i:s");

    date_default_timezone_set($req_zone);
    $required = date("Y-m-d H:i:s");

    /* return $required; */
    $diff1 = (strtotime($gmt) - strtotime($local));
    $diff2 = (strtotime($required) - strtotime($gmt));

    $date = new DateTime($time);
    $date->modify("+$diff1 seconds");
    $date->modify("+$diff2 seconds");

    return $timestamp = $date->format("Y-m-d H:i:s");
}

答案 4 :(得分:0)

代码获取柏林时间并将其转换为UTC时间

[Tasks]
Name: "desktopicon"; ... ; Check: not WizardSilent