2013-11-22 2 views
1

"str (string type)"값이 "28-Nov-2013 09:15 AM"인 UTC 형식으로 변환하는 방법 (위의 시간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

현명한 프로그래머의 무거운 리프팅을 떠나 -이 : STR 변수가 PST에에, 따라서 UTC는 작동하지 않는 코드를 다음 더 그 이상) .I 아래 플렉스 2.Find을 사용하고 8시간해야한다 특수 라이브러리에 대한 날짜 - 시간 계산. Java에서는 Joda-Time (또는 Java 8, JSR 310)입니다. 여기

는 ... 자바 실행 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); 

에 Joda 타임 2.3에 대한 예제 코드입니다

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

감사 바질, 그것은 잘 실행하면 09 날짜 열 = "28 11 월 2013 : 15 AM "이지만 dateString ="28-Nov-2013 09:15 PM "이면 예상 답변이"2013-11-29T05 : 15 : 00.000Z "인"2013-11-28T17 : 15 : 00.000Z "을 제공합니다. "즉 날짜가 바뀌지 않습니다 – user3005581

+0

@ user3005581 포맷터에 대한 패턴 템플릿에서 버그를 발견했습니다. 나는 "24 시간 시계로 해석"을 의미하는 시간에 대문자 "HH"를 사용했습니다. 다른 질문으로이 진단을 발견했습니다. [Joda가 입력 문자열의 PM을 AM으로 변경하는 이유는 무엇입니까?] (http://stackoverflow.com/q/4683950/642706). 이제 예제 코드를 수정하겠습니다. –

+0

감사합니다. @basil은 완벽하게 작동합니다. – user3005581

관련 문제