Reputation: 135
How do I convert String to Date or Date to String using GWT 2.0.3?
Upvotes: 12
Views: 9687
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
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
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