DateUtils日期转换工具

代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 *
 * <p>
 * Description:日期转换工具<br />
 * </p>
 *
 * @title DateUtils.java
 * @package com.util
 * @author yangshijin
 * @version 0.1 2014年8月20日
 */
public class DateUtils {
 /** 日期格式 */
 public static final String YMD_HMS = "yyyy-MM-dd HH:mm:ss";
 public static final String YMDHMS = "yyyyMMddHHmmss";
 public static final String YMD = "yyyyMMdd";
 public static final String YM = "yyyy-MM";
 public static final String MD = "MMdd";
 public static final String MD_DASH = "MM-dd";
 public static final String YMD_SLASH = "yyyy/MM/dd";
 public static final String YMD_DASH = "yyyy-MM-dd";
 public static final String YMD_DASH_WITH_TIME = "yyyy-MM-dd H:m";
 public static final String DATETIME_YMD_DASH = "yyyy-MM-dd HH:mm";
 public static final String YDM_SLASH = "yyyy/dd/MM";
 public static final String YMD_SLAHMS = "yyyy/MM/dd HH:mm:ss ";
 public static final String YDM_DASH = "yyyy-dd-MM";
 public static final String HM = "HHmm";
 public static final String HM_COLON = "HH:mm";
 public static final String HMS_COLON = "HH:mm:ss";
 public static final String YM_DASH = "yyyy-MM";
 public static final String YMD_NYRSH = "yyyy年MM月dd日";
 public static final long DAY = 24 * 60 * 60 * 1000L;
 public static final long MINUTE = 60 * 1000L;
 public static final long SECOND = 1000L;

 private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

 /**
  *
  * <p>
  * Description:获取当前时间的时间戳(精确到秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return String
  */
 public static String getCurrentTimeStamp() {
  return String.valueOf(System.currentTimeMillis() / 1000L);
 }

 /**
  *
  * <p>
  * Description:获取当前时间的时间戳(精确到毫秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return Long
  */
 public static Long getCurrentTimeStamps() {
  return new Date().getTime();
 }

 /**
  *
  * <p>
  * Description:将指定的日期格式字符串转换为时间戳(精确到秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            格式:yyyy-MM-dd
  * @return String
  */
 public static String date2TimeStamp(String date) {
  try {
   SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
   Date s = dateformat.parse(date);
   return String.valueOf(s.getTime() / 1000L);
  } catch (ParseException e) {
   e.printStackTrace();
  }
  return null;
 }

 /**
  *
  * <p>
  * Description:将指定的日期格式字符串转换为时间戳(精确到秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            格式:yyyy-MM-dd HH:mm:ss
  * @return String
  */
 public static String dateTime2TimeStamp(String date) {
  try {
   SimpleDateFormat dateformat = new SimpleDateFormat(
     "yyyy-MM-dd HH:mm:ss");
   Date s = dateformat.parse(date);
   return String.valueOf(s.getTime() / 1000L);
  } catch (ParseException e) {
   e.printStackTrace();
  }
  return null;
 }

 /**
  *
  * <p>
  * Description:获取指定时间的时间戳(精确到秒)<br />
  * </p>
  *
  * @author yangshijin
  * @version 0.1 2014年8月20日
  * @param date
  * @return Long
  */
 public static Long getTimeStampByDate(Date date) {
  return date.getTime() / 1000L;
 }

 /**
  *
  * <p>
  * Description:获取指定时间的时间戳(精确到毫秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  * @return Long
  */
 public static Long getTimeStampsByDate(Date date) {
  return date.getTime();
 }

 /**
  *
  * <p>
  * Description:当前日期的前N天或后N天的时间戳(精确到秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param days
  *            当为负整数时,为前N天,当为正整数时,为后N天
  * @return String
  */
 public static String getCurrentDayTime(int days) {
  Calendar cal = Calendar.getInstance();
  cal.add(6, days);
  return String.valueOf(cal.getTimeInMillis() / 1000L);
 }

 /**
  *
  * <p>
  * Description:指定日期的前N天或后N天的时间戳(精确到秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param days
  *            当为负整数时,为前N天,当为正整数时,为后N天
  * @return String
  */
 public static String getDayTime(Date date, int days) {
  String dateStr = format(dayOffset(date, days), YMD_HMS);
  return dateTime2TimeStamp(dateStr);
 }

