GWT:在客户端使用DateTimeFormat,在服务器上使用SimpleDateFormat
我有一个功能,必须在客户端和服务器上以相同的方式工作,并格式化日期GWT:在客户端使用DateTimeFormat,在服务器上使用SimpleDateFormat,gwt,Gwt,我有一个功能,必须在客户端和服务器上以相同的方式工作,并格式化日期 if (GWT.isClient()) { // Use DateTimeFormat } else { // Use SimpleDateFormat } GWT抱怨:SimpleDataFormat类型没有可用的源代码。这个错误不是致命的(至少在开发模式下是如此),但很烦人,无法抑制。 在上发现了类似的问题。他们建议: 您可以提供的虚拟超级源实现 SimpleDateTimeFormat,以便它可以编译 我试过了。
if (GWT.isClient())
{
// Use DateTimeFormat
} else {
// Use SimpleDateFormat
}
GWT抱怨:SimpleDataFormat类型没有可用的源代码。这个错误不是致命的(至少在开发模式下是如此),但很烦人,无法抑制。
在上发现了类似的问题。他们建议:
您可以提供的虚拟超级源实现
SimpleDateTimeFormat,以便它可以编译
我试过了。现在Eclipse抱怨:
java.text
声明的包“java.text”与预期的包“foo.jre.java.text”SimpleDataFormat.java不匹配
您必须告诉Eclipse不要编译您的超级源代码Java文件。如果您使用的是Maven,那么只需将其移动到src/main/resources;否则,从Eclipse的构建路径中排除您的“jre”包
…话虽如此,我还是希望对使用SimpleDataFormat/DateTimeFormat的类进行超级源代码化,和/或将其移动到要进行超级源代码化的帮助器类。您可以在服务器和客户端上使用
com.google.gwt.i18n.shared.DateTimeFormat
:
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);
}
}
}
调用受保护的构造函数以避免GWT.create
String pattern = "yyyyMMdd"; /*your pattern here*/
DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo();
DateTimeFormat dtf = new DateTimeFormat(pattern, info) {}; // <= trick here
这个技巧是通过扩展
DateTimeFormat
来实现的,这样我们就可以在DateTimeFormatInfo
中使用受保护的构造函数,其中我们使用了新的DefaultDateTimeFormatInfo()
为避免调用GWT.create
此解决方案略有不同,但与@ochakov提出的解决方案路径相同,但它解决了@stepancheg和我提到的GWT 2.7问题
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);
}
}
}
}
希望这对其他人有所帮助。这是否需要@GwtIncompatible from它发布了吗?它现在在GWT 2.6中发布了(但你是对的,它不在2.5.1或更早版本中)在GWT 2.7中不起作用:
类型DateTimeFormat.DateTimeFormatServer的getFormat(String)方法没有定义。
我有@stepancheg提到的相同问题,同样的错误,有什么解决方法吗?非常简洁的解决方案。不起作用,因为getFormat
内部使用了LocaleInfo
,它再次出现在客户端包中。这是一个GWT错误。不幸的是,只是移动了错误:类型DateTimeFormat.DateTimeFormatServer必须实现继承的抽象方法DateTimeFormat.format(Date)
-GWT 2.8构造函数DateTimeFormat(String,DefaultDateTimeFormatInfo)未定义
import com.google.gwt.i18n.shared.DateTimeFormat;
DateTimeFormat fm = DateTimeFormat.getFormat("MM/dd");
String st = fm.format(date);
import com.google.gwt.i18n.shared.DateTimeFormat;
DateTimeFormat fm = DateTimeFormat.getFormat("MM/dd");
String st = fm.format(date);