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: Date handling functions should be implemented in GWT futur versions : http://code.google.com/p/google-web-toolkit/issues/detail?id=603Calendar
might be supported
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() )
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With