javascript 如何在 GWT 中使用时区解析日期字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5105004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:57:25  来源:igfitidea点击:

How to parse date string using Timezone in GWT

javajavascriptgwtdatetimezone

提问by Zalivaka

Has anybody succeeded parsing date string with a custom timezone in GWT? GWT's DateTimeFormat allows to format dates based on time zone, but I haven't found any method for doing opposite operation. So what should I do if I have following string "02:01:2011" (format "MM:dd:yyyy"). It can have different results in different timezones.

有没有人在 GWT 中使用自定义时区成功解析日期字符串?GWT 的 DateTimeFormat 允许根据时区格式化日期,但我还没有找到任何进行相反操作的方法。那么如果我有以下字符串“02:01:2011”(格式“MM:dd:yyyy”),我该怎么办。它可能在不同的时区有不同的结果。

The other problem appears when trying to change dates, months and etc. How can I do it based on a custom timezone?

尝试更改日期、月份等时会出现另一个问题。如何根据自定义时区进行更改?

Maybe there is any library which can simplify all these operations?

也许有任何可以简化所有这些操作的库?



I have made workaround and add timezone part to each date string which miss that part. Still looking for a more professional solution.

我已经制定了解决方法并将时区部分添加到每个错过该部分的日期字符串中。仍在寻找更专业的解决方案。

回答by Brad

Either give the timezone to the client from the server (e.g., include it in the date string) or standardize the timezone on the server so that the client can assume a constant timezone. If you include the timezone with the date string, the below code snippet should work.

从服务器向客户端提供时区(例如,将其包含在日期字符串中)或标准化服务器上​​的时区,以便客户端可以采用恒定时区。如果您在日期字符串中包含时区,则下面的代码片段应该可以工作。

I havent tested this, but according to the docs, it should work:

我还没有测试过这个,但根据文档,它应该可以工作:

String dateStr = "04/21/2011 01:37:36 -0800;
DateTimeFormat format = new DateTimeFormat("MM/dd/yyyy HH:mm:ss Z");
Date date = format.parse(dateStr);

Depending on how you are representing the timezone, you can change the final variable in the format string (the Z). See the docs for details: http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html

根据您表示时区的方式,您可以更改格式字符串(Z)中的最终变量。有关详细信息,请参阅文档:http: //google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html

回答by Peter Jamieson

I did the following to parse a date in the TimeZone tz. It's probably dodgy, but it works: -

我执行以下操作来解析 TimeZone tz 中的日期。这可能是狡猾的,但它有效:-

final long MILLIS_IN_MINUTE = 60000;

Date localDate = DateTimeFormat.getFormat("dd MMM yyyy HH:mm:ss").parse(dateString);

int localOffset = localDate.getTimezoneOffset() * MILLIS_IN_MINUTE;
int targetOffset = tz.getOffset(localDate) * MILLIS_IN_MINUTE;

// Subtract the offset to make this into a UTC date.
return new Date(localDate.getTime() - localOffset + targetOffset);

It parses the date in the client timezone and then adjusts it to the required timezone.

它解析客户端时区中的日期,然后将其调整为所需的时区。

回答by Bartek Jablonski

Recently I passed upon this project: gwt-calendar-classwhich emulates Calendar and TimeZone in javascript.

最近我通过了这个项目:gwt-calendar-class,它在 javascript 中模拟 Calendar 和 TimeZone。

回答by Jaisri_88

public static Date getDateGWT(final String strDate, final int style) {
        Date date = null;
        int useStyle = style;
        if (!validStyle(style)) {
            useStyle = DEFAULT_DATE_STYLE;
        }

        if ((strDate != null) && (strDate.trim().length() > 0)) {
            DateTimeFormat df = getDateFormatGWT(useStyle);
            try {
                date = df.parse(strDate);
            } catch (Exception e) {
                date = df.parse(date.toString());
            }
        }
        return date;
    }

     private static DateTimeFormat getDateTimeFormatGWT(final int style) {
        switch(style) {
        case SHORT:
            return DateTimeFormat.getShortDateTimeFormat();
        case MEDIUM:
            return DateTimeFormat.getMediumDateTimeFormat();
        case LONG:
            return DateTimeFormat.getLongDateTimeFormat();
        case FULL:
            return DateTimeFormat.getFullDateTimeFormat();
        default :
            return DateTimeFormat.getMediumDateTimeFormat();
        }        
   }

Try This

试试这个