battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen their skills
 
HomeGallerySearchRegisterLog in
 

 [java] time ago

Go down 
AuthorMessage
kurosen
codding
codding
kurosen

Posts : 293
Join date : 2012-04-17

[java] time ago Empty
PostSubject: [java] time ago   [java] time ago EmptyMon Nov 23, 2020 12:59 pm

Code:
package com.mkyong.dateUtils;

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

public class DateTimeUtils {

    public static void main(String[] args) {
       
      DateTimeUtils obj = new DateTimeUtils();
      SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

      try {
           
        Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
        Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");
           
        obj.printDifference(date1, date2);
           
      } catch (ParseException e) {
        e.printStackTrace();
      }
       
    }
   
    //1 minute = 60 seconds
    //1 hour = 60 x 60 = 3600
    //1 day = 3600 x 24 = 86400
    public void printDifference(Date startDate, Date endDate){
   
        //milliseconds
        long different = endDate.getTime() - startDate.getTime();
       
        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);
       
        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;
       
        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;
       
        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;
       
        long elapsedSeconds = different / secondsInMilli;
       
        System.out.printf(
            "%d days, %d hours, %d minutes, %d seconds%n",
            elapsedDays,
            elapsedHours, elapsedMinutes, elapsedSeconds);
   
    }
   
}
battle programming
Back to top Go down
 
[java] time ago
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: