Java tutorial
package org.fire.platform.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; public class DateUtil { /*SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMMdd HHmmss"); SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //now.toLocaleString() SimpleDateFormat myFmt3=new SimpleDateFormat("yyyyMMdd HHmmss E ");*/ public static final String FORMAT_1 = "yyyy"; public static final String FORMAT_2 = "yyyy-MM"; public static final String FORMAT_3 = "yyyy-MM-dd"; public static final String FORMAT_4 = "yyyy-MM-dd HH"; public static final String FORMAT_5 = "yyyy-MM-dd HH:mm"; public static final String FORMAT_6 = "yyyy-MM-dd HH:mm:ss"; public final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss"; /*public static Date parseDate(String format, String datestr) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(format); return formatter.parse(datestr); }*/ public static Date parseDate(SimpleDateFormat formatter, String datestr) throws ParseException { return formatter.parse(datestr); } public static String getTime(String type, String time) { if (StringUtils.isEmpty(time)) { return "11-01 15:35"; } SimpleDateFormat formatter = new SimpleDateFormat(type); String newTime = formatter.format(new Date(Long.parseLong(time))); return newTime; } public static String getTime(String type, long time) { if (time == 0) { return "11-01 15:35"; } SimpleDateFormat formatter = new SimpleDateFormat(type); String newTime = formatter.format(new Date(time)); return newTime; } public static String format(Date date, String pattern) { SimpleDateFormat fastDateFormat = new SimpleDateFormat(pattern); return fastDateFormat.format(date); } /** * ?companystarttime endtime 0? 8 ? * * @param type * @param date * @return */ public static String getTime(String type, Date date) { String newTime; if (date == null) { newTime = "0"; } else { SimpleDateFormat formatter = new SimpleDateFormat(type); newTime = formatter.format(date); } return newTime; } /** * ?? * * @param days * @return */ public static Date addDay(Integer days) { Date nowDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); calendar.add(Calendar.DAY_OF_MONTH, days); return calendar.getTime(); } /** * ?? * * @param week * @return */ public static Date addWeek(Integer week) { Date nowDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); calendar.add(Calendar.WEEK_OF_MONTH, week); return calendar.getTime(); } /** * ?? * * @param month * @return */ public static Date addMonth(Integer month, Integer days) { Date nowDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); calendar.add(Calendar.MONTH, month); calendar.add(Calendar.DAY_OF_MONTH, days); return calendar.getTime(); } /** * ? * * @param beforeTime * @param years * @param months * @param days * @return */ public static String addPeriod(String beforeTime, int years, int months, int days) { String afterTime = ""; try { Date before = DateUtil.getDateTime("yyyyMMdd", beforeTime); Calendar c = Calendar.getInstance(); c.setTime(before); c.add(Calendar.YEAR, years); c.add(Calendar.MONTH, months); c.add(Calendar.DATE, days); afterTime = DateUtil.getTime("yyyyMMdd", c.getTime()); } catch (Exception e) { e.printStackTrace(); } return afterTime; } /** * ? ?? * * @param startTime * @param endTime * @param years * @param months * @param days * @return map(key = startTime, key = endTime) */ public static Map<String, String> getIncreasedPeriodDateMap(String startTime, String endTime, int years, int months, int days) { Map<String, String> timeMap = new HashMap<String, String>(); Date currentDate = DateUtil.getDateTime("yyyyMMdd", DateUtil.getTime("yyyyMMdd", new Date())); Date endDate = DateUtil.getDateTime("yyyyMMdd", endTime); if (currentDate.getTime() > endDate.getTime()) { startTime = DateUtil.getTime("yyyyMMdd", currentDate); endTime = startTime; } // add license time endTime = DateUtil.addPeriod(endTime, years, months, days); timeMap.put("startTime", startTime); timeMap.put("endTime", endTime); return timeMap; } /** * 20120101 * * @throws java.text.ParseException */ public static int getMonthNum(String time1, String time2) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date1 = format.parse(time1); Date date2 = format.parse(time2); Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); return (cal2.get(1) - cal1.get(1)) * 12 + (cal2.get(2) - cal1.get(2)); } /** * 2 * * @param time1 * @param time2 * @return */ public static int getIntervalDays(String time1, String time2) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date1 = format.parse(time1); Date date2 = format.parse(time2); Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); long intervalMilli = cal1.getTime().getTime() - cal2.getTime().getTime(); return (int) (intervalMilli / (24 * 60 * 60 * 1000)); } /** * * */ public static int getWeek() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); return c.get(Calendar.WEEK_OF_YEAR); } public static int getDay() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); return c.get(Calendar.DAY_OF_YEAR); } public static int getNowDay() { return Integer.decode(getTime("yyyyMMdd", new Date())); } /** * * */ public static long getNowDayTime() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); } public static long getNowDayTime1() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 2); cal.set(Calendar.MONTH, 1); cal.set(Calendar.YEAR, 2012); cal.set(Calendar.HOUR, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return cal.getTimeInMillis(); } /** * ? * * @return */ public static Map<String, Integer> getWeekUpAndDown() { Map<String, Integer> map = new HashMap<String, Integer>(); Calendar c = Calendar.getInstance(); int weekday = c.get(Calendar.DAY_OF_WEEK) - 2; c.add(Calendar.DAY_OF_MONTH, -weekday); map.put("up", Integer.decode(getTime("yyyyMMdd", c.getTime()))); c.add(Calendar.DAY_OF_MONTH, 6); map.put("down", Integer.decode(getTime("yyyyMMdd", c.getTime()))); return map; } /** * ? * * @return */ public static Map<String, Integer> getLastWeekUpAndDown() { Map<String, Integer> map = new HashMap<String, Integer>(); Calendar c = Calendar.getInstance(); int weekday = c.get(Calendar.DAY_OF_WEEK) + 5; c.add(Calendar.DAY_OF_MONTH, -weekday); map.put("up", Integer.decode(getTime("yyyyMMdd", c.getTime()))); c.add(Calendar.DAY_OF_MONTH, 6); map.put("down", Integer.decode(getTime("yyyyMMdd", c.getTime()))); return map; } /** * ? * * @throws java.text.ParseException */ public static Date getDateTime(String formatString, String time) { SimpleDateFormat format = new SimpleDateFormat(formatString); Date date1 = null; if (!time.equals("0") && !org.springframework.util.StringUtils.isEmpty(time)) { try { date1 = format.parse(time); } catch (ParseException e) { } } return date1; } /** * xx? * * @param datetime * @throws ParseException */ public static String getTimeBetweenNow(long datetime) { String date = ""; long time = System.currentTimeMillis(); long l = time - datetime; long day = l / (24 * 60 * 60 * 1000); long hour = (l / (60 * 60 * 1000) - day * 24); long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); if (day > 365) { date = day / 365 + "?"; } else if (day > 30) { date = day / 30 + "?"; } else if (day > 0) { date = day + "?"; } else if (day == 0 && hour > 0) { date = hour + "??"; } else if (min > 0 && hour == 0) { date = min + "?"; } else if (min == 0) { date = s + "?"; } // System.out.println(""+day+""+hour+"?"+min+""+s+""); return date; } /** * ??true?false * * @param endTime * @return */ public static boolean isOverdue(String endTime) { if (StringUtils.isEmpty(endTime) || !StringUtils.isNumeric(endTime)) { return false; } // ? int now = Integer.parseInt(getTime("yyyyMMdd", new Date())); // ?0?? return Integer.parseInt(endTime) - now < 0; } public static String addMonth(String endtime, int month) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Integer integer = Integer.valueOf(endtime); Integer now = Integer.valueOf(format.format(new Date())); if (integer > now) { try { Date date = format.parse(endtime); calendar.setTime(date); } catch (ParseException e) { e.printStackTrace(); return endtime; } } calendar.add(Calendar.MONTH, month); return format.format(calendar.getTime()); } /** * * @param date * @param minute (??) * @return */ public static Date addMinute(Date date, Integer minute) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(date); calendar1.add(Calendar.MINUTE, minute); return calendar1.getTime(); } public static void main(String args[]) throws ParseException { System.out.println(new Date(1353057498051l)); String endTime = "20170130"; System.out.println(addMonth(endTime, 1)); //System.out.println(DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", "2015-04-10 14:00:00")); System.out.println(DateUtil.getTime("yyyy-MM-dd HH:mm:ss", new Date())); } /** * ???yyyy-MM-dd,yyyy/MM/dd,yyyy/MM/dd hh:mm:ss null, * @param date * @param format ?? * @return ? */ public static String formatDate(Date date, String format) { if (date == null) { return ""; } SimpleDateFormat formater = new SimpleDateFormat(format); return formater.format(date); } /** * ??yyyy-MM-dd null, * @param date * @return ? */ public static String formatDate(Date date) { if (date == null) { return ""; } SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); return formater.format(date); } /** * ??yyyy-MM-dd HH:mm:ss null, * @param date * @return ? */ public static String formatTime(Date date) { if (date == null) { return ""; } SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return formater.format(date); } /** * ???yyyy-MM-dd,yyyy/MM/dd,yyyy/MM/dd hh:mm:ss * @param dateStr ? * @param format ? * @return */ public static Date parseDate(String dateStr, String format) { if (StringUtils.isEmpty(dateStr)) { return null; } SimpleDateFormat formater = new SimpleDateFormat(format); try { return formater.parse(dateStr); } catch (ParseException e) { throw new RuntimeException(e); } } /** * yyyy-MM-dd?? * @param dateStr ? * @return */ public static Date parseDate(String dateStr) { String result = ""; if (dateStr.indexOf("/") != -1) { result = dateStr.replaceAll("/", "-"); } else { result = dateStr; } return parseDate(result, "yyyy-MM-dd"); } /** * ??? * @param value ? * @return */ public static Date parse(String valueParam) { if (StringUtils.isEmpty(valueParam)) { return null; } String value = valueParam.trim().replaceAll("/", "-"); if (value.length() == FORMAT_1.length()) { return parseDate(value, FORMAT_1); } else if (value.length() == FORMAT_2.length()) { return parseDate(value, FORMAT_2); } else if (value.length() == FORMAT_3.length()) { return parseDate(value, FORMAT_3); } else if (value.length() == FORMAT_4.length()) { return parseDate(value, FORMAT_4); } else if (value.length() == FORMAT_5.length()) { return parseDate(value, FORMAT_5); } else if (value.length() == FORMAT_6.length()) { return parseDate(value, FORMAT_6); } else { throw new RuntimeException("?????."); } } /** * * @param startDate * @param endDate * @return */ public static Integer getDays(Date startDate, Date endDate) { if (startDate == null || endDate == null) { return 0; } // Date start = DateUtil.parseDate(DateUtil.formatDate(startDate, "yyyy-MM-dd")); Date end = DateUtil.parseDate(DateUtil.formatDate(endDate, "yyyy-MM-dd")); Long intervalDays = (end.getTime() - start.getTime()) / (24 * 3600 * 1000); return intervalDays.intValue(); } }