数据库中employ表,入职日期,今天日期:
测试代码:
1 package javademo; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 import java.text.SimpleDateFormat; 8 import java.util.Calendar; 9 import java.util.Date; 10 11 public class TimeCompare { 12 13 public static void main(String args[]) throws Exception{ 14 Class.forName("com.mysql.jdbc.Driver"); 15 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?user=root&password=root"); 16 Statement stmt = conn.createStatement(); 17 ResultSet rs = stmt.executeQuery("select * from employ"); 18 19 while(rs.next()){ 20 String hireDatestr = rs.getString("hiredate"); 21 Date hireDate = new SimpleDateFormat("yyyy-MM-dd").parse(hireDatestr); 22 System.out.println("(方法一)入职天数: " + compartDay(new Date(), hireDate)); 23 System.out.println("(方法二)入职天数: " + intervalDays(new Date(), hireDate)); 24 System.out.println("入职日期距今月数: " + getMonthSpace(hireDate, new Date())); 25 System.out.println("入职日期是否超过了2个月:" + afterFewMonth(hireDate, 2)); 26 System.out.println("入职是否已经有半年:" + afterFewMonth(hireDate, 6)); 27 System.out.println("入职是否已经有两周:" + afterFewWeek(hireDate, 2)); 28 29 System.out.println(); 30 } 31 32 rs.close(); 33 stmt.close(); 34 conn.close(); 35 } 36 37 //比较入职日期距今是否过了n个月 38 public static boolean afterFewMonth(Date hireDate, int n){ 39 boolean result = false; 40 41 Calendar c = Calendar.getInstance(); 42 c.add(Calendar.MONTH, n); 43 44 if(hireDate.after(c.getTime())){ 45 result = true; 46 } 47 48 return result; 49 } 50 51 //比较入职是否有了n周 52 /** 53 * Calendar.DATE : 代表天数 54 * Calendar.WEDNESDAY: 代表周数 55 * Calendar.MONTH : 代表月数 56 * Calendar.YEAR :代表年数 57 */ 58 public static boolean afterFewWeek(Date hireDate, int n){ 59 boolean result = false; 60 61 Calendar c = Calendar.getInstance(); 62 c.add(Calendar.WEDNESDAY, n); 63 System.out.println("计算"+n+"周后日期:" + c.getTime()); 64 65 if(hireDate.after(c.getTime())){ 66 result = true; 67 } 68 69 return result; 70 } 71 72 /** 73 * 比较两个日期之间相差的月数 74 * 只能比较月数,计算的不精确,只是单纯的月数之间的比较 75 */ 76 public static int getMonthSpace(Date start, Date end){ 77 if(start.after(end)){ 78 Date temp = start; 79 start = end; 80 end = temp; 81 } 82 83 Calendar c = Calendar.getInstance(); 84 c.setTime(start); 85 int year1 = c.get(Calendar.YEAR); 86 int month1 = c.get(Calendar.MONTH); 87 88 c.setTime(end); 89 int year2 = c.get(Calendar.YEAR); 90 int month2 = c.get(Calendar.MONTH); 91 92 int result; 93 if(year1==year2){ 94 result = month2 - month1; 95 }else{ 96 result = 12*(year2-year1) + month2 - month1; 97 } 98 99 return result; 100 } 101 102 /** 103 * 比较和今天相差的天数 方法一 104 * 用毫秒计算差值 105 */ 106 public static int compartDay(Date date1, Date date2){ 107 if(date1.after(date2)){ 108 Date temp = date1; 109 date1 = date2; 110 date2 = temp; 111 } 112 long intervalMilli = date2.getTime() - date1.getTime(); 113 return (int)(intervalMilli/(24*60*60*1000)); 114 } 115 116 /** 117 * 比较和今天相差的天数方法二 ----不对,纠正结果见下面!!!!!! 118 * 用日历的日来计算差值 119 */ 120 public static int intervalDays(Date date1, Date date2){ 121 if(date1.after(date2)){ 122 Date temp = date1; 123 date1 = date2; 124 date2 = temp; 125 } 126 Calendar calendar = Calendar.getInstance(); 127 calendar.setTime(date1); 128 int day1 = calendar.get(Calendar.DAY_OF_YEAR); 129 calendar.setTime(date2); 130 int day2 = calendar.get(Calendar.DAY_OF_YEAR); 131 return day2 - day1; 132 } 133 }
控制台打印:
纠正:比较和今天相差的天数方法二 这个计算方法不对。更新如下:
上面的方法中,如果不是同一个年份的,计算的结果就不对,参考博客:https://blog.csdn.net/ZeroBz/article/details/80757391
代码如下:
package com.cy.test.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TestDate2 { public static void main(String[] args) throws ParseException { String str = "2010-08-19 14:20:00"; SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = s.parse(str); Date date2 = new Date(); System.out.println(intervalDays1(date1, date2)); System.out.println(intervalDays2(date1, date2)); } /** * 方法一 * 计算两个日期的天数之差 这里默认date1<date2,date精确到时分秒 * 使用日历的日来计算差值 * @param date1 * @param date2 * @return */ public static int intervalDays1(Date date1, Date date2){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date1); int day1 = calendar.get(Calendar.DAY_OF_YEAR); int year1 = calendar.get(Calendar.YEAR); calendar.setTime(date2); int day2 = calendar.get(Calendar.DAY_OF_YEAR); int year2 = calendar.get(Calendar.YEAR); if (year1 != year2) { //不同年 int timeDistance = 0; for (int i = year1; i < year2; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { //闰年 timeDistance += 366; }else{ //平年 timeDistance += 365; } } return timeDistance + (day2 - day1); }else{ return day2 - day1; } } /** * 方法二 * 计算两个日期的天数之差 这里默认date1<date2,date精确到时分秒 * 使用SimpleDateFormat将时分秒的日期转化为只有年月日,再利用毫秒计算差值 * @param date1 * @param date2 * @return */ public static int intervalDays2(Date date1, Date date2){ SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); try { Date day1 = s.parse(s.format(date1)); Date day2 = s.parse(s.format(date2)); int intervalDay = (int) ((day2.getTime() - day1.getTime()) / (1000 * 60 * 60 * 24)); return intervalDay; } catch (Exception e) { e.printStackTrace(); System.out.println("calculateTkDay,parseException"); } return 0; } }
console打印结果计算结果相同:
3289 3289