如何在Java中将标准日期转换为分钟?
问题描述:
如何将标准的日期输出转换为分钟数?
How to convert standard output of date into number of minutes?
我的命令输出如下:
2013年3月4日星期一12:33:58
Mon Mar 4 12:33:58 2013
我需要将其转换为数字的分钟数表示 minutes1
。
and I need to convert it into number of minutes say minutes1
.
因为我有一个给出当前时间的分钟数的代码,该代码是:
because I have a code which gives number of minutes for current time and the code is:
public class DateToMinutes {
public static void main(String[] args) {
Date date = new Date();
long now = ((date.getTime()) / (60000));
System.out.println("Total Minutes are " + now);
}
}
这将在分钟内说出 minutes2
。
我需要比较两个 minutes1
和 minutes2
。
因为我无法将标准日期转换为 minutes1
,现在不可能。
since I am unable to convert Standard date into minutes1
, it's not possible right now.
答
看看SimpleDateFormat的解析方法,在那里会找到用于转换日期的格式。
look at parse method of SimpleDateFormat, there you'll find formats used for converting dates.
在您的情况下,这样的方法应该起作用:
In your case something like this should work:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");
Date theDate = sdf.parse ("Mon Mar 4 12:33:58 2013");
long minutes2 = theDate.getTime() / 60000;