| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731 |
- package com.ydtech.utils;
- import com.github.pagehelper.util.StringUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.util.ObjectUtils;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * 功能:对日期操作的公共方法类 作者:袁伟民 时间:2009-7-29 21:16 修改:
- */
- public class DateUtil_N {
- /**
- * log日志
- */
- private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
- private static final String DATEFORMAT = "yyyy-MM-dd";
- public static Date getNextNDate;
- private DateUtil_N() {
- }
- /***
- * 格式化日期的方法
- *
- * @param date
- * 带格式化的日期
- * @param pattern
- * 格式化的表达式 比如 yyyy-MM-dd
- * @return 格式化后的字符串
- */
- public static String formatDate(Date date, String pattern) {
- // 建立日期FORMAT的实例
- SimpleDateFormat df = new SimpleDateFormat(pattern);
- if (null != date) {
- return df.format(date);
- }
- return null;
- }
- /***
- * 格式化日期的方法
- *
- * @param date
- * 带格式化的日期,默认为yyyy-MM-dd
- * @return 格式化后的字符串
- */
- public static String formatDate(Date date) {
- return formatDate(date, DATEFORMAT);
- }
- /**
- * 格式化日期的方法
- *
- * @param object
- * @return
- */
- public static String getDate(Object object) {
- //小写的mm表示的是分钟
- SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
- return object != null ? sdf.format((Date) object) : null;
- }
- /***
- * 转化成中文日期的方法
- *
- * @param date
- * 带格式化的日期,默认为yyyy-MM-dd
- * @return 格式化后的字符串
- */
- public static String formatCDate(Date date) {
- if (null != date) {
- String dateNew = formatDate(date, DATEFORMAT);
- if (null != dateNew && StringUtil.isNotEmpty(dateNew)) {
- String[] dates = dateNew.split("-");
- if (!ObjectUtils.isEmpty(dates)) {
- return dates[0] + "年" + dates[1] + "月" + dates[2] + "日";
- } else {
- return dateNew;
- }
- }
- }
- return "";
- }
- /***
- * 转化成中文日期的方法(年、月)
- *
- * @param date
- * 带格式化的日期,默认为yyyy-MM
- * @return 格式化后的字符串
- */
- public static String formatCYearAndMonth(Date date) {
- if (null != date) {
- String dateNew = formatDate(date, "yyyy-MM");
- String[] dates = dateNew.split("-");
- if (dates != null) {
- return dates[0] + "年" + dates[1] + "月";
- } else {
- return dateNew;
- }
- }
- return null;
- }
- /***
- * 格式化日期的方法
- *
- * @param date
- * 带格式化的日期,默认为yyyy-MM
- * @return 格式化后的字符串
- */
- public static String formatYearAndMonth(Date date) {
- return formatDate(date, "yyyy-MM");
- }
- /**
- * 利用传入的日期,返回下个月最后一天的日期
- * guoyonggang
- *
- * @return
- */
- public static Date getNextMonthEnd(Date current) {
- Calendar lastDate = Calendar.getInstance();
- lastDate.setTime(current);
- // 加一个月
- lastDate.add(Calendar.MONTH, 1);
- // 把日期设置为当月第一天
- lastDate.set(Calendar.DATE, 1);
- // 日期回滚一天,也就是本月最后一天
- lastDate.roll(Calendar.DATE, -1);
- return lastDate.getTime();
- }
- /***
- * 获取传入日期下一年的日期对象
- *
- * @param date
- * 传入的日期对象
- * @return 下一年的日期对象
- */
- public static Date getNextYear(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.YEAR, 1);
- return calendar.getTime();
- }
- /***
- * 获取传入日期下n年的日期对象
- *
- * @param date
- * 传入的日期对象
- * @return 下n年的日期对象
- */
- public static Date getNextNYear(Date date, int n) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.YEAR, n);
- return calendar.getTime();
- }
- /***
- * 计算传入日期的往后顺延N天以后的日期对象
- *
- * @param date
- * 传入的日期对象
- * @param n
- * 往后顺延的天数
- * @return 顺延后的日期对象
- */
- public static Date getPreviousNDate(Date date, long n) {
- long time = date.getTime();
- // 用毫秒数来计算新的日期
- time = time - n * 1000 * 24 * 60 * 60;
- return new Date(time);
- }
- /***
- * 计算传入日期的往前推算N天的日期对象
- *
- * @param date
- * 传入的日期对象
- * @param n
- * 往后顺延的天数
- * @return 往前推算后的日期对象
- */
- public static Date getNextNDate(Date date, long n) {
- long time = date.getTime();
- // 用毫秒数来计算新的日期
- time = time + n * 1000 * 24 * 60 * 60;
- return new Date(time);
- }
- /***
- * 计算传入日期的前一天
- *
- * @param date
- * 传入日期
- * @return 传入日期的前一天的日期对象
- */
- public static Date getPreviousDate(Date date) {
- return getPreviousNDate(date, 1);
- }
- /***
- * 计算传入日期的前一天
- *
- * @param date
- * 传入日期
- * @return 传入日期的前一天的日期对象
- */
- public static String getPreviousDate(String date) throws Exception {
- SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd");
- Date curMonthStart_wj = getPreviousDate(sdfm.parse(date));
- return dateToStr(curMonthStart_wj);
- }
- /***
- * 计算传入日期的后一天
- *
- * @param date
- * 传入日期
- * @return 传入日期的后一天的日期对象
- */
- public static Date getNextDate(Date date) {
- return getNextNDate(date, 1);
- }
- /**
- * 日期转换成字符串
- *
- * @param date
- * @return str
- * @作者:滑立敏
- * @日期:2010-01-26
- */
- public static String dateToStr(Date date) {
- SimpleDateFormat format = new SimpleDateFormat(DATEFORMAT);
- return format.format(date);
- }
- /**
- * 字符串转换成日期
- *
- * @param str
- * @return date
- * @作者:滑立敏
- * @日期:2010-01-26
- */
- public static Date strToDate(String str) {
- SimpleDateFormat format = new SimpleDateFormat(DATEFORMAT);
- Date date = null;
- try {
- date = format.parse(str);
- } catch (ParseException e) {
- logger.info(e.getMessage());
- }
- return date;
- }
- /**
- * 根据传入的日期计算此日期与下一年该日相差的天数
- *
- * @param startDate 起始日期
- * @return 天数区间
- * @作者:李阳
- * @时间:2010-06-22
- */
- public static int getDaysCountForYear(Date startDate) {
- // 获取传入日期下一年的日期对象
- Date nextYear = getNextYear(startDate);
- // 计算天数
- return getDaysCount(startDate, nextYear);
- }
- /***
- * 根据起始日期、终止日期计算天数
- *
- * @param startDate
- * 起始日期
- * @param endDate
- * 终止日期
- * @return 天数区间
- */
- public static int getDaysCount(Date startDate, Date endDate) {
- return getDaysCount(startDate, 0, endDate, 0);
- }
- /**
- * 根据起始日期、终止日期,起始小时,终止小时计算天数
- *
- * @param startDate
- * @param endDate
- * @param startHour
- * @param endHour
- * @return
- */
- public static int getDaysCounts(Date startDate, Date endDate, int startHour, int endHour) {
- return getDaysCount(startDate, startHour, endDate, endHour);
- }
- /***
- * 根据年数返回所包含的天数
- *
- * @param year
- * 年数
- * @return 该年所包含的天数
- */
- public static int getDaysFromYear(int year) {
- // 判断是平年还是闰年
- boolean flag = (year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0);
- if (flag) {
- return 366;
- } else {
- return 365;
- }
- }
- /**
- * 判斷是否是閏年
- *
- * @param year
- */
- public static boolean isLeapYear(int year) {
- boolean flag1 = (year % 4 == 0);
- boolean flag2 = (year % 100 == 0);
- boolean flag3 = (year % 400 == 0);
- return (flag1 && !flag2) || (flag3);
- }
- /***
- * 根据起始日期、起始时间、终止日期、终止时间计算天数
- *
- * @param startDate
- * 起始日期
- * @param startHour
- * 起始小时
- * @param endDate
- * 终止日期
- * @param endHour
- * 终止小时
- * @return 天数区间
- */
- public static int getDaysCount(Date startDate, int startHour, Date endDate,
- int endHour) {
- // 根据起始日期计算起始的毫秒
- long startTime = startDate.getTime();
- // 根据终止日期计算终止的毫秒
- long endTime = endDate.getTime();
- // 通过起始毫秒和终止毫秒的差值,计算天数
- int dayCount = (int) ((endTime - startTime) / (24 * 60 * 60) + 1);
- if (endHour <= startHour) {
- if (startHour == 24 && endHour == 0) {
- dayCount = dayCount - 2;
- } else {
- dayCount = dayCount - 1;
- }
- }
- return dayCount;
- }
- /***
- * 根据起始日期、起始时间、终止日期、终止时间计算天数
- *
- * @param startDate
- * 起始日期
- * @param startHour
- * 起始小时
- * @param endDate
- * 终止日期
- * @param endHour
- * 终止小时
- * @return 天数区间
- */
- public static int getHour(Date startDate, int startHour, Date endDate,
- int endHour) {
- // 根据起始日期计算起始的毫秒
- long startTime = startDate.getTime();
- // 根据终止日期计算终止的毫秒
- long endTime = endDate.getTime();
- // 通过起始毫秒和终止毫秒的差值,计算天数
- int dayCount = (int) ((endTime - startTime) / (24 * 60 * 60) + 1);
- if (endHour <= startHour) {
- dayCount = dayCount - 2;
- } else {
- dayCount = dayCount - 1;
- }
- return dayCount;
- }
- /**
- * 根据日期获取总小时数
- *
- * @param date 日期对象
- * @return 总小时数
- */
- public static int getHoursCount(Date date) {
- return getHoursCount(date, 0);
- }
- /**
- * 根据日期和小时数,获取总小时数
- *
- * @param date 日期对象
- * @param hour 小时数
- * @return 总小时数
- */
- public static int getHoursCount(Date date, Integer hour) {
- long hourCount = hour;
- hourCount += date.getTime() / 3600 / 1000;
- return (int) hourCount;
- }
- /***
- * 比较两个日期的大小
- *
- * @param startDate
- * 开始日期
- * @param endDate
- * 结束日期
- * @return -1 : 开始日期小于结束日期 0 : 开始日期等于结束日期 1 : 开始日期大于结束日期
- */
- public static int compareDate(Date startDate, Date endDate) {
- long startTime = startDate.getTime();
- long endTime = endDate.getTime();
- if (startTime < endTime) {
- return -1;
- }
- if (startTime == endTime) {
- return 0;
- }
- return 1;
- }
- /***
- * 比较两个日期的大小
- *
- * @param startDate
- * 开始日期
- * @param endDate
- * 结束日期
- * @return -1 : 开始日期小于结束日期 0 : 开始日期等于结束日期 1 : 开始日期大于结束日期
- */
- public static int compareDate(String startDate, String endDate) {
- try {
- return compareDate(strToDate(startDate), strToDate(endDate));
- } catch (Exception e) {
- logger.info(e.getMessage());
- }
- return 0;
- }
- /**
- * 数字转中文数字
- *
- * @param src
- * @return
- */
- public static String intToChineseNum(int src) {
- final String[] num = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
- final String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
- StringBuilder dst = new StringBuilder();
- String s = String.valueOf(src);
- for (int i = 0; i < s.length(); i++) {
- String index = String.valueOf(s.charAt(i));
- dst = dst.append(num[Integer.parseInt(index)]);
- }
- String sss = String.valueOf(dst);
- int i = 0;
- for (int j = sss.length(); j > 0; j--) {
- dst = dst.insert(j, unit[i++]);
- }
- return dst.toString();
- }
- /**
- * 金额格式化,加上千分符
- *
- * @param bd
- * @return
- */
- public static String getFormatNumber(BigDecimal bd) {
- DecimalFormat df = new DecimalFormat(",###,##0.00");
- if (bd != null) {
- bd = bd.setScale(2, RoundingMode.HALF_UP);
- return df.format(bd);
- } else {
- return "0.00";
- }
- }
- public static String getDay(Date date) {
- return new SimpleDateFormat("dd").format(date);
- }
- public static String getMonth(Date date) {
- return new SimpleDateFormat("MM").format(date);
- }
- public static String getYear(Date date) {
- return new SimpleDateFormat("yyyy").format(date);
- }
- public static String getHour(Date date) {
- return new SimpleDateFormat("HH").format(date);
- }
- /**
- * 利用传入的日期,获取本月的第一天
- *
- * @return
- */
- public static String getMonthStart(Date current) {
- Calendar lastDate = Calendar.getInstance();
- lastDate.setTime(current);
- lastDate.set(Calendar.DATE, 1);
- return formatDate(lastDate.getTime());
- }
- /**
- * 利用传入的日期,获取本月的最后一天
- *
- * @return
- */
- public static String getMonthEnd(Date current) {
- Calendar lastDate = Calendar.getInstance();
- lastDate.setTime(current);
- // 加一个月
- lastDate.add(Calendar.MONTH, 1);
- // 把日期设置为当月第一天
- lastDate.set(Calendar.DATE, 1);
- // 日期较少一天,也就不上个月的最后一天
- return formatDate(getPreviousDate(lastDate.getTime()));
- }
- /**
- * 利用传入的日期,获取去年当月的第一天
- *
- * @return
- */
- public static String getPreMonthStart(Date current) {
- Calendar lastDate = Calendar.getInstance();
- lastDate.setTime(current);
- // 减一年
- lastDate.add(Calendar.YEAR, -1);
- lastDate.set(Calendar.DATE, 1);
- return formatDate(lastDate.getTime());
- }
- /**
- * 利用传入的日期,获取上月的最后一天
- *
- * @return
- */
- public static String getPreMonthEnd(Date current) {
- Calendar c = Calendar.getInstance();
- c.setTime(current);
- // 把日期设置为当月第一天
- c.set(Calendar.DATE, 1);
- // 日期较少一天,也就不上个月的最后一天
- return formatDate(getPreviousDate(c.getTime()));
- }
- /**
- * 获取当前季度
- */
- public static int getQuarter(Date current) {
- Calendar c = Calendar.getInstance();
- c.setTime(current);
- int month = c.get(Calendar.MONTH) + 1;
- int quarter;
- if (month >= 1 && month <= 3) {
- quarter = 1;
- } else if (month >= 4 && month <= 6) {
- quarter = 2;
- } else if (month >= 7 && month <= 9) {
- quarter = 3;
- } else {
- quarter = 4;
- }
- return quarter;
- }
- /**
- * 根据传入的日期获取该季度的第一天和最后一天
- *
- * @param
- */
- public static String[] getCurrQuarter(Date current) {
- String[] s = new String[2];
- String str;
- // 设置本年的季
- int num = getQuarter(current);
- switch (num) {
- case 1: // 本年到现在经过了一个季度,在加上前4个季度
- Calendar quarter1 = Calendar.getInstance();
- quarter1.setTime(current);
- quarter1.set(Calendar.MONTH, 3);
- quarter1.set(Calendar.DATE, 1);
- quarter1.add(Calendar.DATE, -1);
- str = new SimpleDateFormat(DATEFORMAT).format(quarter1.getTime());
- s[0] = str.substring(0, str.length() - 5) + "01-01";
- s[1] = str;
- break;
- case 2: // 本年到现在经过了二个季度,在加上前三个季度
- Calendar quarter2 = Calendar.getInstance();
- quarter2.setTime(current);
- quarter2.set(Calendar.MONTH, 6);
- quarter2.set(Calendar.DATE, 1);
- quarter2.add(Calendar.DATE, -1);
- str = new SimpleDateFormat(DATEFORMAT).format(quarter2.getTime());
- s[0] = str.substring(0, str.length() - 5) + "04-01";
- s[1] = str;
- break;
- case 3:// 本年到现在经过了三个季度,在加上前二个季度
- Calendar quarter3 = Calendar.getInstance();
- quarter3.setTime(current);
- quarter3.set(Calendar.MONTH, 9);
- quarter3.set(Calendar.DATE, 1);
- quarter3.add(Calendar.DATE, -1);
- str = new SimpleDateFormat(DATEFORMAT).format(quarter3.getTime());
- s[0] = str.substring(0, str.length() - 5) + "07-01";
- s[1] = str;
- break;
- case 4:// 本年到现在经过了四个季度,在加上前一个季度
- Calendar quarter4 = Calendar.getInstance();
- quarter4.setTime(current);
- str = new SimpleDateFormat(DATEFORMAT).format(quarter4.getTime());
- s[0] = str.substring(0, str.length() - 5) + "10-01";
- s[1] = str.substring(0, str.length() - 5) + "12-31";
- break;
- default:
- break;
- }
- return s;
- }
- /**
- * 根据传入的日期获取当年的第一天和最后一天
- *
- * @param
- */
- public static String[] getCurrYear(Date current) {
- String[] s = new String[2];
- Calendar quarterCalendar = Calendar.getInstance();
- quarterCalendar.setTime(current);
- String str = new SimpleDateFormat(DATEFORMAT).format(quarterCalendar.getTime());
- s[0] = str.substring(0, str.length() - 5) + "01-01";
- s[1] = str.substring(0, str.length() - 5) + "12-31";
- return s;
- }
- /***
- * 获取上n日期yyyy-MM-dd
- */
- public static String getPreviousDateStr(Date date, int n) {
- long time = date.getTime();
- // 用毫秒数来计算新的日期
- time = time - n * 1000 * 24 * 60 * 60;
- return formatDate(new Date(time));
- }
- /**
- * 获取去年这个季度的第一天和最后一天
- */
- public static String[] getPreQuarter(Date current) {
- Calendar quarterCalendar = Calendar.getInstance();
- quarterCalendar.setTime(current);
- //将日期减少一年
- quarterCalendar.add(Calendar.YEAR, -1);
- quarterCalendar.set(Calendar.DATE, 1);
- //返回[当前]季度的第一天和最后一天
- return getCurrQuarter(quarterCalendar.getTime());
- }
- /**
- * 获取上年的第一天和最后一天
- */
- public static String[] getPreYear(Date current) {
- Calendar quarterCalendar = Calendar.getInstance();
- quarterCalendar.setTime(current);
- //将日期减少一年
- quarterCalendar.add(Calendar.YEAR, -1);
- quarterCalendar.set(Calendar.DATE, 1);
- //返回[当前]年份的第一天和最后一天
- return getCurrYear(quarterCalendar.getTime());
- }
- /**
- * 获取去年今日
- */
- public static String getPreYearDay(Date current) {
- Calendar c = Calendar.getInstance();
- c.setTime(current);
- //将日期减少一年
- c.add(Calendar.YEAR, -1);
- return formatDate(c.getTime());
- }
- public static String getPreYearDay1(Date current) {
- Calendar c = Calendar.getInstance();
- c.setTime(current);
- //将日期减少一年
- c.add(Calendar.YEAR, 0);
- return formatDate(c.getTime());
- }
- }
|