This article shares the specific code of Java tool class DateUtils for your reference. The specific content is as follows
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * Description: Public date tool class*/public class DateUtils { public static String DATE_FORMAT = "yyyy-MM-dd"; public static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static String DATE_FORMAT_CHINESE = "yyyyy year M month d day"; /** * Get the current date* * * @return * */ public static String getCurrentDate() { String datestr = null; SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_FORMAT); datestr = df.format(new Date()); return datestr; } /** * Get the current date and time* * * @return * */ public static String getCurrentDateTime() { String datestr = null; SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT); datestr = df.format(new Date()); return datestr; } /** * Get the current date and time* * * @return * */ public static String getCurrentDateTime(String Dateformat) { String datestr = null; SimpleDateFormat df = new SimpleDateFormat(Dateformat); datestr = df.format(new Date()); return datestr; } public static String dateToDateTime(Date date) { String datestr = null; SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_FORMAT); datestr = df.format(date); return datestr; } /** * Convert string date to date format* * * @param datestr * @return * */ public static Date stringToDate(String datestr) { if(datestr ==null ||datestr.equals("")){ return null; } Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_FORMAT); try { date = df.parse(datestr); } catch (ParseException e) { date=DateUtils.stringToDate(datestr,"yyyyMMdd"); } return date; } /** * Convert string date to date format* Custom format* * @param datestr * @return * */ public static Date stringToDate(String datestr, String dateformat) { Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat(dateformat); try { date = df.parse(datestr); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * Convert date format date to string format* * * @param date * @return * */ public static String dateToString(Date date) { String datestr = null; SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_FORMAT); datestr = df.format(date); return datestr; } /** * Convert date format date to string format custom format * * @param date * @param dateformat * @return */ public static String dateToString(Date date, String dateformat) { String datestr = null; SimpleDateFormat df = new SimpleDateFormat(dateformat); datestr = df.format(date); return datestr; } /** * Get the DAY value of the date* * * @param date * Enter the date* @return * */ public static int getDayOfDate(Date date) { int d = 0; Calendar cd = Calendar.getInstance(); cd.setTime(date); d = cd.get(Calendar.DAY_OF_MONTH); return d; } /** * Get the MONTH value of the date* * * @param date * Enter the date* @return * */ public static int getMonthOfDate(Date date) { int m = 0; Calendar cd = Calendar.getInstance(); cd.setTime(date); m = cd.get(Calendar.MONTH) + 1; return m; } /** * Get the YEAR value of the date* * * @param date * Enter the date* @return * */ public static int getYearOfDate(Date date) { int y = 0; Calendar cd = Calendar.getInstance(); cd.setTime(date); y = cd.get(Calendar.YEAR); return y; } /** * Get the day of the week* * * @param date * Enter the date* @return * */ public static int getWeekOfDate(Date date) { int wd = 0; Calendar cd = Calendar.getInstance(); cd.setTime(date); wd = cd.get(Calendar.DAY_OF_WEEK) - 1; return wd; } /** * Get the first day of the month of the input date* * * @param date * Input date* @return * */ public static Date getFirstDayOfMonth(Date date) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.set(Calendar.DAY_OF_MONTH, 1); return cd.getTime(); } /** * Get the last day of the month of the input date* * @param date */ public static Date getLastDayOfMonth(Date date) { return DateUtils.addDay(DateUtils.getFirstDayOfMonth(DateUtils.addMonth(date, 1)), -1); } /** * Determine whether it is a leap year* * * @param date * Input date* @return Yes true No false * */ public static boolean isLeapYEAR(Date date) { Calendar cd = Calendar.getInstance(); cd.setTime(date); int year = cd.get(Calendar.YEAR); if (year % 4 == 0 && year % 100 != 0 | year % 400 == 0) { return true; } else { return false; } } /** * Generate date type format based on the year, month and day represented by integer number* * @param year * year* @param month * month* @param day * day* @return * */ public static Date getDateByYMD(int year, int month, int day) { Calendar cd = Calendar.getInstance(); cd.set(year, month-1, day); return cd.getTime(); } /** * Get the corresponding date of the year cycle* * @param date * Enter date* @param iyear * The number of years represents the previous* @return * */ public static Date getYearCycleOfDate(Date date, int iyear) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.add(Calendar.YEAR, iyear); return cd.getTime(); } /** * Get the corresponding date of the month cycle* * @param date * Enter date* @param i * @return * */ public static Date getMonthCycleOfDate(Date date, int i) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.add(Calendar.MONTH, i); return cd.getTime(); } /** * Calculate how many years the difference between fromDate and toDate* * @param fromDate * @param toDate * @return Years* */ public static int getYearByMinusDate(Date fromDate, Date toDate) { Calendar df=Calendar.getInstance(); df.setTime(fromDate); Calendar dt=Calendar.getInstance(); dt.setTime(toDate); return dt.get(Calendar.YEAR)-df.get(Calendar.YEAR); } /** * Calculate how many months the difference between fromDate and toDate* * @param fromDate * @param toDate * @return Months* */ public static int getMonthByMinusDate(Date fromDate, Date toDate) { Calendar df=Calendar.getInstance(); df.setTime(fromDate); Calendar dt=Calendar.getInstance(); dt.setTime(toDate); return dt.get(Calendar.YEAR)*12+dt.get(Calendar.MONTH)- (df.get(Calendar.YEAR)*12+df.get(Calendar.MONTH)); } /** * Calculate how many days the difference between fromDate and toDate* * @param fromDate * @param toDate * @return days* */ public static long getDayByMinusDate(Object fromDate, Object toDate) { Date f=DateUtils.chgObject(fromDate); Date t=DateUtils.chgObject(toDate); long fd=f.getTime(); long td=t.getTime(); return (td-fd)/(24L * 60L * 60L * 1000L); } /** * Calculate age* * @param birthday * Date date* @param calcDate * Date point to be calculated* @return * */ public static int calcAge(Date birthday, Date calcDate) { int cYear=DateUtils.getYearOfDate(calcDate); int cMonth=DateUtils.getMonthOfDate(calcDate); int cDay=DateUtils.getDayOfDate(calcDate); int bYear=DateUtils.getYearOfDate(birthday); int bMonth=DateUtils.getMonthOfDate(birthday); int bDay=DateUtils.getDayOfDate(birthday); if(cMonth>bMonth||(cMonth==bMonth&&cDay>bDay)){ return cYear-bYear; }else{ return cYear-1-bYear; } } /** * Get the date of birth from the ID card* * @param idno * ID card number* @return * */ public static String getBirthDayFromIDCard(String idno) { Calendar cd = Calendar.getInstance(); if (idno.length() == 15) { cd.set(Calendar.YEAR, Integer.valueOf("19" + idno.substring(6, 8)) .intValue()); cd.set(Calendar.MONTH, Integer.valueOf(idno.substring(8, 10)) .intValue() - 1); cd.set(Calendar.DAY_OF_MONTH, Integer.valueOf(idno.substring(10, 12)).intValue()); } else if (idno.length() == 18) { cd.set(Calendar.YEAR, Integer.valueOf(idno.substring(6, 10)) .intValue()); cd.set(Calendar.MONTH, Integer.valueOf(idno.substring(10, 12)) .intValue() - 1); cd.set(Calendar.DAY_OF_MONTH, Integer.valueOf(idno.substring(12, 14)).intValue()); } return DateUtils.dateToString(cd.getTime()); } /** * Increase (+) or subtract (-) days on the input date* * @param date * Input date* @param emblem * Number of days to increase or decrease*/ public static Date addDay(Date date, int iday) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.add(Calendar.DAY_OF_MONTH, iday); return cd.getTime(); } /** * Add (+) or subtract (-) months on the input date* * @param date * Enter date* @param image * Month score to increase or decrease*/ public static Date addMonth(Date date, int image) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.add(Calendar.MONTH, image); return cd.getTime(); } /** * Add (+) or subtract (-) years on the input date* * @param date * Enter date * @param emblem * Number of years to increase or decrease */ public static Date addYear(Date date, int iyear) { Calendar cd = Calendar.getInstance(); cd.setTime(date); cd.add(Calendar.YEAR, iyear); return cd.getTime(); } /** * OBJECT type Date * @param date * @return */ public static Date chgObject(Object date){ if(date!=null&&date instanceof Date){ return (Date)date; } if(date!=null&&date instanceof String){ return DateUtils.stringToDate((String)date); } return null; } public static long getAgeByBirthday(String date){ Date birthday = stringToDate(date, "yyyy-MM-dd"); long sec = new Date().getTime() - birthday.getTime(); long age = sec/(1000*60*60*24)/365; return age; } /** * @param args */ public static void main(String[] args) { //String temp = DateUtil.dateToString(getLastDayOfMonth(new Date()), /// DateUtil.DATE_FORMAT_CHINESE); //String s=DateUtil.dateToString(DateUtil.addDay(DateUtil.addYear(new Date(),1),-1)); long s=DateUtils.getDayByMinusDate("2012-01-01", "2012-12-31"); System.err.println(s); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.