之前一個同事的寫法是這樣的(綠色的):
public static final String GENERAL_PATTERN_2 = "yyyy-MM-dd HH:mm:ss";
其實沒必要這樣,在vo里面的set方法做個賦值:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
這樣不行,因為這里的createtime是string的,不能這樣寫(如果createtime屬性是Date類型,給String類型的createtimeStr 屬性賦值 可以這樣寫,createtimeStr這個字段值常用於前端展示~ 不用再在前端處理),寫了訪問報500錯:
那就這樣寫:
public String getCreatetime() { try { return DateUtils.getStringDate(DateUtils.stringToDate(createtime, DateUtil.GENERAL_PATTERN_2)); } catch (ParseException e) { e.printStackTrace(); } return createtime; }
重要工具類DateUtils.java具體代碼如下
import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateUtils { private static final int BIN_DIGITS = 12; public static final short ONE_MS = 1 << BIN_DIGITS; public static String getDatePattern() { return "yyyy-MM-dd"; } public static String getTimePattern() { return "yyyy-MM-dd HH:mm:ss"; } public static String getDatePattern8() { return "yyyyMMdd"; } public static String getTimePattern14() { return "yyyyMMddHHmmss"; } public static Date getDateTime(Date date, String format) throws ParseException { SimpleDateFormat df = new SimpleDateFormat(format); String strDate = df.format(date); Calendar cal = new GregorianCalendar(); cal.setTime(stringToDate(strDate, format)); return cal.getTime(); } public static int getYear() { return Calendar.getInstance().get(Calendar.YEAR); } public static int getYear(Date date) { Calendar cal = new GregorianCalendar(); cal.setTime(date); return cal.get(Calendar.YEAR); } public static int getMonth() { return Calendar.getInstance().get(Calendar.MONTH) + 1; } public static int getMonth(Date date) { Calendar cal = new GregorianCalendar(); cal.setTime(date); return cal.get(Calendar.MONTH) + 1; } public static int getDayOfMonth() { return Calendar.getInstance().get(Calendar.DAY_OF_MONTH); } public static int getDayOfMonth(Date date) { Calendar cal = new GregorianCalendar(); cal.setTime(date); return cal.get(Calendar.DAY_OF_MONTH); } public static final Date stringToDate(String strDate, String format) throws ParseException { SimpleDateFormat df = null; Date date = null; df = new SimpleDateFormat(format); try { date = df.parse(strDate); } catch (ParseException pe) { throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return (date); } public static Date stringToDate(String strDate) throws ParseException { Date aDate = null; try { aDate = stringToDate(strDate, getDatePattern()); } catch (ParseException pe) { pe.printStackTrace(); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return aDate; } public static final String dateToString(Date aDate, String format) { SimpleDateFormat df = null; String returnValue = ""; if (aDate == null) { // } else { df = new SimpleDateFormat(format); returnValue = df.format(aDate); } return (returnValue); } public static final String getStringDate(Date date) { SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒"); return format1.format(date.getTime()); } public static final String dateToString(Date aDate) { return dateToString(aDate, getDatePattern()); } public static final String timeToString(Date aDate) { return dateToString(aDate, getTimePattern()); } public static Date getDate(Date aDate) throws ParseException { return getDateTime(aDate, getDatePattern()); } public static Date getTime(Date aDate) throws ParseException { return getDateTime(aDate, getTimePattern()); } public static long getDaysBetween(Date dtFrom, Date dtEnd) { long begin = dtFrom.getTime(); long end = dtEnd.getTime(); long inter = end - begin; if (inter < 0) { inter = inter * (-1); } long dateMillSec = 24 * 60 * 60 * 1000; long dateCnt = inter / dateMillSec; long remainder = inter % dateMillSec; if (remainder != 0) { dateCnt++; } return dateCnt; } public static long getMinsBetween(Date dtFrom, Date dtEnd) { long begin = dtFrom.getTime(); long end = dtEnd.getTime(); long inter = end - begin; return inter / 60000; } public static String getLastDateOfyearAndMonth(String yearAndMonth) throws Exception { String start = yearAndMonth + "01"; Date startDate = DateUtils.stringToDate(start, "yyyyMMdd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); String end = DateUtils.dateToString(calendar.getTime(), "yyyyMMdd"); return end; } public static String getNextMonthString(String strDate, String format) throws ParseException { Date date = stringToDate(strDate, format); Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.add(Calendar.MONTH, 1); return dateToString(cal.getTime(), format); } /** * 獲取上個月的最后一天 * * @param date * @return * @throws ParseException */ public static Date getLastDayOfLastMonth(Date date) throws ParseException { String str_firstDay = dateToString(date, "yyyyMM") + "01"; Date date_firstDay = stringToDate(str_firstDay, "yyyyMMdd"); if (date_firstDay != null) { Calendar cal = Calendar.getInstance(); cal.setTime(date_firstDay); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } return null; } /** * 獲取上個月的最后一天 * * @param date * @return * @throws ParseException */ public static String getLastDayOfLastMonth(Date date, String format) throws ParseException { return dateToString(getLastDayOfLastMonth(date), format); } /** * 獲取傳入時間所在月的最后一天 * * @param date * @return */ public static int getLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 獲取指定時間下個月的第一天零時00:00:00 * * @param date * @return */ public static Date getNextMonthFirstDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } public static Date getMonthLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); } /** * 獲取一個月的開始時間 * * @param date * @return */ public static Date getMonthFirstDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 當前時間是否在抽獎的有效時間內 * * @return */ public static boolean raffleEffectiveTime() { boolean effective = false; try { Date begin = DateUtils.stringToDate("20130701000000", "yyyyMMddHHmmss"); Date end = DateUtils.stringToDate("20131001000000", "yyyyMMddHHmmss"); Date current = new Date(); if (current.compareTo(begin) == 1 && current.compareTo(end) == -1) { return true; } } catch (ParseException e) { e.printStackTrace(); } return effective; } /** * 將源格式字符串日期轉換成目標格式字符串日期 * * @param origStrDate 字符串日期 * @param origFormat 源格式 * @param destFormat 目標格式 * @return * @throws ParseException */ public static final String dateStrToString(String origStrDate, String origFormat, String destFormat) throws ParseException { Date origDate = stringToDate(origStrDate, origFormat); return dateToString(origDate, destFormat); } // 季度一年四季, 第一季度:2月-4月, 第二季度:5月-7月, 第三季度:8月-10月, 第四季度:11月-1月 private static int getQuarterInMonth(int month, boolean isQuarterStart) { int months[] = { 1, 4, 7, 10 }; if (!isQuarterStart) { months = new int[] { 3, 6, 9, 12 }; } if (month >= 2 && month <= 4) return months[0]; else if (month >= 5 && month <= 7) return months[1]; else if (month >= 8 && month <= 10) return months[2]; else return months[3]; } // 獲得本周一與當前日期相差的天數 public static int getMondayPlus() { Calendar cd = Calendar.getInstance(); int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK); if (dayOfWeek == 1) { return -6; } else { return 2 - dayOfWeek; } } // 獲得當前周- 周一的日期 public static Date getCurrentMonday() { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(new Date())); cal.add(Calendar.DATE, -7); return cal.getTime(); } public static Date getThisWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // 獲得當前日期是一個星期的第幾天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 設置一個星期的第一天,按中國的習慣一個星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 獲得當前日期是一個星期的第幾天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根據日歷的規則,給當前日期減去星期幾與一個星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); } /** * 獲得當月起始時間 * * @return */ public static Date getStartMounth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 獲取當前季度 起始時間 * * @return */ public static Date getStartQuarter(Date date) { SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 獲取當年起始時間 */ public static Date getStartYear(Date date) { Calendar currCal = Calendar.getInstance(); int currentYear = currCal.get(Calendar.YEAR); return getYearFirst(currentYear); } /** * 獲取某年第一天日期 * * @param year 年份 * @return Date */ public static Date getYearFirst(int year) { Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); Date currYearFirst = calendar.getTime(); return currYearFirst; } public static void main(String[] args) throws ParseException { // boolean effective=false; // try { // Date d=getMonthFirstDay(new Date()); // System.out.println(DateUtils.dateToString(d, "yyyyMMddHHmmsswww")); // } catch (Exception e) { // e.printStackTrace(); // } Date receiveTime = DateUtils.stringToDate("20131226", "yyyyMMdd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(receiveTime); int week = calendar.get(Calendar.DAY_OF_WEEK); if (week < Calendar.WEDNESDAY) { calendar.add(Calendar.DATE, -14); } else { calendar.add(Calendar.DATE, -7); } calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY); Date effectiveStart = calendar.getTime(); Date effectiveEnd = receiveTime; System.out.println(DateUtils.dateToString(effectiveStart, "yyyyMMddHHmmsswww")); System.out.println(DateUtils.dateToString(effectiveEnd, "yyyyMMddHHmmsswww")); } /** * 獲取兩個時間秒差數(帶毫秒數) * * @param dtFrom * @param dtEnd * @return */ public static double getSecsBetweenD(Date dtFrom, Date dtEnd) { BigDecimal begin = new BigDecimal(dtFrom.getTime()); BigDecimal end = new BigDecimal(dtEnd.getTime()); BigDecimal inter = end.subtract(begin); return inter.divide(new BigDecimal("1000")).doubleValue(); } }