希望找到两次之间的差异 我有两次 12:16:40 和 12:16:50 。 我希望差异为 10秒如果差异以分钟为单位然后显示分钟的差异,或者如果差异以秒为单位则显示差异秒等等。
我想要这样的答案
2秒前
5分钟前
2小时前
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); // Hours:Minutes:Seconds
String dateFormatted = formatter.format(date);
答案 0 :(得分:0)
您好,您可以使用下面的代码这是我给您需要根据您的要求进行更改的示例
try {
Calendar c2 = Calendar.getInstance();
SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss aa",Locale.US);
String formattedTime = df2.format(c2.getTime());
date2 = df2.parse(formattedTime);
long mills = date2.getTime() - date1.getTime();
int Hours = (int) (mills/(1000 * 60 * 60));
int Minuets = (int) (mills/(1000*60)) % 60;
int Seconds = (int) (mills/1000) % 60;
String hr = String.format("%02d", Hours);
String min = String.format("%02d", Minuets);
String sec = String.format("%02d", Seconds);
endTime = hr + ":" + min + ":" +sec; // updated value every1 second
} catch (ParseException e) {
e.printStackTrace();
}
因此,对于您的问题,请使用以下函数来获取答案并导入以下字段
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
private void cluclate()
{
Date date1,date2;
try {
String endTime;
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.HOUR,12);
c1.add(Calendar.MINUTE,16);
c1.add(Calendar.SECOND,40);
SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss", Locale.US);
String formattedTime1 = df1.format(c1.getTime());
date1 = df1.parse(formattedTime1);
Calendar c2 = Calendar.getInstance();
c2.add(Calendar.HOUR,12);
c2.add(Calendar.MINUTE,16);
c2.add(Calendar.SECOND,50);
SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss",Locale.US);
String formattedTime2 = df2.format(c2.getTime());
date2 = df2.parse(formattedTime2);
long mills = date2.getTime() - date1.getTime();
int Hours = (int) (mills/(1000 * 60 * 60));
int Minuets = (int) (mills/(1000*60)) % 60;
int Seconds = (int) (mills/1000) % 60;
String hr = String.format("%02d", Hours);
String min = String.format("%02d", Minuets);
String sec = String.format("%02d", Seconds);
endTime = hr + ":" + min + ":" +sec; // updated value every1 second
Log.d("=============>>>>",endTime);
} catch (ParseException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
试试这个:
long firstTime = System.currentTimeMillis();
/*
your operations should do here
*/
long secondTime = System.currentTimeMillis();
long difference = secondTime - firstTime;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(difference);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.GERMANY);
String yourDate = simpleDateFormat.format(calendar);
答案 2 :(得分:0)
在runnable中使用以下代码,以便继续检查。
Calendar c2 = Calendar.getInstance();
SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss aa",Locale.US);
String formattedTime = df2.format(c2.getTime());
date2 = df2.parse(formattedTime);
//date1 is the first time, date2 is current time.
long mills = date2.getTime() - date1.getTime();
int Hours = (int) (mills/(1000 * 60 * 60));
int Minuets = (int) (mills/(1000*60)) % 60;
int Seconds = (int) (mills/1000) % 60;
if(Hours == 0) {
if(Minutes == 0) {
displayString = Seconds.toString() + " ago";
} else {
displayString = Minutes.toString() + " ago";
}
} else {
displayString = Hours.toString() + " ago";
}
答案 3 :(得分:0)
使用DateUtils类可以轻松实现。
<强> getRelativeTimeSpanString(long time, long now, long minResolution) 强>
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 12);
time.set(Calendar.MINUTE, 16);
time.set(Calendar.SECOND, 40);
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR, 12);
now.set(Calendar.MINUTE, 16);
now.set(Calendar.SECOND, 50);
System.out.println(DateUtils.getRelativeTimeSpanString(time.getTimeInMillis(), now.getTimeInMillis(), DateUtils.SECOND_IN_MILLIS));
如果时差以分钟或小时为单位,这也是有效的。