2009-01-07 17 views
0

我正在使用Jfreechart。我有這樣的代碼:如何將字符串轉換爲Java中的RegularTimePeriod?

TimeSeries t1 = new TimeSeries("EUR/GBP"); 
t1.add(new Day(4, MonthConstants.JANUARY, 2001), new Double(1.5807)); 

但我從我的SQL查詢得到String。 TimeSeries只接受RegularTimePeriodTimeSeriesDataItem

請讓我知道如何將String轉換爲RegularTimePeriod

在此先感謝。

+0

哪個字符串?字符串是什麼樣的? – 2009-01-07 16:42:41

回答

3

首先,您可以通過使用SimpleDateFormat解析您的mysql日期字符串來獲取Date對象,然後使用具有Date arg的構造函數創建您的RegularTimePeriod。

基本上(假設mysqlDateStr是從MySQL查詢您的字符串):

SimpleDateFormat standardDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
// (Define your formatter only once, then reuse) 

Date myDate = standardDateFormat.parse(mysqlDateStr); 
// (you may want to catch a ParseException) 

t1.add(new Day(myDate), new Double(1.5807)); 
+0

當然,你將不得不改變字符串在簡單的日期格式來匹配日期。 – Eldelshell 2009-01-09 10:40:24

相關問題