package com.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils { /** * 日期转换成字符串 * * @param date * @return str */ public static String DateToStr(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = format.format(date); return str; } /** * 字符串转换成日期 * * @param str * @return date */ public static Date StrToDate(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; } /* * 获取当前时间日期的字符串 */ public static String getCurrentDateStr(DateFormatType dateFormatType) { Date date = getCurrentDate(); return (String) OpearationDate(date, dateFormatType.getValue()); } /** * 时间、日期格式化成字符串 */ public static String formatDate(Date date, DateFormatType dateFormatType) { return (String) OpearationDate(date, dateFormatType.getValue()); } /** * 从字符串解析成时间、日期 */ public static Date parseDate(String dateStr, DateFormatType dateFormatType) { return (Date) OpearationDate(dateStr, dateFormatType.getValue()); } /** * 获取当前系统时间(原始格式) */ public static Date getCurrentDate() { Date date = new Date(System.currentTimeMillis()); return date; } /** * 获取当前日期的年、月、日、时、分、秒 */ public static int getCurrentTime(TimeFormatType timeFormatType) { return getTime(getCurrentDate(), timeFormatType); } /** * 获取指定日期的年、月、日、时、分、秒 */ public static int getTime(Date date, TimeFormatType timeFormatType) { try { Calendar c = Calendar.getInstance(); c.setTime(date); int type = timeFormatType.getValue(); int i = c.get(type); return type == 2 ? i + 1 : i; } catch (Exception e) { throw new RuntimeException("获取失败", e); } } /** * 获取指定日期的毫秒数 */ public static long getMillis(Date date) { java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(date); return c.getTimeInMillis(); } /** * 日期相加、减操作 * * 所返回结果单位为:天数 */ public static int operationDate(Date date, Date diffDate, DateOperationType dateOperationType) { long add = getMillis(date) + getMillis(diffDate); long diff = getMillis(date) - getMillis(diffDate); return (int) ((dateOperationType.getValue() ? add : diff) / (24 * 3600 * 1000)); } /** * 日期月份相加、减操作 */ public static Date operationDateOfMonth(Date date, int month, DateOperationType dateOperationType) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.MONTH, dateOperationType.getValue() ? month : month - (month * 2)); return c.getTime(); } /** * 日期天数相加、减操作 */ public static Date operationDateOfDay(Date date, int day, DateOperationType dateOperationType) { Calendar c = Calendar.getInstance(); c.setTime(date); long millis = c.getTimeInMillis(); long millisOfday = day * 24 * 3600 * 1000; long sumMillis = dateOperationType.getValue() ? (millis + millisOfday) : (millis - millisOfday); c.setTimeInMillis(sumMillis); return c.getTime(); } /** * 日期小时相加、减操作 */ public static Date operationDateOfHour(Date date, int hour, DateOperationType dateOperationType) { Calendar c = Calendar.getInstance(); c.setTime(date); long millis = c.getTimeInMillis(); long millisOfday = hour * 3600 * 1000; long sumMillis = dateOperationType.getValue() ? (millis + millisOfday) : (millis - millisOfday); c.setTimeInMillis(sumMillis); return c.getTime(); } private static Object OpearationDate(Object object, String formatStr) { if (object == null || null == formatStr || "".equals(formatStr)) { throw new RuntimeException("参数不能为空"); } SimpleDateFormat format = new SimpleDateFormat(formatStr); try { if (object instanceof Date) return format.format(object); else return format.parse(object.toString()); } catch (Exception e) { throw new RuntimeException("操作失败", e); } } /** * 获取给定日期所在月份的第一天对应时间,时分秒由time字符串给定(如:"00:00:00") * @param date * @param time * @return */ public static Date getFirstDayOfMonth(Date date, String time) { DateFormat df1 = new SimpleDateFormat("yyyy-MM"); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = df1.format(date); try { Date returnDate = df2.parse(dateStr + "-01 " + time); return returnDate; } catch (ParseException e) { // TODO Auto-generated catch block throw new RuntimeException("日期转换错误", e); } } /** * 获取给定日期所在月份的最后一天对应时间,时分秒由time字符串给定(如:"23:59:59") * @param date * @param time * @return */ public static Date getLastDateOfMonth(Date date, String time) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); String dateStr = df.format(c.getTime()); try { Date returnDate = df1.parse(dateStr + " " + time); return returnDate; } catch (ParseException e) { // TODO Auto-generated catch block throw new RuntimeException("日期转换错误", e); } } public enum DateOperationType { /** * 加操作 */ ADD(true), /** * 减操作 */ DIFF(false); private final boolean value; DateOperationType(boolean operation) { this.value = operation; } public boolean getValue() { return value; } } public enum TimeFormatType { YEAR(1), MONTH(2), DAY(5), HOUR(11), MINUTE(12), SECOND(13); private final int value; TimeFormatType(int formatStr) { this.value = formatStr; } public int getValue() { return value; } } public enum DateFormatType { /** * 格式为:yyyy-MM-dd HH:mm:ss */ DATE_FORMAT_STR("yyyy-MM-dd HH:mm:ss"), /** * 格式为:yyyyMMddHHmmss */ SIMPLE_DATE_TIME_FORMAT_STR("yyyyMMddHHmmss"), /** * 格式为:yyyyMMddHHmmss */ SIMPLE_DATE_FORMAT_NUM("yyyyMMdd"), /** * 格式为:yyyy-MM-dd */ SIMPLE_DATE_FORMAT_STR("yyyy-MM-dd"), /** * 格式为:yyyy/MM/dd */ SIMPLE_DATE_FORMAT_VIRGULE_STR("yyyy/MM/dd"), /** * 格式为:HH:mm:ss */ HOUR_MINUTE_SECOND("HH:mm:ss"), /** * 格式为:HH:mm */ HOUR_MINUTE("HH:mm"); private final String value; DateFormatType(String formatStr) { this.value = formatStr; } public String getValue() { return value; } } public static void main(String[] args) { // Date date = new Date(); // System.out.println("日期转字符串:" + DateUtils.DateToStr(date)); // System.out.println("字符串转日期:" + // DateUtils.StrToDate(DateUtils.DateToStr(date))); System.out.println(DateUtils.DateToStr(operationDateOfDay(new Date(), 1, DateOperationType.DIFF))); System.out.println(operationDate(new Date(), DateUtils .StrToDate(DateUtils.DateToStr(operationDateOfDay(new Date(), 1, DateOperationType.DIFF))), DateOperationType.DIFF)); System.out.println(DateUtils.DateToStr(DateUtils.parseDate(DateUtils .DateToStr(DateUtils.operationDateOfDay(new Date(), 1, DateOperationType.ADD)), DateFormatType.SIMPLE_DATE_FORMAT_STR))); System.out.println("------=" + DateUtils.DateToStr(DateUtils.parseDate( DateUtils.DateToStr(new Date()), DateFormatType.SIMPLE_DATE_FORMAT_STR))); System.out.println("------=" + DateUtils.getTime(new Date(), TimeFormatType.YEAR) + DateUtils.getTime(new Date(), TimeFormatType.MONTH) + DateUtils.getTime(new Date(), TimeFormatType.DAY)); String ym = "" + DateUtils.getTime(new Date(), TimeFormatType.YEAR) + DateUtils.getTime(new Date(), TimeFormatType.MONTH) + DateUtils.getTime(new Date(), TimeFormatType.DAY); System.out.println("===" + Integer.valueOf(ym.trim())); System.out.println(new Date()); System.out.println("-----------------------------------------"); System.out.println(DateUtils.DateToStr(operationDateOfHour(new Date(), 80, DateOperationType.DIFF))); } }