美文网首页
Android计算时间差

Android计算时间差

作者: RoomSpace | 来源:发表于2018-11-22 11:10 被阅读0次

public static long get_time_difference(String curtimeStr,String endtimeStr){

SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

long timeMillis = System.currentTimeMillis();

if(null==curtimeStr){

curtimeStr=stampToDate(timeMillis);

}

if(null==endtimeStr){

endtimeStr=stampToDate(timeMillis);

}

Log.e("tag timeMillis =",""+timeMillis);

//将时间戳转为日期格式

    Log.e("tag curTime =",""+curtimeStr);

Log.e("tag endTime =",""+endtimeStr);

Date curDate =null;

Date endDate=null;

try {

curDate = df.parse(curtimeStr);

endDate = df.parse(endtimeStr);

}catch (ParseException e) {

e.printStackTrace();

}

long diff = endDate.getTime() - curDate.getTime();

Log.i("tag","时间差="+diff);

//以天数为单位取整

    Long day = diff / (1000 *60 *60 *24);

//以小时为单位取整

    Long hour=(diff/(60*60*1000)-day*24);

//以分钟为单位取整

    Long min=((diff/(60*1000))-day*24*60-hour*60);

//以秒为单位

    Long second=(diff/1000-day*24*60*60-hour*60*60-min*60);

Log.e("tag","day =" +day);

Log.e("tag","hour =" +hour);

Log.e("tag","min =" +min);

Log.e("tag","second =" +second);

return diff;

}

/**

* 将时间戳转换为时间

*/

public static String stampToDate(long timeMillis){

SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date =new Date(timeMillis);

return simpleDateFormat.format(date);

}

相关文章

网友评论

      本文标题:Android计算时间差

      本文链接:https://www.haomeiwen.com/subject/yybkqqtx.html