2012-04-23 45 views
15

我有一個函數,它必須在客戶端和服務器上以相同的方式工作,並且它格式化日期。GWT:在客戶端上使用DateTimeFormat,在服務器上使用SimpleDateFormat

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

回答

0

你必須告訴Eclipse不會編譯您的超級源代碼的Java文件。如果您使用的是Maven,只需將其移至src/main/resources;否則,從Eclipse的構建路徑中排除您的'jre'

......這樣說,我寧願超類源使用SimpleDateFormat/DateTimeFormat,和/或將它移動到你超級源的助手類。

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

    } 
} 
+0

這是否需要來自http://code.google.com/p/google-web-toolkit/source/detail?r=11570的@GwtIncompatible是否已發佈? – fgb 2013-05-21 16:01:19

+1

這是現在發佈在GWT 2.6中(但你是對的,它不是在2.5.1或更早的版本中) – 2013-12-31 20:39:50

+1

在GWT 2.7中不起作用:'getFormat(String)方法未定義爲類型DateTimeFormat.DateTimeFormatServer' – stepancheg 2015-07-19 15:17:33

27

您可以使用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

該解決方案是一個有點不同,但在@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); 
      } 
     } 

    } 
} 

希望這對他人有所幫助。

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

非常整潔的解決方案。 – Vic 2017-05-25 11:47:06

相關問題