 /**
  *
  * <p>
  * Description:当前日期最后一秒的时间戳(精确到秒)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return String 如:2013-10-30 23:59:59的时间戳
  */
 public static String getTimeDayLastSecond() {
  return dateTime2TimeStamp(TimeStamp2Date(String.valueOf(System
    .currentTimeMillis())) + " 23:59:59");
 }

 /**
  *
  * <p>
  * Description:将指定的时间戳字符串转换为指定日期格式字符串<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param timestampStr
  *            时间戳字符串(精确到秒)
  * @param pattern
  *            日期时间格式
  * @return String
  */
 public static String timeStampToDate(String timestampStr, String pattern) {
  Long timestamp = Long.parseLong(timestampStr) * 1000;
  String date = new java.text.SimpleDateFormat(pattern)
    .format(new java.util.Date(timestamp));
  return date;
 }

 /**
  *
  * <p>
  * Description:将指定的时间戳字符串转换为日期格式<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param timestampStr
  *            时间戳字符串(精确到秒)
  * @return Date
  */
 public static Date timeStampToDate(String timestampStr) {
  Long timestamp = Long.parseLong(timestampStr) * 1000;
  return new java.util.Date(timestamp);
 }

 /**
  *
  * <p>
  * Description:将指定的时间戳字符串转换为日期格式字符串<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param timestampStr
  *            时间戳字符串 (精确到秒)
  * @return 转换后格式为 yyyy-MM-dd HH:mm:ss String
  */
 public static String TimeStamp2DateTime(String timestampStr) {
  Long timestamp = Long.valueOf(Long.parseLong(timestampStr));
  String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    .format(new Date(timestamp.longValue()));
  return date;
 }

