gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:39:34 25 4
gpt4 key购买 nike

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

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

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

You can provide a dummy supersource implementation of SimpleDateTimeFormat so that it would compile.

我试过了。现在 Eclipse 提示:

java.text The declared package "java.text" does not match the expected package "foo.jre.java.text" SimpleDateFormat.java

最佳答案

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

调用 protected 构造函数以避免 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 来完成的,这样我们就可以将 protected 构造函数与 DateTimeFormatInfo 一起使用,其中我们使用 new DefaultDateTimeFormatInfo() 来避免调用GWT.create

关于GWT:在客户端上使用 DateTimeFormat,在服务器上使用 SimpleDateFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10284564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com