我试图创建一个函数,它以 2013-11-05T16:44:54.128515 的形式获取 utc 时间并获取当前的 utc 时间并进行比较。如果少于 24 小时,它应该返回小时的差异,如果超过 24 小时,它应该返回天数
public String time_parser(String time)
{
Date d1 = null;
Date d2 = null;
// DateFormat df = DateFormat.getTimeInstance();
//df.setTimeZone(TimeZone.getTimeZone("utc"));
//String gmtTime = df.format(new Date());
// Log.d("response ok", "TIME TIME TIME " + gmtTime);
String timesince="";
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());
try{
d1=sdf.parse(time);
d2= sdf.parse(utcTime);
long diff = d2.getTime()-d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
if(diffDays >1)
{
timesince = Long.toString(diffDays );
Log.d("response ok", "TIME= " + timesince);
}
else
{
timesince = Long.toString(diffHours);
Log.d("response ok", "TIME 2= " + timesince);
}
}
catch (Exception e) {
e.printStackTrace();
}
return timesince;
}