user312796
user312796

Reputation: 135

convert String to Date on GWT

How do I convert String to Date or Date to String using GWT 2.0.3?

Upvotes: 12

Views: 9687

Answers (3)

Chethan
Chethan

Reputation: 541

To convert Date to StringFormat

    import com.google.gwt.i18n.shared.DateTimeFormat;
    import java.util.Date;

    ...
    public String getStringFormat(Date inputDate){
    String strFormat = null;
    try{
    DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("DD-MMM-YYYY");
    strFormat = dateTimeFormat.format(inputDate);
    }catch(Exception e){
    e.printStackTrace();
    }
    return strFormat;
    }

Upvotes: 1

Maksymilian Wojakowski
Maksymilian Wojakowski

Reputation: 5081

import com.google.gwt.i18n.shared.DateTimeFormat;
import java.util.Date;

...

public Date getDate(String dateString) {
    Date result = null;
    try {
        DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
        result = dateTimeFormat.parse(dateString);
    } catch (Exception e)
    {
        // ignore
    }
    return result;
}

Upvotes: 11

Hilbrand Bouwkamp
Hilbrand Bouwkamp

Reputation: 13519

With the methods on the java.util.Date class itself: parse(String s) and toString(). Although the first is deprecated it is the method to use with GWT. If you want more control over how the Date is formatted, when converting to String use the GWT specific class: com.google.gwt.i18n.client.DateTimeFormat. It will format the date given a pattern, similar to Java's own SimpleDateFormat.

Upvotes: 11

Related Questions