简体   繁体   English

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

[英]How to convert date time from one time zone to another time zone

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

If you type in google "Java date change timezone" or "Javascript date change timezone". 如果您输入谷歌“Java日期更改时区”或“Javascript日期更改时区”。 You will have one of your results: 您将得到一个结果:

In Java (source: http://www.coderanch.com/t/417443/java/java/Convert-Date-one-timezone-another ) 在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 (source: http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329 ) 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'));

java.time 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. 使用现代类:Java 8及更高版本中内置的java.time框架。 Find back-ports for earlier Java 6 & 7 and for Android . 查找早期Java 6和7以及Android的后端口。

An Instant is a moment on the timeline in UTC . InstantUTC时间轴上的一个时刻。

Instant now = Instant.now();

Apply a time zone ( ZoneId ) to get a ZonedDateTime . 应用时区( ZoneId )以获取ZonedDateTime

Never use the 3-4 letter zone abbreviations such as EST or IST . 切勿使用3-4字母区域缩写,例如ESTIST 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 . 使用适合的时区名称 ,以Asia/KolkataPacific/AucklandAmerica/Los_Angelescontinent/region格式建立。

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. 应用不同的时区以生成调整到该时区的另一个ZonedDateTime Call withZoneSameInstant . 使用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 . 如果你想回到UTC ,请求一个Instant

Instant instant = zdt_Paris.toInstant(); 
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);
//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");
}

Code To Get Berlin Time and Convert it into UTC Time 代码获取柏林时间并将其转换为UTC时间

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM