将字符串"8:00"转换为分钟(整数值)

Kla*_*sos 0 java csv string integer

我正在从CSV文件中读取数据.其中一个字段是格式为H:mm的时间,即"8:00".如何将此字符串值转换为分钟(整数值),即8:00 = 8*60 = 480分钟?

String csvFilename = "test.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] row = null;
csvReader.readNext(); // to skip the headers
int i = 0;
while((row = csvReader.readNext()) != null) {
    int open = Integer.parseInt(row[0]);
}
csvReader.close();
Run Code Online (Sandbox Code Playgroud)

Mic*_*hal 6

您可以使用java.text.SimpleDateFormat转换StringDate.然后java.util.Calendar提取小时和分钟.

Calendar cal = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date = sdf.parse("8:00");
cal.setTime(date);

int mins = cal.get(Calendar.HOUR)*60 + cal.get(Calendar.MINUTE);
Run Code Online (Sandbox Code Playgroud)