-2

I have got a date in this format: 2015-07-29 16:29:32

How can I check the difference in minutes between the current date and the given date?

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {
    public static void main(String[] args) throws ParseException {
        String datetocomparestr = "2015-07-29 16:29:32";
        SimpleDateFormat datetocomparesdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
        Date d1 = null;
          d1 = datetocomparesdf.parse(datetocomparestr);
          System.out.println(d1);
          SimpleDateFormat dateFormatcurrentsdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
          Date date = new Date();
          System.out.println(dateFormatcurrentsdf.format(date));

    }
}

Could you please let me know how to resolve this. Thanks in advance.

NRKirby
  • 1,584
  • 4
  • 21
  • 38
Pawan
  • 31,545
  • 102
  • 256
  • 434

5 Answers5

2

A simple solution would be to use the Date.getTime() method (returns "milliseconds since 1.1.1970"), get the difference of these values and divide that value by 1000 * 60 (1000 ms per second, 60 seconds per minute).

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
1

Why so complicated? Just use the long representation of java to determine the time:

long timeToComp = parseSomeTime().getTime();
long timeCurrent = System.currentTimeMillis();

long dif = timeCurrent - timeToComp;
long mins = dif / (1000 * 60);//dif is the number of milliseconds between the current date and the parsed one
1

you can use localdatetime as below

LocalDateTime d1=new LocalDateTime(date1);
LocalDateTime d2=new LocalDateTime(now);

int minutesDiff=Minutes.minutesBetween(d1, d2).getMinutes();
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0
double minutes = (date.getTime() - d1.getTime()) / (1000 * 60);
Simimmo
  • 658
  • 1
  • 6
  • 15
0

Date#getTime() in Java returns the time in milliseconds.

Add the following to your code.

 long minutesDiff = (date.getTime() - d1.getTime()) / 1000 / 60;    //convert to minutes
 System.out.println("Difference in minutes : " + minutesDiff);

Check out this live demo.

Here is the complete code.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {
    public static void main(String[] args) throws ParseException {
        String datetocomparestr = "2015-07-29 16:29:32";
        SimpleDateFormat datetocomparesdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");  
        Date d1 = null;
        d1 = datetocomparesdf.parse(datetocomparestr);
        System.out.println(d1);
        SimpleDateFormat dateFormatcurrentsdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormatcurrentsdf.format(date));

        long minutesDiff = (date.getTime() - d1.getTime()) / 1000 / 60;
        System.out.println("Difference in minutes : " + minutesDiff);

    }
}
Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33