代码之家  ›  专栏  ›  技术社区  ›  Jla

获取GWT中的日期详细信息(日、月、年)

  •  18
  • Jla  · 技术社区  · 16 年前

    我需要从一家公司得到一天,一个月,一年的详细信息 Date 价值但是 getYear() 已弃用,以2位数字表示年份,并且存在Y2K问题(2008年为108)。javadoc建议使用 java.util.calendar

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

    Calendar 可能得到支持 应实现数据处理功能 在GWT未来版本中: http://code.google.com/p/google-web-toolkit/issues/detail?id=603

    4 回复  |  直到 16 年前
        1
  •  20
  •   Ashwin Prabhu    10 年前

    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]
    

    编辑- 同样,避免使用新日期(yy,mm,dd)也会因浏览器和日期范围的不同而出现不一致。

    我在GWT中使用了一个简单的DateUtil类来创建和解析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;
        }
    }
    
        2
  •  26
  •   Andres    15 年前

    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() )
    
        3
  •  2
  •   Oleh Prypin    16 年前

    你可以把1900加到 getYear() 返回值

        4
  •  1
  •   Manolo Carrasco Moñino    16 年前

    我不认为gwt将来会支持Calendar,也许它会支持另一个日期操纵实现。

    推荐文章