Không sử dụng các phương pháp đó bị phản đối trên Lớp ngày trong GWT.
Nếu bạn không muốn sử dụng triển khai ngày của bên thứ ba cho GWT, Bạn sử dụng kết hợp DateTimeFormat
cùng với thao tác chuỗi như giải pháp trong thời gian tới, cho đến khi GWT có hỗ trợ tốt hơn cho thao tác ngày.
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- Tương tự, tránh sử dụng ngày mới mâu thuẫn (yy, mm, dd) đã đến tùy thuộc vào trình duyệt và phạm vi ngày.
Tôi có sử dụng một DateUtil lớp đơn giản để tạo và phân tích đối tượng Date trong GWT, có thể sử dụng một số bạn -
(Cảnh báo: Rất thô, và làm việc cơ bản dở dang)
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;
}
}
Nguồn
2010-07-08 08:45:15
Bạn nên giới thiệu triển khai ngày của bên thứ ba nào? – Jla
Tôi không có số ngày phức tạp trong ứng dụng của mình, và do đó không quan tâm đến bất kỳ thư viện DateTime nào cho GWT. Tôi biết có một tiện ích GwtDateTime (http://code.google.com/p/gwt-examples/wiki/gwtDateTime), nhưng không thử nghiệm nó, không thể xác nhận cho nó. YMMV –
Chúng không hoạt động trong các bài kiểm tra đơn vị: (trừ khi bạn mở rộng GWTTestCase chậm) –