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

Jla*_*Jla 18 gwt datetime date

我需要从一个Date值中获取日,月,年的详细信息但是getYear()已弃用,年份为2位数,并且Y2K存在问题(2008年给出108).java doc建议使用java.util.calendar但GWT不支持它.

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

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

And*_*res 26

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() )
Run Code Online (Sandbox Code Playgroud)


Ash*_*bhu 20

不要在GWT中的Date类上使用这些弃用的方法.

如果你不想使用第三方日期为实现GWT,您使用的组合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]
Run Code Online (Sandbox Code Playgroud)

编辑 - 类似地,应避免使用新的日期取决于浏览器和日期范围(YY,毫米,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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • "不要在GWT中的Date类上使用那些弃用的方法,我已经在客户端看到了一些不一致的行为." - 真的吗?你能给我举个例子吗?`DateTimeFormat`的幕后实现广泛使用这些已弃用的方法:: http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/ GWT/I18N /共享/ DateTimeFormat.java R = 10061 (2认同)