package cn.com.dekn.common.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Date; public class DateUtil { public static Timestamp getNowTime() { return new Timestamp(System.currentTimeMillis()); } public static String getYear() { SimpleDateFormat date = new SimpleDateFormat("yy"); return date.format(new Date()); } public static String getYearYYYY() { SimpleDateFormat date = new SimpleDateFormat("yyyy"); return date.format(new Date()); } public static String getMonth() { SimpleDateFormat date = new SimpleDateFormat("MM"); return date.format(new Date()); } public static String getDay() { SimpleDateFormat date = new SimpleDateFormat("dd"); return date.format(new Date()); } public static String getHour() { SimpleDateFormat date = new SimpleDateFormat("HH"); return date.format(new Date()); } public static String getMinute() { SimpleDateFormat date = new SimpleDateFormat("mm"); return date.format(new Date()); } public static String getSecond() { SimpleDateFormat date = new SimpleDateFormat("ss"); return date.format(new Date()); } public static String getMillisecond() { SimpleDateFormat date = new SimpleDateFormat("SSS"); return date.format(new Date()); } public static boolean isToday(Timestamp time) { SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd"); if (format.format(time).equals(format.format(new Date()))) { return true; } return false; } public static String getCurrentDay(String dateFormat) { SimpleDateFormat format = new SimpleDateFormat(dateFormat); return format.format(new Date()); } public static long getDayBetween(String now, String fore) { long count = 0L; SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); try { Date nowDate = myFormatter.parse(now); Date foreDate = myFormatter.parse(fore); count = (nowDate.getTime() - foreDate.getTime()) / 86400000L; } catch (ParseException e) { count = 0L; e.printStackTrace(); } return count; } public static Timestamp getTodayTimestamp() { String nowDateStr = getCurrentDay("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Timestamp timestamp = null; try { timestamp = new Timestamp(sdf.parse(nowDateStr).getTime()); } catch (ParseException e) { e.printStackTrace(); } return timestamp; } public static Timestamp rollDayTimestamp(String dateStr, int amount) { String nowDate = dateStr; if ((nowDate == null) || (nowDate.trim().equals(""))) { nowDate = getCurrentDay("yyyy-MM-dd"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar nowCalendar = Calendar.getInstance(); try { nowCalendar.setTime(sdf.parse(nowDate)); } catch (ParseException e) { e.printStackTrace(); } nowCalendar.add(5, amount); Timestamp timestamp = new Timestamp(nowCalendar.getTimeInMillis()); return timestamp; } public static Timestamp rollMonthTimestamp(String dateStr, int amount) { String nowDate = dateStr; if ((nowDate == null) || (nowDate.trim().equals(""))) { nowDate = getCurrentDay("yyyy-MM-dd"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar nowCalendar = Calendar.getInstance(); try { nowCalendar.setTime(sdf.parse(nowDate)); } catch (ParseException e) { e.printStackTrace(); } nowCalendar.add(2, amount); Timestamp timestamp = new Timestamp(nowCalendar.getTimeInMillis()); return timestamp; } public static String format(Timestamp time, String formatStr) { if (time == null) { return null; } SimpleDateFormat format = new SimpleDateFormat(formatStr); String date = format.format(time); return date; } public static String format(String time, String formatStr) { if (StrUtil.isNull(time)) { return null; } String date = ""; try { SimpleDateFormat format = new SimpleDateFormat(formatStr); date = format.format(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime())); } catch (ParseException e) { e.printStackTrace(); return ""; } return date; } public static String getMonthStr(int month) { String monthStr = ""; switch (month) { case 1: monthStr = "一月"; break; case 2: monthStr = "二月"; break; case 3: monthStr = "三月"; break; case 4: monthStr = "四月"; break; case 5: monthStr = "五月"; break; case 6: monthStr = "六月"; break; case 7: monthStr = "七月"; break; case 8: monthStr = "八月"; break; case 9: monthStr = "九月"; break; case 10: monthStr = "十月"; break; case 11: monthStr = "十一月"; break; case 12: monthStr = "十二月"; } return monthStr; } public static int getWeekOfYear() { int week = 0; Calendar c = Calendar.getInstance(); week = c.get(3); return week; } public static String getStartDate(int year, int weekNo) { Calendar cal = Calendar.getInstance(); cal.set(1, year); cal.set(3, weekNo); cal.set(7, 2); SimpleDateFormat returnDate = new SimpleDateFormat("yyyy年MM月dd日"); return returnDate.format(cal.getTime()); } public static String getEndDate(int year, int weekNo) { Calendar cal = Calendar.getInstance(); cal.set(1, year); cal.set(3, weekNo + 1); cal.set(7, 1); SimpleDateFormat returnDate = new SimpleDateFormat("yyyy年MM月dd日"); return returnDate.format(cal.getTime()); } public static Collection getTwoDateDayList(String endDate, String beginDate) { Collection resultList = new ArrayList(); long dayCount = getDayBetween(endDate, beginDate); resultList.add(beginDate); for (int i = 0; i < dayCount; i++) { Timestamp now = rollDayTimestamp(beginDate, 1); beginDate = format(now, "yyyy-MM-dd"); resultList.add(beginDate); } return resultList; } public static Collection getTwoDateDayListDesc(String endDate, String beginDate) { Collection resultList = new ArrayList(); long dayCount = getDayBetween(endDate, beginDate); resultList.add(endDate); for (long i = dayCount; i > 0L; i -= 1L) { Timestamp now = rollDayTimestamp(endDate, -1); endDate = format(now, "yyyy-MM-dd"); resultList.add(endDate); } return resultList; } public static String getWeekForDate(String date, String format) { String week = ""; SimpleDateFormat f1 = new SimpleDateFormat(format); Date d = null; try { d = f1.parse(date); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat formatter = new SimpleDateFormat("E"); week = formatter.format(d); return week; } public static String getStandardDate(int year, int month, int day) { String yearStr = String.valueOf(year); String monthStr = String.valueOf(month); String dayStr = String.valueOf(day); if (month < 10) { monthStr = "0" + month; } if (day < 10) { dayStr = "0" + day; } return yearStr + "-" + monthStr + "-" + dayStr; } public static String rollHoursTimestamp(String dateStr, String amount) { SimpleDateFormat sdf = new SimpleDateFormat(dateStr); String timeStr = getCurrentDay(dateStr); Date time = null; try { time = sdf.parse(timeStr); } catch (ParseException e) { e.printStackTrace(); } int hours = StrUtil.getNotNullIntValue(amount, 0); Date torollHours = new Date(time.getTime() - hours * 3600 * 1000); return sdf.format(torollHours).toString(); } public static String rollMinutesTimestamp(String dateStr, String amount) { SimpleDateFormat sdf = new SimpleDateFormat(dateStr); String timeStr = getCurrentDay(dateStr); Date time = null; try { time = sdf.parse(timeStr); } catch (ParseException e) { e.printStackTrace(); } int minutes = StrUtil.getNotNullIntValue(amount, 0); Date torollMinutes = new Date(time.getTime() - minutes * 60 * 1000); return sdf.format(torollMinutes).toString(); } public static boolean compareTotime(String dateStr) { boolean isOK = false; long count = 0L; SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowtimeStr = getCurrentDay("yyyy-MM-dd HH:mm:ss"); try { Date nowDate = myFormatter.parse(nowtimeStr); Date foreDate = myFormatter.parse(dateStr); count = nowDate.getTime() - foreDate.getTime(); } catch (ParseException e) { count = 0L; e.printStackTrace(); } if (count > 0L) { isOK = true; } return isOK; } public static boolean compareTotime(String oneStr, String twoStr) { boolean isOK = false; long count = 0L; SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date nowDate = myFormatter.parse(oneStr); Date foreDate = myFormatter.parse(twoStr); count = nowDate.getTime() - foreDate.getTime(); } catch (ParseException e) { count = 0L; e.printStackTrace(); } if (count > 0L) { isOK = true; } return isOK; } public static Timestamp getNotNullTimestampValue(String data, String dateFormat) { Timestamp value; try { Timestamp value; if ((data == null) || (data.equals(""))) { value = new Timestamp(System.currentTimeMillis()); } else { SimpleDateFormat smd = new SimpleDateFormat(dateFormat); value = new Timestamp(smd.parse(data).getTime()); } } catch (Exception e) { Timestamp value; e.printStackTrace(); value = new Timestamp(System.currentTimeMillis()); } return value; } public static int compareTimstampStr(String sourceStr, String destStr, String formartStr) { int result = 10; SimpleDateFormat dateFormatter = new SimpleDateFormat(formartStr); long temp = 10L; try { Date beginDate = dateFormatter.parse(sourceStr); Date endDate = dateFormatter.parse(destStr); temp = beginDate.getTime() - endDate.getTime(); } catch (ParseException e) { result = 2147483647; e.printStackTrace(); } if (temp > 0L) result = 1; else if (temp == 0L) result = 0; else if (temp < 0L) { result = -1; } return result; } public static String getDateFormat(String date) { Timestamp nowDay = getTodayTimestamp(); String hour = date.substring(11, date.length()); date = format(date, "yyyy-MM-dd"); if (date.equals(getCurrentDay("yyyy-MM-dd"))) { return hour; } if (nowDay.equals(rollDayTimestamp(date, 1))) { return "昨天 " + hour; } if (nowDay.equals(rollDayTimestamp(date, 2))) { return "前天 " + hour; } return date + " " + hour; } public static int[] getDayBetweenForYMD(String endDate, String beginDate) { int endDateY = Integer.parseInt(format(endDate, "yyyy", "yyyy-MM-dd")); int endDateM = Integer.parseInt(format(endDate, "MM", "yyyy-MM-dd")); int endDateD = Integer.parseInt(format(endDate, "dd", "yyyy-MM-dd")); int beginDateY = Integer.parseInt(format(beginDate, "yyyy", "yyyy-MM-dd")); int beginDateM = Integer.parseInt(format(beginDate, "MM", "yyyy-MM-dd")); int beginDateD = Integer.parseInt(format(beginDate, "dd", "yyyy-MM-dd")); int mS = (endDateY - beginDateY) * 12 + endDateM - beginDateM; int y = mS / 12; int m = mS % 12; int d = (int)getDayBetween(getStandardDate(endDateY, endDateM, endDateD), getStandardDate(endDateY, endDateM, beginDateD)); if (d < 0) { m--; d += getMonthLastDay(endDateY, endDateM - 1); } if (m < 0) { y--; m += 12; } return new int[] { y, m, d }; } public static int getMonthLastDay(int year, int month) { Calendar a = Calendar.getInstance(); a.set(1, year); a.set(2, month - 1); a.set(5, 1); a.roll(5, -1); int maxDate = a.get(5); return maxDate; } public static String format(String time, String formatStr, String timeformatStr) { if (StrUtil.isNull(time)) { return null; } String date = ""; try { SimpleDateFormat format = new SimpleDateFormat(formatStr); date = format.format(new Timestamp(new SimpleDateFormat(timeformatStr).parse(time).getTime())); } catch (ParseException e) { e.printStackTrace(); return ""; } return date; } public static boolean checkDate(String date, String format) { SimpleDateFormat df = new SimpleDateFormat(format); Date d = null; try { d = df.parse(date); } catch (Exception e) { return false; } String s1 = df.format(d); return date.equals(s1); } }
例如:
public Timestamp getCONFIRM_TIME() { return CONFIRM_TIME; } public String getConfirmTime(String format){ return DateUtil.format(this.getCONFIRM_TIME(), format); } public void setCONFIRM_TIME(Timestamp cONFIRM_TIME) { CONFIRM_TIME = cONFIRM_TIME; }
在实体类里面添加个String方法Timestamp就可以转String了 format是时间格式,如:
jsonNew.put("delivery_time", order.getDeliveryTime("yyyy-MM-dd HH:mm:ss")); jsonNew.put("confirm_time", order.getConfirmTime("yyyy-MM-dd HH:mm:ss"));