Nie stosować te przestarzałe metody na Klasa daty w GWT.
Jeśli nie chcesz używać zewnętrznych implementacji dat dla GWT, używasz kombinacji DateTimeFormat
wraz z manipulacją ciągami jako obejściem, dopóki GWT nie pojawi się z lepszą obsługą manipulowania datami.
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- Podobnie unikać new Date (rr, mm, dd) doszła niespójności w zależności od przeglądarki i zakresu dat.
mam użyć prostego DateUtil Class do tworzenia i analizowania obiektów data w GWT, może przydać do Ciebie -
(Ostrzeżenie: Bardzo surowy, a prace w toku)
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;
}
}
Jakie implementacje dat strony trzeciej poleciłbyś? – Jla
Nie mam złożonych arytmetyki terminów w mojej aplikacji, a tym samym nie poważnie wypatrywałem żadnej biblioteki DateTime dla GWT. Wiem, że istnieje narzędzie GwtDateTime (http://code.google.com/p/gwt-examples/wiki/gwtDateTime), ale nie przetestowałem go, nie mogę ręczyć za to. YMMV –
Te nie działają w testach jednostkowych :((chyba że rozszerzysz GWTTestCase, która jest wolna) –