问题
I am using Jfreechart. I have the following code:
TimeSeries t1 = new TimeSeries("EUR/GBP");
t1.add(new TimeSeriesDataItem....);
But my SQL query gives date in String format & value in Double. I want to use TimeSeriesDataItem. Please let me know how to convert my String into TimeSeriesDataItem.
Please let me know how to add my Double value to TimeSeriesDataItem.
Thanks in Advance.
回答1:
1) convert your date from String to java.util.Date
2) wrap this Date instance using one of the classes extending RegularTimePeriod. eg. RegularTimePeriod p = new Day (myDate)
3) TimeSeriesDataItem t = new TimeSeriesDataItem (p, a_numeric_value)
回答2:
What is the format of the date string? Assuming the format is DD-MM-YY.
First convert the string to a Date object.
String date_S = "04-06-16"; //your date from SQL
Date date;
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yy");
try {
date = sdf2.parse(dateS);
} catch (ParseException e) {
e.printStackTrace();
}
TimeSeries add takes RegularTimePeriod and Double as arguments
So create a RegularTimePeriod object and add it to the series.
RegularTimePeriod rtp = new Date(date);
TimeSeries t1 = new TimeSeries("EUR/GBP");
TimeSeriesDataItem tsdi = new TimeSeriesDataItem(rtp , Double);
t1.add(tsdi);
来源:https://stackoverflow.com/questions/424264/how-to-convert-a-string-into-timeseriesdataitem