簡體   English   中英

如何將日期時間從一個時區轉換為另一個時區

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

記錄將根據美國的時區保存,但如果我想向用戶顯示相同的記錄,則應將服務器日期時間(美國時區)轉換為用戶的時區的用戶日期時間

如果您輸入谷歌“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'));

java.time

舊的日期時間類設計糟糕,令人困惑,麻煩。 避免他們。

使用現代類:Java 8及更高版本中內置的java.time框架。 查找早期Java 6和7以及Android的后端口。

InstantUTC時間軸上的一個時刻。

Instant now = Instant.now();

應用時區( ZoneId )以獲取ZonedDateTime

切勿使用3-4字母區域縮寫,例如ESTIST 它們既不標准也不獨特(!)。 使用適合的時區名稱 ,以Asia/KolkataPacific/AucklandAmerica/Los_Angelescontinent/region格式建立。

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

代碼獲取柏林時間並將其轉換為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