user1903120
user1903120

Reputation: 97

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

java.lang.IllegalArgumentException: Fri Feb 21 00:00:00 IST 2014

Below is my code in GXT GridEditor class.

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. I need to convert this String to Date. And then I need to convert the Date format to dd/MMM/yyyy format.

Please suggest how to resolve this.

Upvotes: 1

Views: 787

Answers (3)

Andrei Volgin
Andrei Volgin

Reputation: 41099

The parser does not understand IST. Try to move parsing to the server side.

Following citation from API-Doc:

The time zone support for parsing is limited. Only standard GMT and RFC format are supported. Time zone specification using time zone id (like America/Los_Angeles), time zone names (like PST, Pacific Standard Time) are not supported. 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. This decision is based on particular use cases we have studied; in principle, it could be changed in future versions.

Upvotes: 1

Braj
Braj

Reputation: 46871

The problem is with IST that doesn't converted by DateTimeFormat

Try this one, Its working fine without IST in pattern

    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

    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.

        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.

Upvotes: 0

Braza
Braza

Reputation: 425

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

Upvotes: 0

Related Questions