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 SimpleDateFormat
}
GWT complains: No source code is available for type SimpleDateFormat. The error is not fatal (at least in dev mode), but annoying and no way to suppress it. Found a similar question on http://groups.google.com/group/google-web-toolkit/browse_thread/thread/981247fca161c287 . There they suggest:
You can provide a dummy supersource implementation of SimpleDateTimeFormat so that it would compile.
I tried. Now Eclipse complains:
java.text The declared package "java.text" does not match the expected package "foo.jre.java.text" SimpleDateFormat.java
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With