Ne pas utiliser ces méthodes dépréciées sur Classe de date dans GWT. Si vous ne souhaitez pas utiliser les implémentations tierces de Date pour GWT, vous utilisez pour l'instant une combinaison de DateTimeFormat
et de manipulation de chaînes pour que GWT prenne mieux en charge la manipulation des 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]
Modifier- De même, évitez d'utiliser new Date (yy mm, dd) a venir des incohérences en fonction de la plage du navigateur et la date.
Je vérifiions une simple classe DateUtil pour créer et analyser des objets Date à GWT, peut-être d'une certaine utilité pour vous -
(Attention: Très brut et les travaux en cours)
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;
}
}
Quels implémentations de tiers recommanderiez-vous? – Jla
Je n'ai pas d'arithmétique de date complexe dans mon application, et par conséquent, je n'ai pas sérieusement recherché de bibliothèque de DateTime pour GWT. Je sais qu'il existe un utilitaire GwtDateTime (http://code.google.com/p/gwt-examples/wiki/gwtDateTime), mais ne l'ayant pas testé, ne peut pas le garantir. YMMV –
Ceux-ci ne fonctionnent pas dans les tests unitaires :((sauf si vous étendez GWTTestCase qui est lent) –