Java tutorial
/** The MIT License (MIT) * Copyright (c) 2015 * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.joinsystem.goku.common.utils; import org.apache.commons.lang3.StringUtils; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * * * * <p> * <b>J2EE</b> * </p> * * <p> * Copyright: Copyright (c) 2013 - 2015 * </p> * * <p> * Company:?? * </p> * * @author killfen * * @version 100-000-000 * * <p> * ? * </p> * * <p> * Comments:? * </p> * * <p> * Create Date:2013-9-2 * </p> * * <p> * Modification history: * </p> */ public class DateUtil { private int year; private int month; private int day; private int hour; private int minute; private int second; private static final int[] dayArray = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };// ? /** * ? MM/dd/yyyy */ public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy"); /** * ? MM/dd/yyyy HH:mm */ public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm"); /** * ? MM/dd/yyyy HH:mm:ss */ public static final SimpleDateFormat DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); /** * ORA? yyyyMMdd */ public static final SimpleDateFormat ORA_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd"); /** * ORA? yyyyMMddHHmm */ public static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); /** * ORA? yyyyMMddHHmmss */ public static final SimpleDateFormat ORA_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss"); /** * yyyy-MM-dd */ public static final SimpleDateFormat CHN_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); /** * yyyy-MM-dd HH:mm */ public static final SimpleDateFormat CHN_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm"); /** * yyyy-MM-dd HH:mm:ss */ public static final SimpleDateFormat CHN_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); /** () yyyyMMddHHmmss */ public static final String dtLong = "yyyyMMddHHmmss"; /** yyyy-MM-dd HH:mm:ss */ public static final String simple = "yyyy-MM-dd HH:mm:ss"; /** () yyyyMMdd */ public static final String dtShort = "yyyyMMdd"; /** * ?()</br> * @return * yyyyMMddHHmmss?? */ public static String getOrderNum() { Date date = new Date(); DateFormat df = new SimpleDateFormat(dtLong); return df.format(date); } /** * ? */ public DateUtil() { today(); } /** * ? * * @param * inValue 14???14???0?<br> * new DataUtil("yyyyMMddHHmmss"); */ DateUtil(String inValue) { SetDate(inValue); } /** * ? * * @param * mills <br> */ public DateUtil(long mills) { setTimeInMillis(mills); } /** * ? */ public DateUtil(int year, int month, int day, int hour, int minute, int second) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day, hour, minute, second); this.year = calendar.get(Calendar.YEAR); this.month = calendar.get(Calendar.MONTH) + 1; this.day = calendar.get(Calendar.DAY_OF_MONTH); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } /** * * * @param inValue */ private void SetDate(String inValue) { if (inValue.length() != 14) {// ?14????0? for (int i = inValue.length(); i < 14; i++) { inValue = inValue + "0"; } System.out.println(inValue); } try { int year = Integer.parseInt(inValue.substring(0, 4)); int month = Integer.parseInt(inValue.substring(4, 6)); int day = Integer.parseInt(inValue.substring(6, 8)); int hour = Integer.parseInt(inValue.substring(8, 10)); int minute = Integer.parseInt(inValue.substring(10, 12)); int second = Integer.parseInt(inValue.substring(12)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day, hour, minute, second); this.year = calendar.get(Calendar.YEAR); this.month = calendar.get(Calendar.MONTH) + 1; this.day = calendar.get(Calendar.DAY_OF_MONTH); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } catch (Exception e) { System.out.println(e.getMessage()); } } /** * ?? */ private void today() { Calendar calendar = Calendar.getInstance(); this.year = calendar.get(Calendar.YEAR); this.month = calendar.get(Calendar.MONTH) + 1; this.day = calendar.get(Calendar.DAY_OF_MONTH); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } /** * * ? * * @param * df ? <br> * @return String */ public String format(SimpleDateFormat df) { Calendar calendar = Calendar.getInstance(); calendar.set(this.year, this.month - 1, this.day, this.hour, this.minute, this.second); return df.format(calendar.getTime()); } /** * toString yyyy-MM-dd HH:mm:ss? */ public String toString() { return this.format(DateUtil.CHN_DATE_TIME_EXTENDED_FORMAT); } public Date getDate() { Calendar date = Calendar.getInstance(); date.set(Calendar.DAY_OF_MONTH, this.getDay()); date.set(Calendar.MONTH, this.getMonth() - 1); date.set(Calendar.YEAR, this.getYear()); date.set(Calendar.HOUR_OF_DAY, this.getHour()); date.set(Calendar.MINUTE, this.getMinute()); date.set(Calendar.SECOND, this.getSecond()); return date.getTime(); } /** * * * * @return long */ public long getTimeInMillis() { Calendar calendar = Calendar.getInstance(); calendar.set(this.year, this.month - 1, this.day, this.hour, this.minute, this.second); return calendar.getTime().getTime(); } /** * ?? * * @param mills */ public void setTimeInMillis(long mills) { Date dd = new Date(mills); Calendar calendar = Calendar.getInstance(); calendar.setTime(dd); this.year = calendar.get(Calendar.YEAR); this.month = calendar.get(Calendar.MONTH) + 1; this.day = calendar.get(Calendar.DAY_OF_MONTH); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } /** * ?? * * @return boolean true: */ public boolean isLeapYear() { return this.isLeapYear(year); } /** * ? * * @param * year <br> * @return boolean true: */ public boolean isLeapYear(int year) { if ((year % 400) == 0) return true; else if ((year % 4) == 0) { if ((year % 100) == 0) return false; else return true; } else return false; } /** * ?? * * @param * years <br> * @param * months <br> * @param * days <br> * @param * hours ?<br> * @param * minutes <br> * @param * seconds <br> */ public void setDateTime(int years, int months, int days, int hours, int minutes, int seconds) { Calendar calendar = Calendar.getInstance(); calendar.set(this.year + years, this.month - 1 + months, this.day + days, this.hour + hours, this.minute + minutes, this.second + seconds); setTimeInMillis(calendar.getTime().getTime()); } /** * ?? * * @param * years <br> */ public void addYear(int years) { if (month == 2 && day == 29)// ?229? { if (this.isLeapYear(year + years) == true)// this.setDateTime(years, 0, 0, 0, 0, 0); else this.setDateTime(years, 0, -1, 0, 0, 0);// ?2?28?1 } else this.setDateTime(years, 0, 0, 0, 0, 0); } /** * ??(oracle??) * * @param months * <br> */ public void addMonth(int months) { int this_day_end = daysOfMonth();// int that_day_end = dayOfMonth(months);// n if (this.day == this_day_end) this.day = that_day_end;// ?,day?that_day_end else if (this.day > that_day_end) this.day = that_day_end; // nthat_day_endday?,day?that_day_end this.setDateTime(0, months, 0, 0, 0, 0); } /** * ?? * * @param days * */ public void addDay(int days) { this.setDateTime(0, 0, days, 0, 0, 0); } /** * ??? * * @param hours * ? */ public void addHour(int hours) { this.setDateTime(0, 0, 0, hours, 0, 0); } /** * ?? * * @param minutes * */ public void addMinute(int minutes) { this.setDateTime(0, 0, 0, 0, minutes, 0); } /** * ?? * * @param seconds * */ public void addSecond(int seconds) { this.setDateTime(0, 0, 0, 0, 0, seconds); } /** * ? * * @return int */ public int daysOfMonth() { if (month > 12 || month < 0) return 0; if (month == 2 && this.isLeapYear()) return 29; else return dayArray[month - 1]; } /** * ??ms * * @param ms * <br> * @return */ public int dayOfMonth(int ms) { int yy = ms / 12; int mm = ms % 12; int year = this.year + yy; int month = this.month + mm; if (month > 12) { month = month - 12; year = year + 1; } if (month < 1) { month = month + 12; year = year - 1; } if (month == 2 && isLeapYear(year)) return 29; else return dayArray[month - 1]; } /** * ,?? * * @param * mydate1<br> * @param * mydate2<br> * @return long */ public static long diffSec(DateUtil mydate1, DateUtil mydate2) { return (mydate1.getTimeInMillis() - mydate2.getTimeInMillis()) / 1000; } /** * * * @param * start <br> * @param * end ?<br> * @return int */ public static int diffMonth(Date start, Date end) { if (start.after(end)) { Date t = start; start = end; end = t; } Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(start); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(end); Calendar temp = Calendar.getInstance(); temp.setTime(end); temp.add(Calendar.DATE, 1); int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH); if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) { return year * 12 + month + 1; } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) { return year * 12 + month; } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) { return year * 12 + month; } else { return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month); } } /** * * * @param * mydate1<br> * @param * mydate2<br> * @return int */ public static int diffDay(DateUtil mydate1, DateUtil mydate2) { return (int) (mydate1.getTimeInMillis() - mydate2.getTimeInMillis()) / 1000 / (3600 * 24); } /** * mydate1-mydate2 * * @param * mydate1<br> * @param * mydate2<br> * @return int */ public static int diffDay(Date mydate1, Date mydate2) { mydate1 = DateUtil.stringToDate(DateUtil.dateFmtToString(mydate1, "yyyy-MM-dd"), "yyyy-MM-dd"); mydate2 = DateUtil.stringToDate(DateUtil.dateFmtToString(mydate2, "yyyy-MM-dd"), "yyyy-MM-dd"); int _day = (int) ((mydate1.getTime() - mydate2.getTime()) / 1000 / (3600 * 24)); return _day; } /** * * * @param d1 * @param d2 * @return */ public static int diffDays(Calendar d1, Calendar d2) { if (d1.after(d2)) { java.util.Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) { d1 = (Calendar) d1.clone(); do { days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } return days; } /** * * * @param * date <br> * @param * day <br> * @return Date,? */ public static Date addDays(Date date, int day) { if (date == null) return null; Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + day); return c.getTime(); } /** * ? * * @param * date <br> * @param * day <br> * @return Date,?? */ public static Date removeDays(Date date, int day) { if (date == null) return null; Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - day); return c.getTime(); } /** * * * @param * date <br> * @param * month <br> * @return Date,? */ public static Date addMonths(Date date, int month) { if (date == null) return null; Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.MONTH, c.get(Calendar.MONTH) + month); return c.getTime(); } /** * ? * * @param * date <br> * @param * month <br> * @return Date,?? */ public static Date removeMonths(Date date, int month) { if (date == null) return null; Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.MONTH, c.get(Calendar.MONTH) - month); return c.getTime(); } /** * ? * * @param * date Date<br> * @param * fmt ??<br> * @return String */ public static String dateFmtToString(Date date, SimpleDateFormat fmt) { return fmt.format(date); } /** * ?,?yyyy-MM-dd hh:mm:ss * * @param * date Date<br> * @return String */ public static String dateFmtToString(Date date) { return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date); } /** * ? * * @param * date Date<br> * @param * fmt ??<br> * @return String */ public static String dateFmtToString(Date date, String fmt) { return new SimpleDateFormat(fmt).format(date); } /** * * * @param * date <br> * @return java.sql.Date */ public static Date stringToDate(String date) { return java.sql.Date.valueOf(date); } /** * * @param date * @param ftm ? * @return ?null */ public static Date stringToDate(String date, String ftm) { SimpleDateFormat sdf = new SimpleDateFormat(ftm); try { return sdf.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * ?? * * @param * date <br> * @param * dataFmt ??<br> * @return Date */ public static Date stringFmtToDate(String date, String dataFmt) { SimpleDateFormat df = new SimpleDateFormat(dataFmt); return java.sql.Date.valueOf(df.format(java.sql.Date.valueOf(date))); } /** * Data??Timestamp * * @param * date * @return Timestamp */ public static Timestamp dateToTimestamp(Date date) { String temp = CHN_DATE_TIME_EXTENDED_FORMAT.format(date); return Timestamp.valueOf(temp); } /** * ?? * * @return int */ public int getDay() { return day; } /** * ? * * @param * day */ public void setDay(int day) { this.day = day; } /** * ?? * * @return int */ public int getHour() { return hour; } /** * ? * * @param * hour */ public void setHour(int hour) { this.hour = hour; } /** * ?? * * @return int */ public int getMinute() { return minute; } /** * ? * * @param * minute */ public void setMinute(int minute) { this.minute = minute; } /** * ?? * * @return int */ public int getMonth() { return month; } /** * ? * * @param * month */ public void setMonth(int month) { this.month = month; } /** * ?? * * @return int */ public int getSecond() { return second; } /** * ? * * @param * second */ public void setSecond(int second) { this.second = second; } /** * ?? * * @return int */ public int getYear() { return year; } /** * ? * * @param * year */ public void setYear(int year) { this.year = year; } /** * ? * @param start1 * @param end1 * @param start2 * @param end2 * @return boolean true false */ public boolean hasCommon(Date start1, Date end1, Date start2, Date end2) { if (end1.before(start2) || end2.before(start1)) return false; else return true; } /** * ? ?? ?? * @param date * @return true: false: */ public static boolean judgeDateMsg(String date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// ? try { java.sql.Date.valueOf(df.format(java.sql.Date.valueOf(date))); } catch (NumberFormatException e) { return false; } return true; } /** * * @param beginDate * @param endDate ? * @return */ public static int daysBetween(String beginDate, String endDate) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); try { cal.setTime(sdf.parse(beginDate)); cal2.setTime(sdf.parse(endDate)); long time1 = cal.getTimeInMillis(); long time2 = cal2.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return (int) between_days; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 1; } /** * ?() * ???? * @param date * @return */ public static int secondBetween(String date) { SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { long presentDate = sdfSecond.parse(sdfSecond.format(new Date())).getTime(); long enterDate = sdfSecond.parse(date).getTime(); return (int) ((presentDate - enterDate) / 1000); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 1; } /** * ?() * ???? * @param date * @return */ public static int secondBetween(Date date) { if (date == null) { return 0; } SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { long presentDate = sdfSecond.parse(sdfSecond.format(new Date())).getTime(); long enterDate = date.getTime(); return (int) ((presentDate - enterDate) / 1000); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 1; } /** * date? * @param date ? * @return */ public static String pastTime(Date date) { int second = secondBetween(date); if (second < 60) { return second + "?"; } else if (second > 60 && second < 30 * 60) { return (second / 60) + "?"; } else if (second > 30 * 60 && second < 60 * 60) { return "???"; } else if (second > 60 * 60 && second < 24 * 60 * 60) { return (second / 60 / 60) + "??"; } else { return DateUtil.dateFmtToString(date, "yyyy-MM-dd HH:mm:ss"); } } /** * () * ???? * @param beginDate * @param endDate ? * @return */ public static int secondBetween(String beginDate, String endDate) { SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { long _enginDate = sdfSecond.parse(beginDate).getTime(); long _endDate = sdfSecond.parse(endDate).getTime(); return (int) ((_enginDate - _endDate) / 1000); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 1; } /** * ????,eg:2015-4-52015-3-2 * @param date 2015-4-52015-3-2 * @param split * @param fmt ? * @return ?null */ public static Date[] beginEndStringToDate(String date, String split, String fmt) { if (StringUtils.isBlank(date) || StringUtils.isBlank(split)) { return null; } String[] _date = date.split(split); if (_date.length == 2) { Date[] d = new Date[2]; d[0] = DateUtil.stringFmtToDate(_date[0], fmt); d[1] = DateUtil.stringFmtToDate(_date[1], fmt); return d; } return null; } /** * date? * @param date * @return Calendarl */ public static Calendar date2Calendar(Date date) { Calendar calendar = Calendar.getInstance(); // calendar.setTime(date); return calendar; } }