18

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.

Edit: Calendar might be supported Date handling functions should be implemented in GWT futur versions : http://code.google.com/p/google-web-toolkit/issues/detail?id=603

Jla
  • 11,304
  • 14
  • 61
  • 84

4 Answers4

26

The workaround presented by Ashwin works and it's not that tricky. But I'd suggest using the date patterns instead of splitting:

For date - 
DateTimeFormat.getFormat( "d" ).format( new Date() )

For month - 
DateTimeFormat.getFormat( "M" ).format( new Date() )

For year - 
DateTimeFormat.getFormat( "yyyy" ).format( new Date() )
Andres
  • 1,484
  • 19
  • 29
20

Do not use those deprecated methods on Date class in 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]

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

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;
    }
}
Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
  • What third party Date implementations would you recommend ? – Jla Jul 08 '10 at 08:59
  • I don't have complex date arithmetics in my application, and hence haven't seriously looked out for any DateTime library for GWT. I know there is a GwtDateTime Utility ( http://code.google.com/p/gwt-examples/wiki/gwtDateTime ), but not having tested it, can't vouch for it. YMMV – Ashwin Prabhu Jul 08 '10 at 09:10
  • These don't work in unit tests :( (unless you extend GWTTestCase which is slow) – Ben Noland Oct 12 '11 at 14:31
  • 2
    "Do not use those deprecated methods on Date class in GWT, I have witnessed some inconsistent behaviour on the client side." -- really? Can you give an example? The behind-the-scenes implementation of `DateTimeFormat` uses these deprecated methods extensively :: http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/i18n/shared/DateTimeFormat.java?r=10061 – funkybro May 16 '12 at 09:37
  • I use those deprecated functions in my GWT client code. They were deprecated for Java when Calendar came to be. I see no harm using them in a GWT client. I think the correct solution is shared by BlaXpirit below, simply call getYear() and add 1900. Upvoted. – broc.seib Aug 07 '12 at 04:09
2

You may just add 1900 to getYear() return value

Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
1

I don't think gwt will support Calendar in the future, may be it could support another date manipulation implementation. So, because the java recommendation about not using Date is not valid in Gwt and you have not any other option without importing a third party library, the right way is to use Date and ignore deprecation warnings.

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • There are a few edge cases which fail in the deprecated getters of Date. It took me hours to locate the source of the bug to a getDate() method on the date, since then stopped using Date methods. I would strongly advise you to test your date methods thoroughly for your expected date range before deploying on production. – Ashwin Prabhu Jul 08 '10 at 09:38