Java tutorial
/* * Copyright (c) 2013, FPX and/or its affiliates. All rights reserved. * Use, Copy is subject to authorized license. */ package com.camel.framework.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * * <pre> * ? * </pre> * * @author camel.deng * @date 2013917 */ public final class DateUtils { /** * {@value} */ private static final int DAY_31 = 31; /** * ?yyyy-MM-dd HH:mm:ss-- */ private static final int NUM_10 = 10; /** * log4j */ private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class); /** * ? */ private DateUtils() { } /** * ? */ public static final String STANDARD = "yyyy-MM-dd HH:mm:ss"; /** * ? */ public static final String COMPACT = "yyMMdd HH:mm:ss"; /** * ??, */ public static final String FILENAME = "yyMMdd_HHmmss"; /** * ? */ public static final String SIMPLE = "yyMMdd"; /** * ? */ public static final String DATE = "yyyy-MM-dd"; /** * ? */ public static final String QUERY_STR = "yyyy-MM-ddHHmm"; /** * ?(?)??,?, */ public static final String FORMAT_NO_CONNECTOR = "yyMMddHHmmssSS"; /** * */ public static final char YEAR_TYPE = 'y'; /** * */ public static final char MONTH_TYPE = 'M'; /** * */ public static final char DAY_TYPE = 'd'; /** * */ public static final char HOUR_TYPE = 'H'; /** * */ public static final char MINUTE_TYPE = 'm'; /** * */ public static final char SECOND_TYPE = 's'; /** * */ public static final String START_TIME = " 00:00:00"; /** * ? */ public static final String END_TIME = " 23:59:59"; /** * ??(yyyy-MM-dd HH:mm:ss) * * @param strDate * @return */ public static Date getStandardStringDate(String strDate) { return getStringDate(strDate, STANDARD); } /** * ?? * * @param strDate * @param strType ? * @return */ public static Date getStringDate(String strDate, String strType) { Date date = null; SimpleDateFormat objSDF = new SimpleDateFormat(strType); try { date = objSDF.parse(strDate); } catch (Exception e) { LOGGER.warn("", e); } return date; } /** * * * @param objGC Calender * @param strType ?"yyyy-MM-dd HH:mm:ss" * @return strType? */ public static String getDateString(GregorianCalendar objGC, String strType) { if (objGC == null) { return ""; } Date objDate = objGC.getTime(); return getDateString(objDate, strType); } /** * ?? * * @param objGC Calender * @return ? */ public static String getStandardDate(GregorianCalendar objGC) { return getDateString(objGC, STANDARD); } /** * ???"yyMMdd_HH:mm:ss" * * @param objGC Calender * @return ?? */ public static String getFileNameDate(GregorianCalendar objGC) { return getDateString(objGC, FILENAME); } /** * ??"yyMMdd HH:mm:ss" * * @param objGC Calender * @return ? */ public static String getCompactDate(GregorianCalendar objGC) { return getDateString(objGC, COMPACT); } /** * ?? * * @param objDate java.utils.Date * @param strType ? * @return ? */ public static String getDateString(Date objDate, String strType) { if (objDate == null) { return ""; } SimpleDateFormat objSDF = new SimpleDateFormat(strType); return objSDF.format(objDate); } /** * ??"yyyy-MM-dd HH:mm:ss" * * @param objDate java.utils.Date * @return ? */ public static String getStandardDate(Date objDate) { return getDateString(objDate, STANDARD); } /** * ??"yyMMdd HH:mm:ss" * * @param objGC java.utils.Date * @return ? */ public static String getCompactDate(Date objDate) { return getDateString(objDate, COMPACT); } /** * ???"yyMMdd_HH:mm:ss" * * @param objGC java.utils.Date * @return ?? */ public static String getFileNameDate(Date objDate) { return getDateString(objDate, FILENAME); } /** * ??? * * @param strType ? * @return ? */ public static String getSysdateString(String strType) { Date objDate = new Date(); return getDateString(objDate, strType); } /** * ??? * * @return ? */ public static String getStandardSysdate() { Date objDate = new Date(); return getDateString(objDate, STANDARD); } /** * ?(YYMMDD) * * @return ? */ public static String getSimpleSysdate() { Date objDate = new Date(); return getDateString(objDate, SIMPLE); } /** * ? , * * @param strDate * @return ? */ public static String getStringStandardDate(String strDate) { return getDateString(strDate, DATE); } /** * ????(yyMMdd HH:mm:ss) * * @return ?? */ public static String getCompactSysdate() { Date objDate = new Date(); return getDateString(objDate, COMPACT); } /** * ????(yyMMdd_HH:mmss) * * @return */ public static String getFileNameSysdate() { Date objDate = new Date(); return getDateString(objDate, FILENAME); } /** * ?(yyyy-MM-dd HH:mm:ss) * * @param strDate * @return */ public static Date getStandardDate(String strDate) { return getStringDate(strDate, STANDARD); } /** * ?? * * @param strDate * @return ? */ public static String getStandardDateForDB(String strDate) { if (StringUtils.isBlank(strDate)) { return null; } Date objDate = getStandardDate(strDate); return getStandardDate(objDate); } /** * ?? * * @param strDate (2010-01-01 12:30:50.0) * @return ? */ public static String getDateString(String strDate, String strType) { if (StringUtils.isBlank(strDate)) { return null; } Date objDate = getStandardDate(strDate); return getDateString(objDate, strType); } /** * ?, yyyy-MM-dd 00:00:00 * * @param strBeginDate * @return , yyyy-MM-dd 00:00:00 */ public static String getStandardBeginDate(String strBeginDate) { String beginDate = strBeginDate; return beginDate.substring(0, NUM_10) + START_TIME; } /** * ??, yyyy-MM-dd 23:59:59 * * @param strEndDate * @return ?, yyyy-MM-dd 23:59:59 */ public static String getStandardEndDate(String strEndDate) { String beginDate = strEndDate; return beginDate.substring(0, NUM_10) + END_TIME; } /** * ? * * @param strDate * @return */ public static String getStandardDateString(String strDate) { SimpleDateFormat objFormat = new SimpleDateFormat(STANDARD); Date objDate = null; for (int i = 0; i < 2; i++) { try { objDate = objFormat.parse(strDate); objFormat.applyPattern(STANDARD); } catch (ParseException e) { objFormat.applyPattern(DATE); } } if (null != objDate) { return objFormat.format(objDate); } return null; } /** * ? * * @param day day of month * @return */ public static String getConfirmDayDateString(Integer day) { Calendar objCal = new GregorianCalendar(); objCal.set(Calendar.DATE, day); return getStandardDate(objCal.getTime()); } /** * ? * * @param day day of month * @return */ public static String getConfirmMonthDayDateString(Integer day) { Calendar objCal = Calendar.getInstance(); Date date = new Date(); objCal.setTime(date); objCal.set(Calendar.MONTH, objCal.get(Calendar.MONTH) - 1); if (day != DAY_31) { objCal.set(Calendar.DAY_OF_MONTH, day); } else { objCal.set(Calendar.DAY_OF_MONTH, objCal.getActualMaximum(Calendar.DAY_OF_MONTH)); } return getStandardDate(objCal.getTime()); } /** * ? * * @param strEndDate * @return */ @SuppressWarnings("deprecation") public static String getMonthDayDateString(String strEndDate) { Date objDate = getStringDate(strEndDate, STANDARD); return String.valueOf(objDate.getMonth()); } /** * ???(235959) * * @param strDate * @param strDay * @return 23:59:59 */ public static String getEndPointByDateString(final String strDate, final String strDay) { String day = strDay; if (StringUtils.isBlank(day) || day.length() > 2) { return null; } if (strDay.length() == 1) { day = "0" + day; } return getStandardEndDate(day.substring(0, 8) + day); } /** * <code> * ???amountIllegalArgumentException<br> * X<br> * YEAR_TYPE - <br> * MONTH_TYPE - <br> * DAY_TYPE - <br> * HOUR_TYPE - ?<br> * MINUTE_TYPE - <br> * SECOND_TYPE - <br> * </code> * * @param strDate * @param amount ? * @param type ? * @return ??amount */ public static Date getDateBefore(String strDate, int amount, char type) { return getDateAfter(strDate, -amount, type); } /** * <code> * ???amountIllegalArgumentException<br> * X<br> * YEAR_TYPE - <br> * MONTH_TYPE - <br> * DAY_TYPE - <br> * HOUR_TYPE - ?<br> * MINUTE_TYPE - <br> * SECOND_TYPE - <br> * </code> * * @param date * @param amount ? * @param type ? * @return ??amount */ public static Date getDateBefore(Date date, int amount, char type) { return getDateAfter(date, -amount, type); } /** * <code> * ???amountIllegalArgumentException<br> * X<br> * YEAR_TYPE - <br> * MONTH_TYPE - <br> * DAY_TYPE - <br> * HOUR_TYPE - ?<br> * MINUTE_TYPE - <br> * SECOND_TYPE - <br> * </code> * * @param strDate * @param amount ? * @param type ? * @return ??amount */ public static Date getDateAfter(String strDate, int amount, char type) { return getDateAfter(getStandardDate(strDate), amount, type); } /** * <code> * ???amountIllegalArgumentException<br> * X<br> * YEAR_TYPE - <br> * MONTH_TYPE - <br> * DAY_TYPE - <br> * HOUR_TYPE - ?<br> * MINUTE_TYPE - <br> * SECOND_TYPE - <br> * </code> * * @param date * @param amount ? * @param type ? * @return ??amount */ public static Date getDateAfter(Date date, int amount, char type) { switch (type) { case YEAR_TYPE: return org.apache.commons.lang3.time.DateUtils.addYears(date, amount); case MONTH_TYPE: return org.apache.commons.lang3.time.DateUtils.addMonths(date, amount); case DAY_TYPE: return org.apache.commons.lang3.time.DateUtils.addDays(date, amount); case HOUR_TYPE: return org.apache.commons.lang3.time.DateUtils.addHours(date, amount); case MINUTE_TYPE: return org.apache.commons.lang3.time.DateUtils.addMinutes(date, amount); case SECOND_TYPE: return org.apache.commons.lang3.time.DateUtils.addSeconds(date, amount); } throw new IllegalArgumentException("type can not be " + type); } /** * ?() * * @param strD1 String * @param strD2 String * @return int */ public static int getTimeDiff(String strD1, String strD2) { SimpleDateFormat objFM = new SimpleDateFormat(DATE); Date date1 = null; Date date2 = null; try { date1 = objFM.parse(strD1); date2 = objFM.parse(strD2); return (int) ((date1.getTime() - date2.getTime()) / 10000); } catch (Exception ex) { LOGGER.error("error", ex); throw new RuntimeException("?", ex); } } }