2011-11-23 10 views
8

रिकॉर्ड अमेरिका के समय क्षेत्र के अनुसार सहेजे जा रहे हैं, लेकिन यदि मैं एक ही रिकॉर्ड को उपयोगकर्ता को वापस दिखाना चाहता हूं तो इसे सर्वर दिनांक समय (यूएस टाइम जोन) उपयोगकर्ता के समय क्षेत्र के साथ उपयोगकर्ता के दिनांक समयदिनांक समय को एक समय क्षेत्र से दूसरे समय क्षेत्र में कनवर्ट करने के लिए कैसे करें

+2

Oi में कन्वर्ट करने के लिए! जावा और जावास्क्रिप्ट दो पूरी तरह से अलग सामान हैं! – LouwHopley

उत्तर

12

यदि आप Google "जावा दिनांक परिवर्तन समय क्षेत्र" या "जावास्क्रिप्ट दिनांक परिवर्तन समय क्षेत्र" में टाइप करते हैं।

जावा में (स्रोत:: 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 जावा 8 और बाद में निर्मित ढांचे का उपयोग करें। बैक-पोर्ट for earlier Java 6 & 7 और for Android खोजें।

InstantUTC में समयरेखा पर एक पल है।

Instant now = Instant.now(); 

एक समय क्षेत्र (ZoneId) एक ZonedDateTime प्राप्त करने के लिए लागू करें।

कभी भी 0-या IST जैसे 3-4 अक्षर क्षेत्र संक्षेपों का उपयोग न करें। वे न तो मानकीकृत हैं और न ही अद्वितीय (!)। proper time zone names का उपयोग करें, continent/region प्रारूप में Asia/Kolkata, Pacific/Auckland, America/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(); 
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); 
संबंधित मुद्दे