简体   繁体   English

无法在GWT中将字符串转换为日期

[英]Not able to convert the String to Date in GWT

I am trying to change a String to Date in GWT.After Searching in StackOverFlow, I got one solution.But still I am getting 我试图在GWT中将String更改为Date。在StackOverFlow中搜索后,我得到了一个解决方案。

java.lang.IllegalArgumentException: Fri Feb 21 00:00:00 IST 2014 java.lang.IllegalArgumentException:2014年2月21日星期五00:00:00

Below is my code in GXT GridEditor class. 下面是我在GXT GridEditor类中的代码。

DateTimeFormat fmt = DateTimeFormat.getFormat("EEE MMM dd HH:mm:ss z yyyy");
Date checkInDate = fmt.parse(ACCCheckBoxModel.getSelectedItem().getCheckinDate());

From ACCCheckBoxModel.getSelectedItem().getCheckinDate() I am getting a String. ACCCheckBoxModel.getSelectedItem().getCheckinDate()我得到一个字符串。 I need to convert this String to Date. 我需要将此字符串转换为Date。 And then I need to convert the Date format to dd/MMM/yyyy format. 然后,我需要将日期格式转换为dd/MMM/yyyy格式。

Please suggest how to resolve this. 请提出解决方法。

The parser does not understand IST. 解析器不了解IST。 Try to move parsing to the server side. 尝试将解析移到服务器端。

Following citation from API-Doc : 根据API-Doc的引用:

The time zone support for parsing is limited. 解析时区支持有限。 Only standard GMT and RFC format are supported. 仅支持标准GMT和RFC格式。 Time zone specification using time zone id (like America/Los_Angeles), time zone names (like PST, Pacific Standard Time) are not supported. 不支持使用时区ID(例如America / Los_Angeles),时区名称(例如PST,太平洋标准时间)的时区规范。 Normally, it is too much a burden for a client application to load all the time zone symbols. 通常,对于客户端应用程序而言,加载所有时区符号会带来太多负担。 And in almost all those cases, it is a better choice to do such parsing on server side through certain RPC mechanism. 在几乎所有这些情况下,通过某些RPC机制在服务器端进行此类解析都是一个更好的选择。 This decision is based on particular use cases we have studied; 该决定基于我们研究过的特定用例。 in principle, it could be changed in future versions. 原则上,可以在将来的版本中进行更改。

The problem is with IST that doesn't converted by DateTimeFormat 问题出在DateTimeFormat不能转换的IST

Try this one, Its working fine without IST in pattern 试试这个,在没有IST模式的情况下可以正常工作

    DateTimeFormat fmt = DateTimeFormat.getFormat("EEE MMM dd HH:mm:ss yyyy");
    Date checkInDate = fmt.parse("Fri Feb 21 00:00:00 2014");
    System.out.println(DateTimeFormat.getFormat("dd/MMM/yyyy").format(checkInDate));

or use GMT in place of IST as shown below 或使用GMT代替IST ,如下所示

    DateTimeFormat fmt = DateTimeFormat.getFormat("EEE MMM dd HH:mm:ss v yyyy");
    Date checkInDate = fmt.parse("Fri Feb 21 00:00:00 GMT+05:30 2014");
    System.out.println(DateTimeFormat.getFormat("dd/MMM/yyyy").format(checkInDate));

Simply call a GWT RPC call and at server try below code to find out your localized date format and use it now. 只需调用GWT RPC调用,然后在服务器上尝试以下代码,即可找到本地化的日期格式并立即使用。

        Locale locale = httpServletRequest.getLocale();

        final Date currentDate = new Date();
        final DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.FULL, locale);
        final String format = dateInstance.format(currentDate);
        System.out.println(format);

        if (dateInstance instanceof SimpleDateFormat) {
            System.out.println("pattern: " + ((SimpleDateFormat) dateInstance).toPattern());
            System.out.println("localized pattern: "+((SimpleDateFormat) dateInstance).toLocalizedPattern());
        }

Here is the sample code about date pattern at internationalization sample 这是有关国际化示例中日期模式的示例代码

Here is one more link to read about How to get the pattern of Number Format of a specific locale? 这里是阅读有关如何获取特定语言环境的数字格式模式的另一个链接 at server side. 在服务器端。

Try this : 尝试这个 :

String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(date); // Sat Jan 02 00:00:00 BOT 2010

and take a look at 看看

Java string to date conversion Java字符串到日期的转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM