2010-08-22 103 views

回答

10

使用java.util.Date類本身的方法:parse(String s)toString()。雖然第一個已被棄用,但它是與GWT一起使用的方法。如果您想更好地控制Date的格式,轉換爲String時使用GWT特定類:com.google.gwt.i18n.client.DateTimeFormat。它會根據模式格式化日期,類似於Java自己的SimpleDateFormat

4
import com.google.gwt.i18n.shared.DateTimeFormat; 
import java.util.Date; 

... 

public Date getDate(String dateString) { 
    Date result = null; 
    try { 
     DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd"); 
     result = dateTimeFormat.parse(dateString); 
    } catch (Exception e) 
    { 
     // ignore 
    } 
    return result; 
} 
1

要轉換日期的StringFormat

import com.google.gwt.i18n.shared.DateTimeFormat; 
    import java.util.Date; 

    ... 
    public String getStringFormat(Date inputDate){ 
    String strFormat = null; 
    try{ 
    DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("DD-MMM-YYYY"); 
    strFormat = dateTimeFormat.format(inputDate); 
    }catch(Exception e){ 
    e.printStackTrace(); 
    } 
    return strFormat; 
    }