I have a function that must work the same way on both client and server and it formats dates.
if (GWT.isClient())
{
// Use DateTimeFormat
} else {
// Use
You can use com.google.gwt.i18n.shared.DateTimeFormat
on both server and client:
Call the protected constructor to avoid GWT.create
String pattern = "yyyyMMdd"; /*your pattern here*/
DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo();
DateTimeFormat dtf = new DateTimeFormat(pattern, info) {}; // <= trick here
Example
Date d = dtf.parse("20120301");
CalendarUtil.addDaysToDate(d, -1);
String s = dtf.format(d);
// s now contains "20120229"
The trick is done by extending the DateTimeFormat
so we can use protected constructor with DateTimeFormatInfo
where we use the new DefaultDateTimeFormatInfo()
to avoid calling of GWT.create
import com.google.gwt.i18n.shared.DateTimeFormat;
DateTimeFormat fm = DateTimeFormat.getFormat("MM/dd");
String st = fm.format(date);
This solution is a bit different but in the same path of the one @ochakov presented but it resolves the GWT 2.7 problem @stepancheg and I mentioned.
import java.util.Date;
import com.google.gwt.core.client.GWT;
import com.google.gwt.thirdparty.guava.common.annotations.GwtCompatible;
import com.google.gwt.thirdparty.guava.common.annotations.GwtIncompatible;
public abstract class DateTimeFormat {
static DateTimeFormat getFormat(String pattern)
{
if (GWT.isClient())
return new DateTimeFormatClient(pattern);
else
return new DateTimeFormatServer(pattern);
}
public abstract String format(Date date);
public abstract Date parse(String dateString);
@GwtCompatible
private static class DateTimeFormatClient extends DateTimeFormat
{
protected String pattern;
public DateTimeFormatClient(String pattern)
{
this.pattern = pattern;
}
public String format(Date date)
{
return com.google.gwt.i18n.client.DateTimeFormat.getFormat(pattern).format(date);
}
public Date parse(String stringDate){
return com.google.gwt.i18n.client.DateTimeFormat.getFormat(pattern).parseStrict(stringDate);
}
}
private static class DateTimeFormatServer extends DateTimeFormatClient
{
public DateTimeFormatServer(String pattern)
{
super(pattern);
}
@GwtIncompatible("Server format")
public String format(Date date)
{
return (new java.text.SimpleDateFormat(pattern)).format(date);
}
@GwtIncompatible("Server parse")
public Date parse(String dateString){
try{
return (new java.text.SimpleDateFormat(pattern)).parse(dateString);
}catch(Exception ex){
throw new IllegalArgumentException("Cannot convert to date: "+ dateString);
}
}
}
}
Hope this help others.
You have to tell Eclipse not to compile your super-source'd Java file. If you're using Maven, it's simply a matter of moving it to src/main/resources; otherwise, exclude your 'jre' package from Eclipse's build path.
...that being said, I'd rather super-source the class the uses the SimpleDateFormat/DateTimeFormat, and/or move that to a helper class that you'd super-source.
import java.util.Date;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.thirdparty.guava.common.annotations.GwtCompatible;
import com.google.gwt.thirdparty.guava.common.annotations.GwtIncompatible;
public abstract class DateTimeFormat
{
static DateTimeFormat getFormat(String pattern)
{
if (GWT.isClient())
return DateTimeFormatClient.getFormat(pattern);
else
return DateTimeFormatServer.getFormat(pattern);
}
public abstract String format(Date date);
@GwtCompatible
private static class DateTimeFormatClient extends DateTimeFormat
{
private com.google.gwt.i18n.client.DateTimeFormat dateTimeFormat;
protected DateTimeFormatClient(String pattern)
{
this.dateTimeFormat = com.google.gwt.i18n.client.DateTimeFormat.getFormat(pattern);
}
public static DateTimeFormat getFormat(String pattern)
{
return new DateTimeFormatClient(pattern);
}
public String format(Date date)
{
return dateTimeFormat.format(date);
}
}
@GwtIncompatible("Server version of the class")
private static class DateTimeFormatServer extends DateTimeFormat
{
private java.text.SimpleDateFormat dateTimeFormat;
protected DateTimeFormatServer(String pattern)
{
this.dateTimeFormat = new java.text.SimpleDateFormat(pattern);
}
public static DateTimeFormat getFormat(String pattern)
{
return new DateTimeFormatServer(pattern);
}
public String format(Date date)
{
return dateTimeFormat.format(date);
}
}
}