问题
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