public int difftime(String string, String string2) {
int hours;
int min = 0;
int days;
long difference ;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date date1 = simpleDateFormat.parse("08:00 AM");
Date date2 = simpleDateFormat.parse("04:00 PM");
difference = date2.getTime() - date1.getTime();
days = (int) (difference / (1000 * 60 * 60 * 24));
hours = (int) ((difference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
min = (int) (difference - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))
/ (1000 * 60);
hours = (hours < 0 ? -hours : hours);
Log.i("======= Hours", " :: " + hours);
return min;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return min;
}
这是我以分钟的形式获得时差的功能,但总是为零。我不知道我错在哪里请告诉我哪里出错了。 我希望以分钟的形式显示结果。
答案 0 :(得分:3)
首先,你做了一个非常基本的错误。 对于12小时制,你应该使用hh而不是HH 。而且我很惊讶地看到,没有一个答案纠正了这个错误。
其次,你不是在考虑日期,而是仅仅依赖于时间。 所以08:00 AM和04:00 PM没有任何细微差别。它只有8小时的差异。
所以,现在在你的情况下,你必须根据两个场景来计算分钟数,一个来自小时,一个来自分钟差异。我更正你的代码。请检查一下,因为这在我结束时按预期工作。
public long diffTime() {
long min = 0;
long difference ;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa"); // for 12-hour system, hh should be used instead of HH
// There is no minute different between the two, only 8 hours difference. We are not considering Date, So minute will always remain 0
Date date1 = simpleDateFormat.parse("08:00 AM");
Date date2 = simpleDateFormat.parse("04:00 PM");
difference = (date2.getTime() - date1.getTime()) / 1000;
long hours = difference % (24 * 3600) / 3600; // Calculating Hours
long minute = difference % 3600 / 60; // Calculating minutes if there is any minutes difference
min = minute + (hours * 60); // This will be our final minutes. Multiplying by 60 as 1 hour contains 60 mins
} catch (Throwable e) {
e.printStackTrace();
}
return min;
}
答案 1 :(得分:0)
一个简单的例子希望它可以帮助你:)
SimpleDateFormat dfDate = new SimpleDateFormat("HH:mm a");
java.util.Date d = null;
java.util.Date d1 = null;
Calendar cal = Calendar.getInstance();
try {
d = dfDate.parse("08:00 AM");
d1 = dfDate.parse("04:00 PM");
} catch (java.text.ParseException e) {
e.printStackTrace();
}
long diffInTime = (long) ((d.getTime() - d1.getTime())/ (1000 * 60 * 60 *
24));
long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInTime); <--- converting
the result in minutes
System.out.println(minutes);
答案 2 :(得分:0)
Mulitply需要24小时才能获得小时数而不是MINUTES
1&GT;解析异常
2 - ;你没有在 MINUTE 中获得好处,所以你需要改变
改变,
hours = (int) ((difference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));
要
hours = (int) ((difference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60 * 24));
检查
public int difftime() {
int hours;
int min = 0;
int days;
long difference;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date date1 = simpleDateFormat.parse("08:00 AM");
Date date2 = simpleDateFormat.parse("04:00 PM");
difference = date2.getTime() - date1.getTime();
days = (int) (difference / (1000 * 60 * 60 * 24));
hours = (int) ((difference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60 * 24));
min = (int) (difference - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours)) / (1000 * 60);
hours = (hours < 0 ? -hours : hours);
Log.e("======= Hours", " :: " + hours);
Log.e("======= min", " :: " + min);
return min;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return min;
}
答案 3 :(得分:0)
尝试在java中使用TimeUnit类。这会将您收到的Millis中的长值转换为小时和分钟。
int hours;
int min;
long difference ;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date date1 = simpleDateFormat.parse("08:00 AM");
Date date2 = simpleDateFormat.parse("04:00 PM");
difference = date2.getTime() - date1.getTime();
hours = Long.valueOf(TimeUnit.MILLISECONDS.toHours(difference)).intValue();
min = Long.valueOf(TimeUnit.MILLISECONDS.toMinutes(difference)).intValue();
hours = (hours < 0 ? -hours : hours);
Log.i("======= Hours", " :: " + hours);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 4 :(得分:0)
您可以尝试执行此操作。 在此示例中,我计算小时和分钟之间的时差,而忽略日和年字段。
public static Date getDate(int hour, int minute) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 0);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 0);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static int getDiff(Date date1, Date date2){
/* this function get the difference between date1 and date2
* represented in minutes
* assume that the formate is HH:MM */
int min = 0;
int hr = 0;
if (date1.getMinutes() >= date2.getMinutes()){
min = date1.getMinutes() - date2.getMinutes();
}else{
min = date1.getMinutes() + ( 60 - date2.getMinutes());
hr = -1;
}
if ((date1.getHours()+hr) >= date2.getHours()){
hr += date1.getHours() - date2.getHours();
}else{
hr += date1.getHours() + (24 - date2.getHours());
}
return min + (hr * 60);
}
第一个创建日期的函数,第二个获得差值
然后您将获得如下所示的区别
Date currentTime = Calendar.getInstance().getTime();
Date secondTime = getDate(11,5); this will be 11:05
int diff = getDiff(secondTime ,currentTime);
diff将代表从当前时间到到达第二时间之间所花费的分钟数。
答案 5 :(得分:-1)
这对我有用,我有两个textview,我正在使用timepicker来设置时间
case R.id.starttime_activityentry:
mcurrentTime = Calendar.getInstance();
hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
minutes = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(ActivityEntryDetail.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
starttime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minutes, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
break;
case R.id.endtime_activityentry:
endmcurrentTime = Calendar.getInstance();
endhour = endmcurrentTime.get(Calendar.HOUR_OF_DAY);
endminute = endmcurrentTime.get(Calendar.MINUTE);
TimePickerDialog endmTimePicker;
endmTimePicker = new TimePickerDialog(ActivityEntryDetail.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
endtime.setText( selectedHour + ":" + selectedMinute);
}
}, endhour, endminute, true);//Yes 24 hour time
endmTimePicker.setTitle("Select Time");
然后按一个按钮显示差异
case R.id.durations:
hourdifs();
break;
调用此方法
public static void hourdifs()
{
try {
String sttime= starttime.getText().toString();
System.out.println("sttime before parse DATE"+sttime);
String endtm= endtime.getText().toString();
System.out.println("endtmie before parse DATE"+endtm);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date date1 = simpleDateFormat.parse(sttime);
Date date2 = simpleDateFormat.parse(endtm);
long difference = date2.getTime() - date1.getTime();
int days = (int) (difference / (1000*60*60*24));
int hours = (int) ((difference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60 * 24));
int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
hours = (hours < 0 ? -hours : hours);
Log.i("=== Hours"," :: "+hours+":"+min);
}
catch (Exception j){
j.printStackTrace();
}
}