转换字符串“8:00”进入分钟(整数值) [英] Convert the string "8:00" into the minutes (integer value)

查看:259
本文介绍了转换字符串“8:00”进入分钟(整数值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm reading the data from CSV file. One of the fields is the time in the format H:mm, i.e. "8:00". How to convert this string value into the minutes (integer value), i.e. 8:00 = 8*60 = 480 minutes?

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();


推荐答案

您可以使用 java。 text.SimpleDateFormat String 转换为日期。然后 java.util.Calendar 提取小时和分钟。

You can use java.text.SimpleDateFormat to convert String to Date. And then java.util.Calendar to extract hours and minutes.

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);

这篇关于转换字符串“8:00”进入分钟(整数值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