Java hh:mm格式的字符串
我有两个hh:mm字符串,我想对它们进行比较 我的意思是,我想加减它们,进行运算。我有一个带当前时间的字符串和另一个写着“15:00”的字符串。我想知道两个字符串之间有多少分钟,我可以通过做减法得到的结果。Java hh:mm格式的字符串,java,string,methods,time,hour,Java,String,Methods,Time,Hour,我有两个hh:mm字符串,我想对它们进行比较 我的意思是,我想加减它们,进行运算。我有一个带当前时间的字符串和另一个写着“15:00”的字符串。我想知道两个字符串之间有多少分钟,我可以通过做减法得到的结果。 这可能吗?这会让你开始。。对这两个字符串执行以下操作&然后执行必要的比较/加/减 String time1 = "15:00"; String time2 = "16:00"; DateFormat sdf = new SimpleDateFormat("hh:mm"); Date dat
这可能吗?这会让你开始。。对这两个字符串执行以下操作&然后执行必要的比较/加/减
String time1 = "15:00";
String time2 = "16:00";
DateFormat sdf = new SimpleDateFormat("hh:mm");
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
long milliSecondsDiff = date1.getTime() - date2.getTime(); //in milliseconds
int seconds = milliSecondsDiff/1000; //seconds
int minutes = seconds/60; //minutes
这会让你开始。。对这两个字符串执行以下操作&然后执行必要的比较/加/减
String time1 = "15:00";
String time2 = "16:00";
DateFormat sdf = new SimpleDateFormat("hh:mm");
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
long milliSecondsDiff = date1.getTime() - date2.getTime(); //in milliseconds
int seconds = milliSecondsDiff/1000; //seconds
int minutes = seconds/60; //minutes
您可以使用
simpleDataFormat
解析它,然后使用Date
类中的getTime()
方法以毫秒为单位获取差值
DateFormat f = new SimpleDateFormat("hh:mm");
Date d1 = f.parse(s1);
Date d2 = f.parse(s2);
long difference = d1.getTime() - d2.getTime(); // milliseconds
您可以使用
simpleDataFormat
解析它,然后使用Date
类中的getTime()
方法以毫秒为单位获取差值
DateFormat f = new SimpleDateFormat("hh:mm");
Date d1 = f.parse(s1);
Date d2 = f.parse(s2);
long difference = d1.getTime() - d2.getTime(); // milliseconds
您可以使用joda datetime来执行以下操作:
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
DateTime first = formatter.parseDateTime("15:00");
DateTime second = formatter.parseDateTime("15:49");
Interval interval = new Interval(first, second);
System.err.println(interval.toDuration().getStandardMinutes());
}
看一看
使用Duration还提供了一些其他简洁的方法,如getStandardSeconds(),在这种情况下,它将为您提供2940。您可以使用joda datetime来实现此目的:
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
DateTime first = formatter.parseDateTime("15:00");
DateTime second = formatter.parseDateTime("15:49");
Interval interval = new Interval(first, second);
System.err.println(interval.toDuration().getStandardMinutes());
}
看一看
使用Duration还提供了一些其他简洁的方法,如getStandardSeconds(),在这种情况下,它将为您提供2940。是的,这是可能的。然而,网上有很多资源可以帮助你做到这一点。问答网站也是如此。没有教程请求。对不起,如果我很粗鲁,但这是真的,转换成分钟,然后你可以很容易地添加/减除/比较它是可能的。然而,网上有很多资源可以帮助你做到这一点。问答网站也是如此。没有教程请求。抱歉,如果我很粗鲁,但这是一个真正的线程,可以转换为分钟,然后你可以很容易地添加/减去/比较你可以使用
a.getTime()-b.getTime()
以毫秒为单位计算差值。@PeterLawrey绝对。。不想在OP发布一些代码之前就把它泄露出去。。感谢您可以使用a.getTime()-b.getTime()
以毫秒为单位获得差值。@PeterLawrey绝对。。不想在OP发布一些代码之前就把它泄露出去。。谢谢