【问题标题】:Get Date details (day, month, year) in GWT在 GWT 中获取日期详细信息(日、月、年)
【发布时间】:2010-07-08 08:19:11
【问题描述】:

我需要从 Date 值中获取日、月、年的详细信息,但 getYear() 已被弃用,年份为 2 位数,并且 Y2K 存在问题(2008 年给出 108)。 Java 文档推荐使用java.util.calendar,但 GWT 不支持。

我想避免为了处理日期而在服务器和客户端之间来回发送所有信息。

编辑:Calendar 可能会被支持 日期处理功能应该在 GWT 未来版本中实现http://code.google.com/p/google-web-toolkit/issues/detail?id=603

【问题讨论】:

    标签: datetime gwt date


    【解决方案1】:

    Ashwin 提出的解决方法很有效,而且没有那么棘手。但我建议使用日期模式而不是拆分:

    For date - 
    DateTimeFormat.getFormat( "d" ).format( new Date() )
    
    For month - 
    DateTimeFormat.getFormat( "M" ).format( new Date() )
    
    For year - 
    DateTimeFormat.getFormat( "yyyy" ).format( new Date() )
    

    【讨论】:

      【解决方案2】:

      不要在 GWT 中的 Date 类上使用那些不推荐使用的方法。

      如果您不想为 GWT 使用第三方 Date 实现,您可以暂时使用 DateTimeFormat 和字符串操作的组合作为解决方法,直到 GWT 对操作日期提供更好的支持。

      For date - 
      DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[0]
      
      For month - 
      DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[1]
      
      For year - 
      DateTimeFormat.getFormat( "d-M-yyyy" ).format( new Date() ).split( "-")[2]
      

      编辑- 同样,避免使用 new Date( yy, mm, dd ) 会出现不一致,具体取决于浏览器和日期范围。

      我使用了一个简单的 DateUtil 类在 GWT 中创建和解析 Date 对象,也许对你有些用处 -

      (警告:非常粗糙,正在进行中)

      public class DateUtil
      {
          private static final String D_M_YYYY = "d-M-yyyy";
          private static final String DATE_SEPARATOR = "-";
      
          public static Date getDate( Integer dd, Integer mm, Integer yyyy )
          {
              if ( dd == null || mm == null || yyyy == null )
                  return null;
      
              Date retVal = null;
              try
              {
                  retVal = DateTimeFormat.getFormat( D_M_YYYY ).parse( dd + DATE_SEPARATOR + mm + DATE_SEPARATOR + yyyy );
              }
              catch ( Exception e )
              {
                  retVal = null;
              }
      
              return retVal;
          }
      
          public static String getDayAsString( Date date )
          {
              return ( date == null ) ? null : DateTimeFormat.getFormat( D_M_YYYY ).format( date ).split( DATE_SEPARATOR )[0];
          }
      
          public static String getMonthAsString( Date date )
          {
              return ( date == null ) ? null : DateTimeFormat.getFormat( D_M_YYYY ).format( date ).split( DATE_SEPARATOR )[1];
          }
      
          public static String getYearAsString( Date date )
          {
              return ( date == null ) ? null : DateTimeFormat.getFormat( D_M_YYYY ).format( date ).split( DATE_SEPARATOR )[2];
          }
      
          public static boolean isValidDate( Integer dd, Integer mm, Integer yyyy )
          {
              boolean isvalidDate = true;
      
              try
              {
                  String transformedInput = DateTimeFormat.getFormat( D_M_YYYY ).format( getDate( dd, mm, yyyy ) );
                  String originalInput = dd + DATE_SEPARATOR + mm + DATE_SEPARATOR + yyyy;
      
                  isvalidDate = transformedInput.equals( originalInput );
              }
              catch ( Exception e )
              {
                  isvalidDate = false;
              }
      
              return isvalidDate;
          }
      }
      

      【讨论】:

      • 您会推荐哪些第三方 Date 实现?
      • 我的应用程序中没有复杂的日期算法,因此没有认真寻找任何用于 GWT 的 DateTime 库。我知道有一个 GwtDateTime 实用程序 (code.google.com/p/gwt-examples/wiki/gwtDateTime),但没有对其进行测试,无法保证。 YMMV
      • 这些在单元测试中不起作用:((除非你扩展 GWTTestCase 很慢)
      • “不要在 GWT 中的 Date 类上使用那些不推荐使用的方法,我在客户端看到了一些不一致的行为。” - 真的?能给我举个例子吗? DateTimeFormat 的幕后实现广泛使用这些已弃用的方法 :: code.google.com/p/google-web-toolkit/source/browse/trunk/user/…
      • 我在我的 GWT 客户端代码中使用了那些不推荐使用的函数。当 Calendar 出现时,它们已被 Java 弃用。我认为在 GWT 客户端中使用它们并没有什么坏处。我认为下面的 BlaXpirit 分享了正确的解决方案,只需调用 getYear() 并添加 1900。Upvoted。
      【解决方案3】:

      您可以将 1900 添加到 getYear() 返回值

      【讨论】:

        【解决方案4】:

        我认为 gwt 将来不会支持日历,可能它可以支持另一个日期操作实现。 因此,由于关于不使用 Date 的 java 建议在 Gwt 中无效,并且在不导入第三方库的情况下您没有任何其他选择,因此正确的方法是使用 Date 并忽略弃用警告。

        【讨论】:

        • 有一些边缘情况在 Date 的弃用 getter 中失败。我花了几个小时在日期的 getDate() 方法中找到错误的来源,此后停止使用 Date 方法。我强烈建议您在部署到生产环境之前,针对您的预期日期范围彻底测试您的日期方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-27
        • 2011-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多