Difference in days between two dates in Java?

后端 未结 19 1757
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:26

I need to find the number of days between two dates: one is from a report and one is the current date. My snippet:

  int age=calculateDiffer         


        
19条回答
  •  悲哀的现实
    2020-11-22 09:02

    I might be too late to join the game but what the heck huh? :)

    Do you think this is a threading issue? How are you using the output of this method for example? OR

    Can we change your code to do something as simple as:

    Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.set();
        calendar2.set();
        long milliseconds1 = calendar1.getTimeInMillis();
        long milliseconds2 = calendar2.getTimeInMillis();
        long diff = milliseconds2 - milliseconds1;
        long diffSeconds = diff / 1000;
        long diffMinutes = diff / (60 * 1000);
        long diffHours = diff / (60 * 60 * 1000);
        long diffDays = diff / (24 * 60 * 60 * 1000);
        System.out.println("\nThe Date Different Example");
        System.out.println("Time in milliseconds: " + diff
     + " milliseconds.");
        System.out.println("Time in seconds: " + diffSeconds
     + " seconds.");
        System.out.println("Time in minutes: " + diffMinutes 
    + " minutes.");
        System.out.println("Time in hours: " + diffHours 
    + " hours.");
        System.out.println("Time in days: " + diffDays 
    + " days.");
      }
    

提交回复
热议问题