我在gwt appliation中使用了一个运动图。我的问题是如何从字符串创建一个Date对象?以下是我的代码:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.MotionChart;
import com.google.gwt.visualization.client.visualizations.MotionChart.Options;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import component.client.SQLRunner;
import component.client.SQLRunnerAsync;
public class DashboardWidget
{
private HorizontalPanel containerPanel=null;
private DataTable data=null;
public DashboardWidget()
{
containerPanel = new HorizontalPanel();
}
public HorizontalPanel getContainerPanel()
{
SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
AsyncCallback<ArrayList<String[]>> callback = new AsyncCallback<ArrayList<String[]>>()
{
@Override
public void onFailure(Throwable caught)
{
}
@Override
public void onSuccess(final ArrayList<String[]> result)
{
Runnable onLoadCallback = new Runnable()
{
public void run()
{
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, " Name");
data.addColumn(ColumnType.DATE, "Date");
data.addColumn(ColumnType.NUMBER, "Count");
data.addColumn(ColumnType.NUMBER, "Something");
data.addColumn(ColumnType.NUMBER, "Something Else");
data.addRows(result.size());
@**SuppressWarnings("deprecation")
Date date = new Date(1888,06,01);
DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy,MM,dd");**
for(int i=0;i<result.size();i++)
{
String[] temp = result.get(i);
String name=temp[0];//name
String count= temp[2];
String something=temp[3];
String seomthingElse=temp[5];
data.setValue(i, 0, name);//NAME
data.setValue(i, 1, date);//date
data.setValue(i, 2, Integer.parseInt(count));//eloc
data.setValue(i, 3, Integer.parseInt(something));//warning
data.setValue(i, 4, Integer.parseInt(somethingeElse));//static (open) warning
}
Options options = Options.create();
options.setWidth(1000);
options.setHeight(1000);
MotionChart chart = new MotionChart(data, options);
containerPanel.add(chart);
}
};
VisualizationUtils.loadVisualizationApi(onLoadCallback, MotionChart.PACKAGE);
}
};
service.getDataFromDatabase("","","","", callback);
return containerPanel;
}
}
那么如何从String为GWT应用程序创建日期对象?
答案 0 :(得分:3)
你能不能使用DateTimeFormat对象?像这样:
Date parsed = fmt.parse(text);