当前时间 - 最后时间(java)

时间:2015-07-31 10:43:57

标签: java android date

抱歉我的英文。我有当前时间2015-07-31 12:19:55和上次2015-07-31 13:38:20,我不知道如何获得日期差异。例如:

2015-07-31 12:19:55 - 2015-07-31 13:38:20 = date what i need

我的简单代码:

private Date date = new Date();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String currectDate = dateFormat.format(date);
    private String lastLoginDate = "2015-07-31 12:19:55";

1 个答案:

答案 0 :(得分:0)

for ( auto inner : *v )
{
    for ( char c : *inner ) std::cout << c;
    std::cout << std::endl;
}        

以天为单位?

    java.text.DateFormat outputFormat1 =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date d1 = outputFormat1.parse("2015-07-31 12:19:55");//start date
    Date d2 = outputFormat1.parse("2015-07-31 13:38:20");//end date
    long diff = d2.getTime() - d1.getTime();
    long diffHours = diff / (60 * 60 * 1000) % 24;//in terms of hours
    long diffmin = diff / (60 * 1000) % 60;//in mimutes
    long diffsec = diff / (1000) % 60;//in seconds
    String rtime=diffHours+"h:"+diffmin+"m:"+diffsec+"s";
    System.out.println(rtime);//1h:18m:25s

使用Period

  long diffDays = diff / (24 * 60 * 60 * 1000);//in terms of days