将时间值为“0:00”的字符串转换为 decimal/double 值?
Convert String with Time Value "0:00" into decimal/double value?
我正在尝试从 table 中检索数据并将它们转换为双精度值以便绘制它们。第 0 列填充格式为“00:00”的时间值,第 1 列填充小数值。
使用这段代码,我似乎已经正确检索了数据并将第 1 列的内容转换为双精度值,但不知道如何将具有时间值的字符串转换为双精度值以表示小时数:
int row = tableRadio.getRowCount();
for (int r = 0; r < row; r++) {
double r2 = Double.parseDouble( tableModel.getValueAt(r, 0).toString() );
double r3 = Double.parseDouble( tableModel.getValueAt(r, 1).toString() );
series.add(r2, r3);
}
我不断收到 java.lang.NumberFormatException: For input string: "0:00" 试图绘制此数据时,据我了解,这是因为冒号不能等同于数值。但是我怎么能说“00:21”而不是显示为“0.21”或我可以用来绘制数据图表的东西呢?欢迎任何建议和示例。
使用SimpleDateFormat
to parse()
the time column and create a time series chart, as shown . Use setDateFormatOverride()
on the resulting DateAxis
to format the time, as shown . If necessary, you can change the DateFormatSymbols
, as shown here.
所以我使用@Dawood ibn Kareem 的方法让它工作。这是对我有用的最终代码:
//get number of rows
int row = tableRadio.getRowCount();
for (int r = 0; r < row; r++) {
//get column0's values and convert to String
String time = ( tableModel.getValueAt(r, 0).toString() );
//split time into two arrays
String[] timeSplit = time.split(":");
//convert arrays to doubles
double hours = Double.parseDouble(timeSplit[0]);
double min = Double.parseDouble(timeSplit[1]);
//represent column0 values by number of hours
double col0 = (min / 60) + hours;
//get column1's values and convert to double
double col1 = Double.parseDouble( tableModel.getValueAt(r, 1).toString() );
//add values to series
series.add(col0, col1);
}//end of for loop`
现在所有 table 值都转换为双精度值,并且我已成功将它们绘制成图表。
我正在尝试从 table 中检索数据并将它们转换为双精度值以便绘制它们。第 0 列填充格式为“00:00”的时间值,第 1 列填充小数值。
使用这段代码,我似乎已经正确检索了数据并将第 1 列的内容转换为双精度值,但不知道如何将具有时间值的字符串转换为双精度值以表示小时数:
int row = tableRadio.getRowCount();
for (int r = 0; r < row; r++) {
double r2 = Double.parseDouble( tableModel.getValueAt(r, 0).toString() );
double r3 = Double.parseDouble( tableModel.getValueAt(r, 1).toString() );
series.add(r2, r3);
}
我不断收到 java.lang.NumberFormatException: For input string: "0:00" 试图绘制此数据时,据我了解,这是因为冒号不能等同于数值。但是我怎么能说“00:21”而不是显示为“0.21”或我可以用来绘制数据图表的东西呢?欢迎任何建议和示例。
使用SimpleDateFormat
to parse()
the time column and create a time series chart, as shown setDateFormatOverride()
on the resulting DateAxis
to format the time, as shown DateFormatSymbols
, as shown here.
所以我使用@Dawood ibn Kareem 的方法让它工作。这是对我有用的最终代码:
//get number of rows
int row = tableRadio.getRowCount();
for (int r = 0; r < row; r++) {
//get column0's values and convert to String
String time = ( tableModel.getValueAt(r, 0).toString() );
//split time into two arrays
String[] timeSplit = time.split(":");
//convert arrays to doubles
double hours = Double.parseDouble(timeSplit[0]);
double min = Double.parseDouble(timeSplit[1]);
//represent column0 values by number of hours
double col0 = (min / 60) + hours;
//get column1's values and convert to double
double col1 = Double.parseDouble( tableModel.getValueAt(r, 1).toString() );
//add values to series
series.add(col0, col1);
}//end of for loop`
现在所有 table 值都转换为双精度值,并且我已成功将它们绘制成图表。