2013-11-22 109 views
1

我有一個變量str(字符串類型),其值爲「28-Nov-2013 09:15 AM」。如何將其轉換爲UTC格式(上述時間在str變量是在PST,因此UTC應在8小時不止於此)。我現在用下面的柔性2.Find被下面的代碼這是不工作: -將PST中的時間轉換爲UTC格式

txtDate.text= formatDateUTC(txtDate.text); //here txtDate.text=28-Nov-2013 09:15 AM 

    private function formatDateUTC(originalDate:String):String 
    { 
     Alert.show('original '+originalDate); 
     var dtValue:Date = new Date(Date.parse(originalDate.replace("-"," "))); 
     var editedDate:String=pstFormatter.format(dtValue); 
     Alert.show('edited '+editedDate); 


     return (dateFormatter.format(dateAdd("hours",8,dtValue))).toString(); 

    } 
    private function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date 
      { 
     if (date == null) { 
      date = new Date(); 
     } 

     var returnDate:Date = new Date(date);; 

     switch (datepart.toLowerCase()) { 
      case "fullyear": 
      case "month": 
      case "date": 
      case "hours": 
      case "minutes": 
      case "seconds": 
      case "milliseconds": 
       returnDate[datepart] += number; 
       break; 
      default: 
       /* Unknown date part, do nothing. */ 
       break; 
     } 
     return returnDate; 
    } 

回答

1

聰明的程序員留下的繁重工作日期時間計算到專門的圖書館。在Java中,這將是Joda-Time(或在Java 8,JSR 310中)。

這裏是在Java 7中

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. 

String dateString = "28-Nov-2013 09:15 AM"; // Assumed to be the local date-time in United States west coast. 
//String dateString = "28-Nov-2013 09:15 PM"; // Test "PM" as well as "AM" if you like. 

// Joda-Time has deprecated use of 3-letter time zone codes because of their inconsistency. Use other identifier for zone. 
// Time Zone list: http://joda-time.sourceforge.net/timezones.html 
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles"); 

// Joda-Time formatting codes: http://www.joda.org/joda-time/key_format.html 
org.joda.time.format.DateTimeFormatter dateStringFormat = org.joda.time.format.DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm aa").withZone(californiaTimeZone); 

org.joda.time.DateTime californiaDateTime = dateStringFormat.parseDateTime(dateString); 
org.joda.time.DateTime utcDateTime = californiaDateTime.toDateTime(org.joda.time.DateTimeZone.UTC); 

// Both of these date-time objects represent the same moment in the time-line of the Universe, 
// but presented with different time-zone offsets. 
System.out.println("californiaDateTime: " + californiaDateTime); 
System.out.println("utcDateTime: " + utcDateTime); 

中運行時,喬達時間2.3示例代碼...

californiaDateTime: 2013-11-28T09:15:00.000-08:00 
utcDateTime: 2013-11-28T17:15:00.000Z 
+0

感謝羅勒,它運行良好,如果dateString =「28 - 11月2013年09: 15 AM「,但如果dateString =」2013年11月28日09:15 PM「,則它給出」2013-11-28T17:15:00.000Z「,其中預期的答案是」2013-11-29T05:15:00.000Z 「即日期不會改變 – user3005581

+0

@ user3005581您在格式化程序的模式模板中發現了一個錯誤。我用大寫的「HH」表示「解釋爲24小時制」。我在另一個問題中發現了這種診斷,[爲什麼喬達時間將輸入字符串中的PM更改爲AM?](http://stackoverflow.com/q/4683950/642706)。我現在修復我的示例代碼。 –

+0

感謝@basil,完美地工作。 – user3005581