在GWT上将String转换为Date

use*_*796 9 gwt

如何使用GWT 2.0.3将String转换为Date或Date转换为String?

Hil*_*amp 10

使用java.util.Date类本身的方法:parse(String s)toString().虽然第一个被弃用,但它是与GWT一起使用的方法.如果您想要更好地控制Date的格式,请在转换为String时使用GWT特定的类:com.google.gwt.i18n.client.DateTimeFormat.它将格式化给定模式的日期,类似于Java自己的日期SimpleDateFormat.


Mak*_*ski 7

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;
}
Run Code Online (Sandbox Code Playgroud)