import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class MyDateUtil { public static int daysBetween(String date1, String date2) throws ParseException { Date data_1 = new SimpleDateFormat("yyyy-MM-dd").parse(date1); Date data_2 = new SimpleDateFormat("yyyy-MM-dd").parse(date2); Calendar cal = Calendar.getInstance(); cal.setTime(data_1); long time1 = cal.getTimeInMillis(); cal.setTime(data_2); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } public static int daysBetweenDate(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); cal.setTime(date1); long time1 = cal.getTimeInMillis(); cal.setTime(date2); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } public static Date getDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { return sdf.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static String getDateStr(Date date) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } public static String getDateStrDate(Date date) { return new SimpleDateFormat("yyyy-MM-dd ").format(date); } public static String getDateStrTrim(Date date) { return new SimpleDateFormat("yyyyMMddHHmmss").format(date); } public static String getDateStrDat(Date date) { return new SimpleDateFormat("yyyyMMdd").format(date); } public static long getLongDate() { return System.currentTimeMillis(); } public static String getTime1() { java.util.Date date1 = new java.util.Date(); return date1.toString(); } public static String getYearMonthDay1() { java.sql.Date date2 = new java.sql.Date(System.currentTimeMillis()); return date2.toString(); } public static String getYearMonthDay2() { java.util.Date date1 = new java.util.Date(); java.sql.Date date3 = new java.sql.Date(date1.getTime()); return date3.toString(); } public static String getSeconds1() { Timestamp stamp1 = new Timestamp(System.currentTimeMillis()); return stamp1.toString(); } public static String getSeconds2() { java.util.Date date1 = new java.util.Date(); Timestamp stamp2 = new Timestamp(date1.getTime()); return stamp2.toString(); } public static String getYear_Second1() { Timestamp stamp1 = new Timestamp(System.currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timeStr = sdf.format(stamp1); return timeStr; } public static String getYear_Second2() { java.util.Date date1 = new java.util.Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String timeStr2 = df.format(date1); return timeStr2; } public static String changeStringToDate1(String str) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date date6 = null; try { date6 = sdf.parse(str); return date6.toString(); } catch (ParseException e) { e.printStackTrace(); return ""; } } public static String changeStringToDate2(String str) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date date6 = null; try { date6 = sdf.parse(str); java.sql.Date date7 = new java.sql.Date(date6.getTime()); return date7.toString(); } catch (ParseException e) { e.printStackTrace(); return ""; } } public static String changeStringToDate3(String str) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date date6 = null; try { date6 = sdf.parse(str); java.sql.Date date7 = new java.sql.Date(date6.getTime()); Timestamp stamp9 = new Timestamp(date7.getTime()); return stamp9.toString(); } catch (ParseException e) { e.printStackTrace(); return ""; } } public static int getCurrentHour() { Calendar c = Calendar.getInstance(); return c.get(Calendar.HOUR_OF_DAY); } public static String monthPlus(String dateStr, int monthDay) { try { Date now = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MONTH, monthDay); return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); return null; } } public static String monthPlus(Date dateStr, int monthDay) { Calendar calendar = Calendar.getInstance(); calendar.setTime(dateStr); calendar.add(Calendar.MONTH, monthDay); String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; int w = calendar.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())+" "+weekDays[w]; } public static String dayPlus(Date dateStr, int monthDay) { Calendar calendar = Calendar.getInstance(); calendar.setTime(dateStr); calendar.add(Calendar.DAY_OF_MONTH, monthDay); String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; int w = calendar.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())+" "+weekDays[w]; } public static String getWeek(Date date) { String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; Calendar cal = Calendar.getInstance(); cal.setTime(date); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } }