我正在制作一个警报应用程序,该应用程序应该能够显示直到下一次警报的时间。我可以使用当前时间,小时:分钟:AM / PM值以及星期几,例如星期三凌晨3:15。我该如何计算?我唯一感兴趣的是像这样打印:07h 35m。
我所做的是,我试图将其转换为日期,然后将其转换回毫,然后进行一些数学运算,但是它不起作用,我一直得到错误的结果。
编辑:
我的问题是,我的时间由我的自定义视图设置,例如星期二凌晨3:15。我需要将其转换为最近的Date对象。例如,今天是星期二,就是凌晨2:00,我将只编辑时间,但是今天是星期五,我需要添加一些日期和时间。
答案 0 :(得分:1)
尝试一下:
SimpleDateFormat format = new SimpleDateFormat("hh:mm a EEE", Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(format.parse("3:15 AM Tue").getTime());
Calendar now = Calendar.getInstance();
Calendar target = Calendar.getInstance();
if (calendar.get(Calendar.DAY_OF_WEEK) > now.get(Calendar.DAY_OF_WEEK)) {
target.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
target.add(Calendar.DATE, calendar.get(Calendar.DAY_OF_WEEK) - now.get(Calendar.DAY_OF_WEEK));
} else if (calendar.get(Calendar.DAY_OF_WEEK) < now.get(Calendar.DAY_OF_WEEK)) {
target.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
target.add(Calendar.DATE, 7 - (now.get(Calendar.DAY_OF_WEEK) - calendar.get(Calendar.DAY_OF_WEEK)));
} else {
if (calendar.get(Calendar.HOUR_OF_DAY) > now.get(Calendar.HOUR_OF_DAY)) {
target.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
} else if (calendar.get(Calendar.HOUR_OF_DAY) < now.get(Calendar.HOUR_OF_DAY)) {
target.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
target.add(Calendar.DATE, 7);
} else {
if (calendar.get(Calendar.MINUTE) > now.get(Calendar.MINUTE)) {
target.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
} else if (calendar.get(Calendar.MINUTE) < now.get(Calendar.MINUTE)) {
target.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
target.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
target.add(Calendar.DATE, 7);
} else {
// target time is now!
}
}
}
long remainingTime = target.getTimeInMillis() - now.getTimeInMillis();
final long hr = TimeUnit.MILLISECONDS.toHours(remainingTime);
final long min = TimeUnit.MILLISECONDS.toMinutes(remainingTime - TimeUnit.HOURS.toMillis(hr));
String result = String.format("%02d:%02d", hr, min);
答案 1 :(得分:0)
要将这样的字符串转换为实际的日期对象,可以使用如下代码:
String str = "Fri Mar 1, 2013 4:30 PM";
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd, yyyy hh:mm a");
Date date = sdf1.parse(str);
System.out.println("Date Object:" + date);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
System.out.println("Formatted Date:" + sdf2.format(date));
编辑:
因此,从编辑开始,如果我正确理解了您的要求,则希望获得两个日期之间的时差(可能相隔几天),并希望以小时为单位显示它们之间的时间。
为此,您可以使用如下代码:
//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
}
答案 2 :(得分:0)
与毫厘一起工作应该毫不费力。请记住,1000毫秒= 1秒。进行数学运算,然后使用SimpleDateFormat
将结果转换为所需的格式。另外,在使用dates
时,请确保您导入的是java.util.Date
而不是sql.Date
。
PS:如何从字符串中获取日期:
String TimeStringToConvertToActualDate; // Let's assume this is the string holding your time and you want to convert it to a date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Here you should write the format you are actually using instead of this one
Date myDate = null;
try {
myDate= sdf.parse(TimeStringToConvertToActualDate);
} catch (ParseException e) {
e.printStackTrace();
}
答案 3 :(得分:0)