Java Calendar calculation time difference and calendar time difference
- Public static void main (String [] args ){
- Calendar c = Calendar. getInstance ();
- Int y = 2016; // year
- Int M = 1; // month
- Int d = 1; // day
- Int H = 0; // hour
- Int m = 0; // minute
- Int s = 0; // second
- C. set (Calendar. YEAR, y );
- C. set (Calendar. MONTH, M-1 );
- C. set (Calendar. DATE, d );
- C. set (Calendar. HOUR_OF_DAY, H );
- C. set (Calendar. MINUTE, m );
- C. set (Calendar. SECOND, s );
- Calendar now = Calendar. getInstance ();
- Long aTime = now. getTimeInMillis ();
- Long bTime = c. getTimeInMillis ();
- Long cTime = aTime-bTime;
- Long sTime = cTime/1000; // time difference, in seconds
- Long mTime = sTime/60;
- Long hTime = mTime/60;
- Long dTime = hTime/24;
- SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
- System. out. println ("current time:" + f. format (now. getTime ()));
- System. out. println ("set time:" + f. format (c. getTime ()));
- System. out. println ("Time Difference:" + dTime + "day" + hTime % 24 + "Hour" + mTime % 60 + "Minute" + sTime % 60 + "second ");
- }