GWT:在客户端上使用DateTimeFormat,在服务器上使用SimpleDateFormat

时间:2012-04-23 16:35:30

标签: gwt

我有一个必须在客户端和服务器上以相同方式工作的函数,它会格式化日期。

if (GWT.isClient())
{
  // Use DateTimeFormat
} else {
  // Use SimpleDateFormat
}

GWT抱怨:没有源代码可用于SimpleDateFormat类型。这个错误并不是致命的(至少在开发模式下),但是令人烦恼并且无法抑制它。 在http://groups.google.com/group/google-web-toolkit/browse_thread/thread/981247fca161c287上找到了类似的问题。在那里他们建议:

  

您可以提供虚拟的超级源实现   SimpleDateTimeFormat以便它可以编译。

我试过了。现在Eclipse抱怨:

  

java.text中   声明的包“java.text”与预期的包“foo.jre.java.text”SimpleDateFormat.java

不匹配

5 个答案:

答案 0 :(得分:28)

您可以在服务器和客户端上使用com.google.gwt.i18n.shared.DateTimeFormat

调用受保护的构造函数以避免GWT.create

String pattern = "yyyyMMdd"; /*your pattern here*/ 
DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo();
DateTimeFormat dtf = new DateTimeFormat(pattern, info) {};  // <= trick here

实施例

Date d = dtf.parse("20120301");
CalendarUtil.addDaysToDate(d, -1);
String s = dtf.format(d);
// s now contains "20120229"

诀窍是通过扩展DateTimeFormat来完成的,因此我们可以使用DateTimeFormatInfo保护构造函数,我们使用new DefaultDateTimeFormatInfo()来避免调用GWT.create

答案 1 :(得分:1)

这个解决方案有点不同,但与@ochakov提供的路径相同,但它解决了GWT 2.7问题@stepancheg,我提到过。

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);
            }
        }

    }
}

希望这有助于他人。

答案 2 :(得分:1)

import com.google.gwt.i18n.shared.DateTimeFormat;
DateTimeFormat fm = DateTimeFormat.getFormat("MM/dd");
String st = fm.format(date);

答案 3 :(得分:0)

你必须告诉Eclipse不要编译你的超级源Java文件。如果你正在使用Maven,那只需要将它移到src / main / resources;否则,排除你的“jre”#39; Eclipse的构建路径中的 package

...话虽如此,我更倾向于使用SimpleDateFormat / DateTimeFormat超级使用类,和/或将其移动到您超级源的助手类。

答案 4 :(得分:0)

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);
        }       

    }
}
相关问题