convert String to Date on GWT

家住魔仙堡 提交于 2019-12-23 07:33:34

问题


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


回答1:


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.




回答2:


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;
}



回答3:


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;
    }


来源:https://stackoverflow.com/questions/3543832/convert-string-to-date-on-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!