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

12
记录是按照美国的时区保存的,但如果我想将相同的记录显示给用户,它应该将服务器日期时间与(美国时区)转换为用户的日期时间与用户的时区。

2
喂!Java 和 JavaScript 是完全不同的东西! - Pangolin
5个回答

13
如果你在谷歌上搜索“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

Javascript是一种用于Web开发的编程语言,用于在网页上创建动态效果和交互行为。它可以通过转换本地时间来实现跨时区计算,并且还有许多其他便捷的功能。

// 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'));

12

java.time

旧的日期时间类设计不良,令人困惑和麻烦。 要避免使用它们。请使用现代类:内置于Java 8及更高版本中的java.time框架。可以找到适用于早期Java 6&7 的后移版本 适用于Android的版本 Instant 是在 UTC时区时间线上的一个瞬间。
Instant now = Instant.now();

应用时区 (ZoneId) 以获取 ZonedDateTime

永远不要使用三到四个字母的时区缩写,例如 ESTIST。它们既没有标准化也不唯一!请使用 正确的时区名称,采用 大洲/地区 格式构建,例如 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
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);

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");
}

问题是关于Java的。 - Saikat

0

获取柏林时间并将其转换为协调世界时的代码

Calendar sc = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
            String strt = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");    
            sf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));    
            sc.set(sc.get(Calendar.YEAR),sc.get(Calendar.MONTH),  sc.get(Calendar.DATE),sc.get(Calendar.HOUR) , sc.get(Calendar.MINUTE));
            strt = sf.format(sc.getTime());
            System.out.println("Start :"+strt);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接