我需要显示倒计时时钟,我计算了以毫秒为单位的日期之间的差异,如何将其转换为倒计时时钟,即“ 20days 12hrs 12min remaining ”?我可以留下来。
(mili / 24 * 60 * 60 * 1000)
但这是剩余的总时数我怎样才能将其转换为0-24小时0-60分钟的格式?
答案 0 :(得分:3)
//setting time
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE, 31);
cal2.set(Calendar.HOUR_OF_DAY, 1);
cal2.set(Calendar.MINUTE, 50);
//printing dates
System.out.println("Date1"+ cal1.getTime());
System.out.println("Date1"+ cal2.getTime());
long diffInMillis = cal2.getTimeInMillis() - cal1.getTimeInMillis();
System.out.println("Diff In Millis : " + diffInMillis);
int hour = (int)(diffInMillis/(60.0 * 1000 * 60 ));
int min = (int)((diffInMillis - (hour *60.0 *1000 *60))/(60.0*1000));
System.out.println("Diff In HH:MM: " + hour + ":" + min );
答案 1 :(得分:0)
您可以使用标准java.util.Calendar
类。
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(yourTime);
int hour = cal.get(Calendar.HOURS_IN_DAYS);