【问题标题】:Converting Date with Time in PST into UTC format将 PST 中的日期与时间转换为 UTC 格式
【发布时间】:2013-12-07 07:54:02
【问题描述】:

我有一个变量 str(字符串类型),其值为“28-Nov-2013 09:15 AM”。如何将其转换为 UTC 格式(上述 str 变量中的时间是 PST,因此是 UTC应该比这多 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;
    }

【问题讨论】:

    标签: java javascript apache-flex actionscript utc


    【解决方案1】:

    聪明的程序员将繁重的日期时间计算留给专门的库。在 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 );
    

    运行时……

    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 您在我的格式化程序模式模板中发现了一个错误。我在时间中使用了大写的“HH”,意思是“解释为 24 小时制”。我在另一个问题Why does Joda time change the PM in my input string to AM? 中找到了这个诊断。我现在将修复我的示例代码。
    猜你喜欢
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2020-11-21
    • 2021-10-14
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多