0

Sorry for my English. I have current time 2015-07-31 12:19:55 and last time 2015-07-31 13:38:20 and i don't know how to get date difference. Example:

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

My simple code:

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";
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
g7932223
  • 15
  • 2

1 Answers1

0
    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

in terms of days?

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

Use Period

Period period = new Period(d1, d2);
System.out.print(period.getYears() + " years, ");
System.out.print(period.getMonths() + " months, ");
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31