package com.nationalelectric.greenH5.utils; import java.sql.Timestamp; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * 日期工具类 2013.01.06 */ public class DateTime implements Comparable { /* Private Fields */ private Date date; private Calendar calendar; private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private SimpleDateFormat df = new SimpleDateFormat(); private static Set dateFormats = new HashSet(); private static Map monthMaps = new HashMap(); private static Map weekdayMaps = new HashMap(); // 注册一下默认识别的日期转换格式 static { DateTime.dateFormats.add("yyyy-MM-dd HH:mm:ss"); DateTime.dateFormats.add("yyyy/MM/dd HH:mm:ss"); DateTime.dateFormats.add("yyyy-MM-dd HH:mm"); DateTime.dateFormats.add("yyyy-MM-dd"); DateTime.dateFormats.add("yyyy/MM/dd"); DateTime.monthMaps.put(1, "一月"); DateTime.monthMaps.put(2, "二月"); DateTime.monthMaps.put(3, "三月"); DateTime.monthMaps.put(4, "四月"); DateTime.monthMaps.put(5, "五月"); DateTime.monthMaps.put(6, "六月"); DateTime.monthMaps.put(7, "七月"); DateTime.monthMaps.put(8, "八月"); DateTime.monthMaps.put(9, "九月"); DateTime.monthMaps.put(10, "十月"); DateTime.monthMaps.put(11, "十一月"); DateTime.monthMaps.put(12, "十二月"); DateTime.weekdayMaps.put(1, "星期一"); DateTime.weekdayMaps.put(2, "星期二"); DateTime.weekdayMaps.put(3, "星期三"); DateTime.weekdayMaps.put(4, "星期四"); DateTime.weekdayMaps.put(5, "星期五"); DateTime.weekdayMaps.put(6, "星期六"); DateTime.weekdayMaps.put(0, "星期日"); } /** * 构造函数 * * @param dt * util.Date */ public DateTime(Date dt) { this.date = dt; this.calendar = Calendar.getInstance(); this.calendar.setTime(dt); } /** * 默认构造函数,当前时间 */ public DateTime() { this(new Date()); } /** * 构造函数 * * @param s * 时间字符串 * @throws ParseException * 转换异常 */ public DateTime(String s) { this.calendar = Calendar.getInstance(); Iterator it = dateFormats.iterator(); while (it.hasNext()) { try { String pattern = (String) it.next(); df.applyPattern(pattern); this.date = df.parse(s); this.calendar.setTime(this.date); break; } catch (ParseException ex) { } } if (date == null || this.calendar == null) { throw new RuntimeException("请指定SimpleDateFormat"); } } /** * 构造函数 * * @param s * 字符串 * @param simpleDateFormat * 格式化语句 * @throws ParseException * 转换异常 */ public DateTime(String s, String simpleDateFormat) { try { this.calendar = Calendar.getInstance(); df.applyPattern(simpleDateFormat); this.date = df.parse(s); this.calendar.setTime(this.date); } catch (ParseException e) { throw new RuntimeException("从字符串转换为时间失败", e); } } /** * 构造函数 * * @param year * 年 * @param month * 月 * @param day * 日 */ public DateTime(int year, int month, int day) { this.calendar = Calendar.getInstance(); calendar.clear(); this.calendar.set(year, month - 1, day); this.date = this.calendar.getTime(); } /** * 构造函数 * * @param year * 年 * @param month * 月 * @param day * 日 * @param hour * 时 * @param min * 分 * @param sec * 秒 */ public DateTime(int year, int month, int day, int hour, int min, int sec) { this.calendar = Calendar.getInstance(); calendar.clear(); this.calendar.set(year, month - 1, day, hour, min, sec); this.date = this.calendar.getTime(); } /** * 获取年份 * * @return 年 */ public int getYear() { return this.calendar.get(Calendar.YEAR); } /** * 获取月份 * * @return 月 */ public int getMonth() { return this.calendar.get(Calendar.MONTH) + 1; } /** * 获取月份中文 * * @return 如:一月 */ public String getMonthText() { return DateTime.monthMaps.get(this.getMonth()); } /** * 获取日 * * @return */ public int getDay() { return this.calendar.get(Calendar.DATE); } /** * 获取星期几 * * @return 0表示星期天 */ public int getDayOfWeek() { return this.calendar.get(Calendar.DAY_OF_WEEK) - 1; } /** * 获取星期几 * * @return 如:星期一 */ public String getDayOfWeekText() { return DateTime.weekdayMaps.get(this.getDayOfWeek()); } /** * 获取月份 * * @return 月份 */ public int getDayOfMonth() { return this.calendar.get(Calendar.DAY_OF_MONTH); } public int getDayOfYear() { return this.calendar.get(Calendar.DAY_OF_YEAR); } /** * 获取小时 * * @return 小时 */ public int getHour() { return this.calendar.get(Calendar.HOUR_OF_DAY); } /** * 获取分钟 * * @return */ public int getMinute() { return this.calendar.get(Calendar.MINUTE); } /** * 获取秒 * * @return */ public int getSecond() { return this.calendar.get(Calendar.SECOND); } /** * 获取季度 * * @return */ public int getSeasonOfYear() { return (this.calendar.get(Calendar.MONTH) / 3) + 1; } /** * 获取旬 * * @return */ public int getTenDayOfMonth() { int x = (this.getDay() - 1) / 10; return x < 3 ? (x + 1) : 3; } /** * 是否闰年 * * @return */ public boolean isLeapYear() { int year = this.getYear(); return isLeapYear(year); } /** * 是否迟于 */ public boolean isAfter(DateTime dt) { return this.date.after(dt.toUtilDate()); } /** * 是否早于 */ public boolean isBefore(DateTime dt) { return this.date.before(dt.toUtilDate()); } /** * 转换为Timestamp */ public Timestamp toTimestamp() { return new Timestamp(this.date.getTime()); } /** * 克隆一个副本 */ public DateTime copy() { return new DateTime(new Date(this.getTime())); } /** * 加几天 */ public DateTime addDays(int i) { Calendar c = Calendar.getInstance(); c.setTime(this.date); c.add(Calendar.DATE, i); return new DateTime(c.getTime()); } /** * 加几年 */ public DateTime addYears(int i) { Calendar c = Calendar.getInstance(); c.setTime(this.date); c.add(Calendar.YEAR, i); return new DateTime(c.getTime()); } /** * 加几月 */ public DateTime addMonths(int i) { Calendar c = Calendar.getInstance(); c.setTime(this.date); c.add(Calendar.MONTH, i); return new DateTime(c.getTime()); } /** * 加小时 */ public DateTime addHours(int i) { Calendar c = Calendar.getInstance(); c.setTime(this.date); c.add(Calendar.HOUR, i); return new DateTime(c.getTime()); } /** * 加分钟 */ public DateTime addMinutes(int i) { Calendar c = Calendar.getInstance(); c.setTime(this.date); c.add(Calendar.MINUTE, i); return new DateTime(c.getTime()); } /** * 加秒 */ public DateTime addSeconds(int i) { Calendar c = Calendar.getInstance(); c.setTime(this.date); c.add(Calendar.SECOND, i); return new DateTime(c.getTime()); } @Override public String toString() { return toString("yyyy-MM-dd HH:mm:ss"); } /** * 转换为字符串 * * @param mask * 如yyyy-MM-dd等 */ public String toString(String mask) { df.applyPattern(mask); return df.format(this.date); } /** * 转为java.util.Date * * @return */ public Date toUtilDate() { return this.date; } /** * 返回凌晨0点. 仅日期,不带时间 */ public DateTime onlyDate() { return new DateTime(this.getYear(), this.getMonth(), this.getDay()); } /** * 毫秒数 * * @return */ public long getTime() { return this.date.getTime(); } /** * 仅日期 * * @return 指定日期的凌晨0点 */ public static DateTime onlyDate(Date dt) { return new DateTime(dt).onlyDate(); } /** * 判断是否闰年 */ public static boolean isLeapYear(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } else { return false; } } /** * 将util.Date转为Timestamp */ public static Timestamp toTimestamp(Date dt) { return new Timestamp(dt.getTime()); } /** * 当前时间 */ public static DateTime now() { return new DateTime(new Date()); } /** * 今天凌晨 */ public static DateTime today() { return DateTime.now().onlyDate(); } /** * 获取某年某季度第一天 * * @param year * 年 * @param season * 季度 * @return 日期 */ public static DateTime getSeasonStart(int year, int season) { Calendar c = Calendar.getInstance(); c.clear(); c.set(year, (season - 1) * 3, 1); Date dt = c.getTime(); return new DateTime(dt); } /** * 获取某年某季度最后一天 * * @param year * 年 * @param season * 季度 * @return 日期 */ public static DateTime getSeasonEnd(int year, int season) { Calendar c = Calendar.getInstance(); c.clear(); c.set(year, season * 3, 1); c.add(Calendar.SECOND, -1); Date dt = c.getTime(); return new DateTime(dt); } /** * 获取指定日期的旬 * * @param date * 日期 * @return 1-上,2-中,3-下 */ public static int getTenDayInMonth(DateTime date) { return (date.getDay() - 1) / 10 + 1; } /** * 获取某年某月某旬的第一天 * * @param year * 年 * @param month * 月 * @param tenday * 旬 * @return 日期 */ public static DateTime getTenDayStart(int year, int month, int tenday) { Calendar c = Calendar.getInstance(); c.clear(); c.set(year, month - 1, (tenday - 1) * 10 + 1); Date dt = c.getTime(); return new DateTime(dt); } /** * 获取某年某月某旬的最后一天 * * @param year * 年 * @param month * 月 * @param tenday * 旬 * @return 日期 */ public static DateTime getTenDayEnd(int year, int month, int tenday) { Calendar c = Calendar.getInstance(); c.clear(); c.set(year, month - 1, 1); if (tenday < 3) { c.set(year, month - 1, tenday * 10, 23, 59, 59); } else { c.add(Calendar.MONTH, 1); c.add(Calendar.SECOND, -1); } Date dt = c.getTime(); return new DateTime(dt); } /** * 获取某年某月的第一天 * * @param year * 年 * @param month * 月 * @return 日期 */ public static DateTime getMonthStart(int year, int month) { Calendar c = Calendar.getInstance(); c.clear(); c.set(year, month - 1, 1); Date dt = c.getTime(); return new DateTime(dt); } /** * 获取某年某月的最后一天 * * @param year * 年 * @param month * 月 * @return 日期 */ public static DateTime getMonthEnd(int year, int month) { Calendar c = Calendar.getInstance(); c.clear(); c.set(year, month - 1, 1); c.add(Calendar.MONTH, 1); c.add(Calendar.SECOND, -1); Date dt = c.getTime(); return new DateTime(dt); } /** * 获取两个日期相差天数 * * @param startdate * 起始 * @param enddate * 结束 * @return 天数 */ public static int getBetweenDays(Date startdate, Date enddate) { return (int) Math.rint((double) (new DateTime(enddate).toUtilDate().getTime() - new DateTime(startdate).toUtilDate().getTime()) / (1000 * 60 * 60 * 24)); } /** * 与某个日期相差天数 * * @param enddate * 结束日期 * @return 天数 */ public int getBetweenDays(DateTime enddate) { return (int) Math.rint((double) (enddate.toUtilDate().getTime() - this.date.getTime()) / (1000 * 60 * 60 * 24)); } /** * 比较 */ public int compareTo(DateTime c) { return this.toUtilDate().compareTo(c.toUtilDate()); } public static void main(String[] args) throws ParseException { Date startDate = new DateTime().strToDate("2019-11-11 20:00", "yyyy-MM-dd HH:mm"); System.out.println( new DateTime(startDate).addMinutes(-20).toString()); // System.out.println(b.addDays(1).addDays(1)); // System.out.println(b.getHour()); // System.out.println(b.getMinute()); // System.out.println(b.getSecond()); // System.out.println(DateTime.getTenDayInMonth(new DateTime(2008, 2, 20))); // System.out.println(new DateTime(2013, 3, 28).getBetweenDays(new DateTime(2014, 1, 1))); } /** * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss * * @param strDate * @return */ public static Date strToDateLong(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static Date strToDateLong4(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static Date strToDateLong2(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static Date strToDateLong3(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static Date strToDateLongByYearMonth(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static Date strToDate(String strDate,String format) { SimpleDateFormat formatter = new SimpleDateFormat(format); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static String dateToStr(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String dateString = formatter.format(dateDate); return dateString; } public static String dateToStr(java.util.Date dateDate,String format) { SimpleDateFormat formatter = new SimpleDateFormat(format); String dateString = formatter.format(dateDate); return dateString; } public static String dateToStrYearMonth(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月"); String dateString = formatter.format(dateDate); return dateString; } public static String dateToStrYear(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy"); String dateString = formatter.format(dateDate); return dateString; } public static String dateToStrMonth(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("MM"); String dateString = formatter.format(dateDate); return dateString; } public static String dateToStr2(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("dd"); String dateString = formatter.format(dateDate); return dateString; } /** * 获取当前时间 YYYY-MM-DD HH:mm:ss * @param dateDate * @return */ public static String dateToYSStr(java.util.Date dateDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(dateDate); return dateString; } /** * 获取上个月年月 * @return */ public static String lastData(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); //得到前一个月 String start = format.format(c.getTime()); return start; } /** * 当前季度的开始时间,即2012-01-1 00:00:00 * * @return */ public static Date getCurrentQuarterStartTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) -2; 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:01"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前季度的结束时间,即2012-03-31 23:59:59 * * @return */ public static Date getCurrentQuarterEndTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) -2; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) { c.set(Calendar.MONTH, 2); c.set(Calendar.DATE, 31); } else if (currentMonth >= 4 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 9) { c.set(Calendar.MONTH, 8); c.set(Calendar.DATE, 30); } else if (currentMonth >= 10 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } }