将PST时区的日期和时间转换为UTC格式

3

我是一个变量str(字符串类型),其值为“28-Nov-2013 09:15 AM”。如何将它转换为UTC格式(上述str变量中的时间为PST,因此UTC应比PST多8个小时)。 我正在使用Flex 2.以下是不起作用的代码:

 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个回答

2

聪明的程序员会将日期和时间计算的重活交给专门的库来处理。在Java中,这个库就是Joda-Time(或在Java 8中,JSR 310)。

这里是Java 7中Joda-Time 2.3的示例代码。

// © 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 );

When run…

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

谢谢 Basil,如果 dateString = "28-Nov-2013 09:15 AM",它可以正常运行,但是如果 dateString = "28-Nov-2013 09:15 PM",则会显示 "2013-11-28T17:15:00.000Z",而期望的答案是 "2013-11-29T05:15:00.000Z",即日期未更改。 - user3005581
@user3005581 你发现了我格式化模板中的一个bug。我在时间中使用了大写字母“HH”,它意味着“将其解释为24小时制时钟”。我在另一个问题为什么Joda时间会将我的输入字符串中的PM更改为AM?中发现了这个诊断。我现在会修复示例代码。 - Basil Bourque

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接