. 1 public static void main (String [] args) throws a ParseException { 2 / ** . 3 * Get the current time . 4 * . 5 * / . 6 a Date DATE = new new a Date (); . 7 / ** converter output formats mentioned date * / . 8 the SimpleDateFormat = the dateFormat new new the SimpleDateFormat ( "the mM-dd-YYYY HH: mm: SS" ); . 9 10 . 11 string A = "2020-01-01"; // time string 12 is string B = dateFormat.format (DATE); 13 is 14 Between_dayInteger = Long between_days (A, B); 15 16 System.out.println (between_dayInteger); . 17 18 is } . 19 20 is public static Long between_days (A String, String B) { 21 is 22 is the SimpleDateFormat SimpleDateFormat = new new the SimpleDateFormat ( "the MM-YYYY -dd "); // custom time format 23 is 24 calendar calendar_a Calendar.getInstance = (); // Get a calendar object 25 calendar calendar_b = Calendar.getInstance (); 26 is 27 a Date date_a = null ; 28 a Date date_b = null ; 29 30 the try { 31 is date_a = simpleDateFormat.parse (A); // string transfer a Date 32 date_b = simpleDateFormat.parse (B); 33 is calendar_a.setTime (date_a); // Set the calendar 34 is calendar_b .setTime (date_b); 35 } the catch (a ParseException E) { 36 e.printStackTrace (); // format abnormality 37 [ } 38 is 39 Long Time_A = calendar_a.getTimeInMillis (); 40 Long Time_B = calendar_b.getTimeInMillis (); 41 is 42 is Long between_days = (Time_B - Time_A) / (1000 * 3600 * 24); // difference day_count_convention 43 is 44 is return between_days; 45 }
Java or calculated current time with the number of days specified time difference between the two dates strings
Guess you like
Origin www.cnblogs.com/xieshilin/p/12310356.html
Recommended
Ranking