2016-05-23 48 views
0

我想在我的libGdx應用程序中使用DateTime格式,並且我想與Web版本(gwt)兼容。默認的java方式不支持gwt版本,因爲所需的類不受支持。如何在libGdx中使用date-time utils?

我已經嘗試使用外部庫,如joda-timetreeten-bp。後者具有gwt中不支持的java類的依賴關係,並且joda-time具有我無法解析的依賴關係...

我已經看到gwt有我需要的時間utils,但我無法弄清楚如何使用它們。

甚至如何添加一些邏輯,以在Android和桌面使用普通的java utils,並在html5/gwt中使用gwt類。但我無法弄清楚如何使用gwt類即。在libGdx中爲DateTimeFormat。試圖剛剛導入它的結果在Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gwt/core/client/GWTBridge

在此先感謝您的幫助!

回答

1

您需要使用ActionResolver併爲您的每個模塊實現希望使用的DateTime方法(有關如何編寫平臺特定代碼的信息,請參見https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code)。

對於Android,iOS和桌面實現,您可以在ActionResolver中使用SimpleDateFormat。

對於HTML模塊,你可以使用庫

com.google.gwt.i18n.client.DateTimeFormat 

並實現如下

public class HtmlLauncher extends GwtApplication implements com.yourpackage.util.ActionResolver{ 

     @Override 
     public GwtApplicationConfiguration getConfig() { 
       return new GwtApplicationConfiguration(360, 640); 
     } 

     @Override 
     public ApplicationListener getApplicationListener() { 
       return new MyGame(this); 
     } 

     @Override 
     public String convertDate(String format, Date date) { 
      DefaultDateTimeFormatInfo info = new DefaultDateTimeFormatInfo(); 
      DateTimeFormat dateFormat = new DateTimeFormat(format, info) {}; 

      return dateFormat.format(date); 
     } 

    } 
+0

非常感謝!我知道我已經閱讀過有關這種方法..它只是沒有點擊我的腦海,這正是我所需要的。 –

+0

找到它了:https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code –