在 GWT 中获取日期详细信息(日、月、年) [英] Get Date details (day, month, year) in GWT

查看:21
本文介绍了在 GWT 中获取日期详细信息(日、月、年)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get the day, month, year details from a Date value but getYear() is deprecated, gives year on 2 digits, and has problems with Y2K (2008 gives 108). The java doc recommends using java.util.calendar but it is not supported in GWT.

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

I want to avoid sending all the info back and forth between the server and client just to deal with dates.

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

推荐答案

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

Do not use those deprecated methods on Date class in GWT.

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

If you don't want to use third party Date implementations for GWT, You use a combination of DateTimeFormat along with string manipulation as a workaround for the time being, until GWT comes up with some better support for manipulating dates.

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 ) 会导致不一致.

Edit- Similarly, avoid using new Date( yy, mm, dd ) has come inconsistencies depending on the browser and date range.

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

I have use a simple DateUtil Class to create and parse Date objects in GWT, maybe of some use to you -

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

(Warning: Very crude, and work in progress)

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;
    }
}

这篇关于在 GWT 中获取日期详细信息(日、月、年)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