 /**
  *
  * <p>
  * Description:将指定的时间戳字符串转换为日期格式字符串<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param timestampStr
  *            时间戳字符串 (精确到秒)
  * @return 转换后格式为 yyyy-MM-dd String
  */
 public static String TimeStamp2Date(String timestampStr) {
  Long timestamp = Long
    .valueOf(timestampStr.length() < 13 ? Long
      .parseLong(timestampStr) * 1000L : Long
      .parseLong(timestampStr));
  String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
    timestamp.longValue()));
  return date;
 }

 /**
  *
  * <p>
  * Description:将日期的字符串形式按格式转换成日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param source
  *            日期格式字符串
  * @param pattern
  *            日期格式
  * @return Date
  */
 public static Date parse(String source, String pattern) {
  if (source == null) {
   return null;
  }
  Date date;
  try {
   date = getFormat(pattern).parse(source);
  } catch (ParseException e) {
   e.getStackTrace();
   return null;
  }
  return date;
 }

 /**
  *
  * <p>
  * Description:按格式要求,将日期按字符串格式输出<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param pattern
  *            日期格式
  * @return String
  */
 public static String format(Date date, String pattern) {
  if (date == null) {
   return null;
  }
  return getFormat(pattern).format(date);
 }

 /**
  *
  * <p>
  * Description:按格式要求,按DateFormat格式输出<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param pattern
  * @return DateFormat
  */
 public static DateFormat getFormat(String pattern) {
  DateFormat format = DFS.get(pattern);
  if (format == null) {
   format = new SimpleDateFormat(pattern);
   DFS.put(pattern, format);
  }
  return format;
 }

 /**
  *
  * <p>
  * Description:按格式要求,获得当前时间,按字符串格式输出<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param pattern
  *            日期格式
  * @return String
  */
 public static String getNow(String pattern) {
  return DateFormatUtils.format(new Date(), pattern);
 }

 /**
  *
  * <p>
  * Description:判断输入的年、月、日是否是有效日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param year
  *            年
  * @param month
  *            月(1-12)
  * @param day
  *            日(1-31)
  * @return boolean
  */
 public static boolean isValid(int year, int month, int day) {
  if (month > 0 && month < 13 && day > 0 && day < 32) {
   // month of calendar is 0-based
   int mon = month - 1;
   Calendar calendar = new GregorianCalendar(year, mon, day);
   if (calendar.get(Calendar.YEAR) == year
     && calendar.get(Calendar.MONTH) == mon
     && calendar.get(Calendar.DAY_OF_MONTH) == day) {
    return true;
   }
  }
  return false;
 }

 /**
  *
  * <p>
  * Description:将指定日期转换为日历格式的日期对象<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @return Calendar
  */
 private static Calendar convert(Date date) {
  Calendar calendar = new GregorianCalendar();
  calendar.setTime(date);
  return calendar;
 }

 /**
  * 返回指定年数位移动后的日期
  *
  * @param date
  *            指定日期
  * @param offset
  *            当为负整数时日期前移,当为正整数是日期后期
  * @return
  */
 /**
  *
  * <p>
  * Description:返回指定日期按年份【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date yearOffset(Date date, int offset) {
  return offsetDate(date, Calendar.YEAR, offset);
 }

 /**
  *
  * <p>
  * Description:返回指定日期按月份【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date monthOffset(Date date, int offset) {
  return offsetDate(date, Calendar.MONTH, offset);
 }

 /**
  *
  * <p>
  * Description:返回指定日期按天数【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date dayOffset(Date date, int offset) {
  return offsetDate(date, Calendar.DATE, offset);
 }

 /**
  *
  * <p>
  * Description:返回指定日期按小时数【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date hoursOffset(Date date, int offset) {
  return offsetDate(date, Calendar.HOUR, offset);
 }

 /**
  *
  * <p>
  * Description:返回指定日期按分钟数【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date minuteOffset(Date date, int offset) {
  return offsetDate(date, Calendar.MINUTE, offset);
 }

 /**
  *
  * <p>
  * Description:返回指定日期按秒数【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date secondOffset(Date date, int offset) {
  return offsetDate(date, Calendar.SECOND, offset);
 }

 /**
  *
  * <p>
  * Description:返回指定日期按指定方式【前移】或【后移】后的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @param field
  *            指定方式,如:Calendar.YEAR 按年份,Calendar.MONTH 按月份,Calendar.DATE 按天数
  * @param offset
  *            当为正整数,则后移,当为负整数,则前移
  * @return Date
  */
 public static Date offsetDate(Date date, int field, int offset) {
  Calendar calendar = convert(date);
  calendar.add(field, offset);
  return calendar.getTime();
 }

 /**
  *
  * <p>
  * Description:返回指定日期所在当月的第一天的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @return Date
  */
 public static Date firstDay(Date date) {
  Calendar calendar = convert(date);
  calendar.set(Calendar.DATE, 1);
  return calendar.getTime();
 }

 /**
  *
  * <p>
  * Description:返回指定日期所在当月的最后一天的日期<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期
  * @return Date
  */
 public static Date lastDay(Date date) {
  Calendar calendar = convert(date);
  calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
  return calendar.getTime();
 }

 /**
  *
  * <p>
  * Description:返回两个日期间的差异天数<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date1
  *            参照日期
  * @param date2
  *            比较日期
  * @return 参照日期与比较日期之间的天数差异,正数表示参照日期在比较日期之后,0表示两个日期同天,负数表示参照日期在比较日期之前 int
  */
 public static int dayDiff(Date date1, Date date2) {
  long diff = date1.getTime() - date2.getTime();
  return (int) (diff / DAY);
 }

 public static int dayDiffday(Date d1, Date d2) throws ParseException {
  SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");

  Date cDate = df.parse(df.format(d1));
  Date dDate = df.parse(df.format(d2));
  int diff = (int) ((cDate.getTime() - dDate.getTime()) / (24 * 60 * 60 * 1000));
  return diff;
 }

 /**
  *
  * <p>
  * Description:返回两个日期间的差异分钟数<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date1
  *            参照日期
  * @param date2
  *            比较日期
  * @return 参照日期与比较日期之间的分钟数差异,正数表示参照日期在比较日期之后,0表示两个日期同分,负数表示参照日期在比较日期之前 int
  */
 public static int minuteDiff(Date date1, Date date2) {
  long diff = date1.getTime() - date2.getTime();
  return (int) (diff / MINUTE);
 }

 /**
  *
  * <p>
  * Description:返回两个日期间的差异秒数<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date1
  *            参照日期
  * @param date2
  *            比较日期
  * @return 参照日期与比较日期之间的秒数差异,正数表示参照日期在比较日期之后,0表示两个日期同分,负数表示参照日期在比较日期之前 int
  */
 public static int secondDiff(Date date1, Date date2) {
  long diff = date1.getTime() - date2.getTime();
  return (int) (diff / SECOND);
 }

 /**
  *
  * <p>
  * Description:将指定日期转换为小时数<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  * @return Double
  */
 public static Double hour(Date date) {
  SimpleDateFormat st = new SimpleDateFormat("yyyyMMddHH");
  return Double.parseDouble(st.format(date));
 }

 /**
  *
  * <p>
  * Description:将日期转化成 mysql 的 str_to_date(date,'%Y-%m-%d %H:%i:%s') 格式<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            指定日期格式字符串
  * @param hqlFormat
  *            mysql的日期格式,例如:'%Y-%m-%d %H:%i:%s'
  * @return String
  */
 public static String toDate(String date, String hqlFormat) {
  StringBuffer bf = new StringBuffer();
  bf.append("str_to_date('");
  bf.append(date);
  bf.append("','");
  bf.append(hqlFormat);
  bf.append("')");
  return bf.toString();
 }

 /**
  *
  * <p>
  * Description:将日期转化成 时间戳(mysql语法)<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param date
  *            日期格式字符串,如 yyyy-MM-dd HH:mm:ss
  * @return String
  */
 public static String unixTimestamp(String date) {
  StringBuffer bf = new StringBuffer();
  bf.append("unix_timestamp('");
  bf.append(date);
  bf.append("')");
  return bf.toString();
 }

 /**
  *
  * <p>
  * Description:判断当前时间是否超过某个指定日期一天的时间<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param end
  *            指定日期
  * @return 当前时间大于指定时间一天时返回true boolean
  */
 public static boolean isDateExpired(Date end) {
  if (end == null)
   return false;
  if (new Date().after(new Date(end.getTime() + 24 * 60 * 60 * 1000))) {
   return true;
  }
  return false;

 }

 /**
  *
  * <p>
  * Description:获取当月的最后一天是几号<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return int
  */
 public static int getMonthDays() {
  Calendar calendar = Calendar.getInstance();
  int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  return maxDay;
 }

 /**
  *
  * <p>
  * Description:获取今天是几号<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return int
  */
 public static int getDateByDay() {
  Calendar calendar = Calendar.getInstance();
  return calendar.get(Calendar.DATE);
 }

 /**
  *
  * <p>
  * Description:获取今天是几月<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return int
  */
 public static int getMonthByDay() {
  Calendar calendar = Calendar.getInstance();
  return calendar.get(Calendar.MONTH) + 1;
 }

 /**
  *
  * <p>
  * Description:获取今天是那年<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return int
  */
 public static int getYearByDay() {
  Calendar calendar = Calendar.getInstance();
  return calendar.get(Calendar.YEAR);
 }

 /**
  *
  * <p>
  * Description:获取指定日期是星期几<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param dt
  *            指定日期
  * @return String
  */
 public static String getWeekOfDate(Date dt) {
  String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  Calendar cal = Calendar.getInstance();
  cal.setTime(dt);
  int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  if (w < 0)
   w = 0;
  return weekDays[w];
 }

 /**
  *
  * <p>
  * Description:获取给定日期当月的工作日集合<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param year
  *            指定年份
  * @param month
  *            指定月份
  * @return List<Date>
  */
 public static List<Date> getDates(int year, int month) {
  List<Date> dates = new ArrayList<Date>();

  Calendar cal = Calendar.getInstance();
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.MONTH, month - 1);
  cal.set(Calendar.DATE, 1);

  while (cal.get(Calendar.YEAR) == year
    && cal.get(Calendar.MONTH) < month) {
   int day = cal.get(Calendar.DAY_OF_WEEK);

   if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
    dates.add((Date) cal.getTime().clone());
   }
   cal.add(Calendar.DATE, 1);
  }
  return dates;

 }

 /**
  *
  * <p>
  * Description:取得当月天数<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @return int
  */
 public static int getCurrentMonthLastDay() {
  Calendar a = Calendar.getInstance();
  a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
  a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
  int maxDate = a.get(Calendar.DATE);
  return maxDate;
 }

 /**
  *
  * <p>
  * Description:得到指定月的天数<br />
  * </p>
  *
  * @author 杨仕金
  * @version 0.1 2013年10月30日
  * @param year
  *            指定年份
  * @param month
  *            指定月份
  * @return int
  */
 public static int getMonthLastDay(int year, int month) {
  Calendar a = Calendar.getInstance();
  a.set(Calendar.YEAR, year);
  a.set(Calendar.MONTH, month - 1);
  a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
  a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
  int maxDate = a.get(Calendar.DATE);
  return maxDate;
 }

 public static void main(String[] args) {
  System.out.println(getCurrentMonthLastDay());
  System.out.println(getMonthLastDay(2013, 2));
  System.err.println(getCurrentTimeStamp());
  System.err.println(getTimeStampByDate(new Date()));
  System.err.println(getTimeStampsByDate(new Date()));
 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值