2011-11-23 2 views

답변

12

Google에 "Java date change timezone"또는 "Javascript date change timezone"을 입력하는 경우.

자바에서

(출처 : 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)); 

자바 스크립트 (출처 : 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')); 
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"); 
} 
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); 
7

java.time

오래된 날짜 - 시간 클래스는 잘못 설계되고 혼란스럽고 귀찮습니다. 그들을 피하십시오.

현대 클래스를 사용하십시오 : java.time 프레임 워크가 Java 8 이상에 내장되어 있습니다. 다시 포트 for earlier Java 6 & 7for Android을 찾습니다.

Instant은 타임 라인상의 한 순간입니다 (UTC).

Instant now = Instant.now(); 

ZonedDateTime 얻을 수있는 시간대 ( ZoneId)를 적용합니다.

EST 또는 IST과 같은 3-4 자의 영역 약어를 절대로 사용하지 마십시오. 그들은 표준화되지도 않았고 독특하지도 않습니다 (!). Asia/Kolkata, Pacific/Auckland, America/Los_Angeles과 같은 continent/region 형식으로 작성된 proper time zone names을 사용하십시오.

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

코드 베를린 시간을 얻고 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); 
관련 문제