Ahmad Alkhatib
Ahmad Alkhatib

Reputation: 1226

How to get day,hours,minutes to reach a certain date

I need to get Day Hours Minutes to reach certain date example :

Date = "14-08-2015 16:38:28"

Current_Date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());

and to reach that Date the result will be 2 Days and 1 hour and 30 minutes

Upvotes: 2

Views: 2109

Answers (2)

Smudoo
Smudoo

Reputation: 547

You could use:

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);

There are plenty of constants in Calendar for everything you need. Edit: Calendar class documentation

Upvotes: 2

N J
N J

Reputation: 27515

Simple searching on google I got

    String dateStart = "01/14/2012 09:29:58";
    String dateStop = "01/15/2012 10:31:48";

    //HH converts hour in 24 hours format (0-23), day calculation
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    Date d1 = null;
    Date d2 = null;

    try {
        d1 = format.parse(dateStart);
        d2 = format.parse(dateStop);

        //in milliseconds
        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);

        System.out.print(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");

    } catch (Exception e) {
        e.printStackTrace();
    }

see below links

  1. http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/
  2. How to find the duration of difference between two dates in java?
  3. Calculate date/time difference in java

Upvotes: 9

Related Questions