|
|
@@ -0,0 +1,930 @@
|
|
|
+package com.aijia.basic.utils;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
+
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.NumberFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class DateHelper {
|
|
|
+
|
|
|
+
|
|
|
+ private static final long ONE_MINUTE = 60;
|
|
|
+ private static final long ONE_HOUR = 3600;
|
|
|
+ private static final long ONE_DAY = 86400;
|
|
|
+ static String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
|
|
|
+ static Integer[] weekDay = {7, 1, 2, 3, 4, 5, 6};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
|
|
|
+ *
|
|
|
+ * @param sformat yyyyMMddhhmmss
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getUserDate(String sformat) {
|
|
|
+ Date currentTime = new Date();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(sformat);
|
|
|
+ String dateString = formatter.format(currentTime);
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getOrderNo(int k) {
|
|
|
+
|
|
|
+ return getUserDate("yyyyMMddHHmmss") + getRandom(k);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回一个随机数
|
|
|
+ *
|
|
|
+ * @param i
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getRandom(int i) {
|
|
|
+ Random jjj = new Random();
|
|
|
+ if (i == 0){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String jj = "";
|
|
|
+ for (int k = 0; k < i; k++) {
|
|
|
+ jj = jj + jjj.nextInt(9);
|
|
|
+ }
|
|
|
+ return jj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String formatDateByFormat(Date date, String format) {
|
|
|
+ String result = "";
|
|
|
+ if (date != null) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ result = sdf.format(date);
|
|
|
+ result = "/" + result;
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseDate(String dateStr, String format) {
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ DateFormat df = new SimpleDateFormat(format);
|
|
|
+ String dt = dateStr;
|
|
|
+ if ((!"".equals(dt)) && (dt.length() < format.length())) {
|
|
|
+ dt = dt + format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
|
|
|
+ }
|
|
|
+ date = df.parse(dt);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String format(String dateStr) throws Exception {
|
|
|
+
|
|
|
+ Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
+ return format.format(date.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String format(Date date, String format) {
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ if (date != null) {
|
|
|
+ DateFormat df = new SimpleDateFormat(format);
|
|
|
+ result = df.format(date);
|
|
|
+ }
|
|
|
+ } catch (Exception localException) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getDate(Date date) {
|
|
|
+ return format(date, "yyyy/MM/dd");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getDateTime(Date date) {
|
|
|
+ return format(date, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取好友的时间显示,如 5分钟前
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTimeDes(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+
|
|
|
+ long time = date.getTime() / 1000;
|
|
|
+ long now = System.currentTimeMillis() / 1000;
|
|
|
+ long ago = now - time;
|
|
|
+ if ( ONE_HOUR >=ago ) {
|
|
|
+ return ago / ONE_MINUTE + "分钟前";
|
|
|
+ } else if (ONE_DAY >=ago) {
|
|
|
+ return ago / ONE_HOUR + "小时前";
|
|
|
+ } else if ( ONE_DAY * 2 >=ago ) {
|
|
|
+ return "昨天";
|
|
|
+ } else if ( ONE_DAY * 3 >=ago) {
|
|
|
+ return "前天";
|
|
|
+ } else {
|
|
|
+ return ago / ONE_DAY + "天前";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDays(Date date, int i) throws Exception {
|
|
|
+ int days = daysOfTwo(date, new Date());
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
|
|
|
+ String time = "";
|
|
|
+ if (i == 1) {
|
|
|
+ time = "<br/>" + format.format(date.getTime());
|
|
|
+ }
|
|
|
+ if (days == 0) {
|
|
|
+ return "今天" + time;
|
|
|
+ } else if (days == 1) {
|
|
|
+ return "昨天" + time;
|
|
|
+ } else if (days == 2) {
|
|
|
+ return "前天" + time;
|
|
|
+ } else if (days <= 7 && days >= 3) {
|
|
|
+ return numToUpper(days - 1) + "天前" + time;
|
|
|
+ } else if (days < 0) {
|
|
|
+ if (days == -1) {
|
|
|
+ return "明天" + time;
|
|
|
+ } else if (days == -2) {
|
|
|
+ return "后天" + time;
|
|
|
+ }
|
|
|
+ return Math.abs(days) + "天" + time;
|
|
|
+ } else {
|
|
|
+ if (days / 7 > 3) {
|
|
|
+ return numToUpper(days / 30) + "月前" + time;
|
|
|
+ }
|
|
|
+ return numToUpper(days / 7) + "周前" + time;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String numToUpper(int num) {
|
|
|
+ String[] u = {"上", "两", "三", "四", "五", "六", "七", "八", "九"};
|
|
|
+ char[] str = String.valueOf(num).toCharArray();
|
|
|
+ String rstr = "";
|
|
|
+ for (int i = 0; i < str.length; i++) {
|
|
|
+ rstr = rstr + u[Integer.parseInt(str[i] + "")];
|
|
|
+ }
|
|
|
+ return rstr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将时间转换为标准的天+小时+分+秒格式
|
|
|
+ */
|
|
|
+ public static String getStandardTime(long diff){
|
|
|
+ String DateTimes = null;
|
|
|
+ long hours = (diff % ( 60 * 60 * 24)) / (60 * 60);
|
|
|
+ long minutes = (diff % ( 60 * 60)) /60;
|
|
|
+ long seconds = diff % 60;
|
|
|
+ if(hours>0){
|
|
|
+ DateTimes=hours + ":" + minutes + ":" + seconds;
|
|
|
+ }else if(minutes>0){
|
|
|
+ DateTimes="00"+":"+minutes + ":" + seconds;
|
|
|
+ }else{
|
|
|
+ DateTimes="00"+":"+"00"+":"+seconds;
|
|
|
+ } return DateTimes;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getDifferenceTime(Date one, Date two) {
|
|
|
+ String DateTimes = null;
|
|
|
+ long day = 0;
|
|
|
+ long hour = 0;
|
|
|
+ long min = 0;
|
|
|
+ long sec = 0;
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = (time2 - time1);
|
|
|
+ } else {
|
|
|
+ diff = (time1 - time2);
|
|
|
+ }
|
|
|
+ long days = diff / (1000 * 60 * 60 * 24);
|
|
|
+ long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
|
|
|
+ long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
|
|
|
+ long seconds=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
|
|
|
+ long addHour=hours+days*24;
|
|
|
+ if(addHour>0){
|
|
|
+ DateTimes=addHour + ":" + minutes + ":" + seconds;
|
|
|
+ }else if(minutes>0){
|
|
|
+ DateTimes="00"+":"+minutes + ":" + seconds;
|
|
|
+ }else{
|
|
|
+ DateTimes="00"+":"+"00"+":"+seconds;
|
|
|
+ } return DateTimes;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCostTime(Date one, Date two) {
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ long min = diff / (60 * 1000);
|
|
|
+ return Long.toString(min);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getNextDay(Date date, int days) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, days);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上月的第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param
|
|
|
+ * @return 创建人:ZhangCaibao
|
|
|
+ * 2018年2月1日 上午9:55:52
|
|
|
+ * @Date
|
|
|
+ */
|
|
|
+ public static Date getLastMonthFirstDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.MONTH, -1);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取下月的第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param
|
|
|
+ * @return 创建人:ZhangCaibao
|
|
|
+ * 2018年2月1日 上午9:55:52
|
|
|
+ * @Date
|
|
|
+ */
|
|
|
+ public static Date getNextMonthFirstDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.MONTH, 1);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getTheMonthFirstDay() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.add(Calendar.MONTH, 0);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上月的最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param
|
|
|
+ * @return 创建人:ZhangCaibao
|
|
|
+ * 2018年2月1日 上午9:55:52
|
|
|
+ * @Date
|
|
|
+ */
|
|
|
+ public static Date getLastMonthLastDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 0);
|
|
|
+ date = calendar.getTime();
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getMonth(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int month = calendar.get(Calendar.MONTH) + 1;
|
|
|
+ return month;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param
|
|
|
+ * @return 根据开学时间,和当前时间,相差几周。
|
|
|
+ * @throws Exception
|
|
|
+ * @Integer
|
|
|
+ */
|
|
|
+ public static Integer diffWeeks(String date) throws Exception {
|
|
|
+ Date startDate = parseDate(date, "yyyy-MM-dd");
|
|
|
+ Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
|
|
|
+ Long weekNum = ((endDate.getTime() / 1000 - startDate.getTime() / 1000) % 604800) > 0 ? (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800 + 1 : (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800;
|
|
|
+ return weekNum.intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer diffWeeks(String date, Date start) throws Exception {
|
|
|
+ Date startDate = parseDate(date, "yyyy-MM-dd");
|
|
|
+ Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(start));
|
|
|
+ Long weekNum = ((endDate.getTime() / 1000 - startDate.getTime() / 1000) % 604800) > 0 ? (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800 + 1 : (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800;
|
|
|
+ return weekNum.intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断当前时间是不是大于活动结束时间
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static boolean isInDate(Date date, String strDateBegin,
|
|
|
+ String strDateEnd) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date beginDate = sdf.parse(strDateBegin);
|
|
|
+ Date endDate = sdf.parse(strDateEnd);
|
|
|
+ if (date.getTime() >= beginDate.getTime() && date.getTime() <= endDate.getTime()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isInDate(Date date, Date beginDate,
|
|
|
+ Date endDate) {
|
|
|
+ try {
|
|
|
+ if (date.getTime() >= beginDate.getTime() && date.getTime() <= endDate.getTime()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据日期字符串判断周几
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static int getday(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ //第几天,从周日开始
|
|
|
+ int day = calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getWeekDays(Date date) {
|
|
|
+ int day = getday(date);
|
|
|
+ return weekDays[day - 1];
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer getWeekDay(Date date) {
|
|
|
+ int day = getday(date);
|
|
|
+ return weekDay[day - 1];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param date 当前时间
|
|
|
+ * @param week_of_year 第几周 下周(1),下下周(2)
|
|
|
+ * @param day_of_week 周几,周日(1),周一(2),周六(7)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getNextTuesday(Date date, int week_of_year, int day_of_week) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.WEEK_OF_YEAR, week_of_year);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, day_of_week);
|
|
|
+
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getPercentage(int num1, int num2) {
|
|
|
+ // 创建一个数值格式化对象
|
|
|
+
|
|
|
+ NumberFormat numberFormat = NumberFormat.getInstance();
|
|
|
+
|
|
|
+ // 设置精确到小数点后2位
|
|
|
+
|
|
|
+ numberFormat.setMaximumFractionDigits(2);
|
|
|
+
|
|
|
+ String result = numberFormat.format((float) num1 / (float) num2 * 100);
|
|
|
+ return result + "%";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较两个日期相差的天数
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static int daysOfTwo(Date smdate, Date bdate) throws Exception {
|
|
|
+
|
|
|
+ smdate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(smdate));
|
|
|
+ bdate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(bdate));
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(smdate);
|
|
|
+ long time1 = cal.getTimeInMillis();
|
|
|
+ cal.setTime(bdate);
|
|
|
+ long time2 = cal.getTimeInMillis();
|
|
|
+ long between_days = (time2 - time1) / (1000 * 3600 * 24);
|
|
|
+
|
|
|
+ return daysOfTian(smdate,bdate);
|
|
|
+
|
|
|
+ }
|
|
|
+ public static LocalDate date2LocalDate(Date date) {
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ return localDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int daysOfTian(Date date1, Date date2) {
|
|
|
+
|
|
|
+ if (date1 == null || date2 == null) {
|
|
|
+ throw new RuntimeException("日期不能为空");
|
|
|
+ }
|
|
|
+ LocalDate localDate1 = date2LocalDate(date1);
|
|
|
+ LocalDate localDate2 = date2LocalDate(date2);
|
|
|
+
|
|
|
+ return (int) (localDate2.toEpochDay() - localDate1.toEpochDay());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取相差的天数
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @throws Exception 创建人:ZhangCaibao
|
|
|
+ * 2017年4月19日 下午4:27:42
|
|
|
+ * @int
|
|
|
+ */
|
|
|
+ public static int daysDiffer(String startdate, String enddate) throws Exception {
|
|
|
+
|
|
|
+ Date smdate = new SimpleDateFormat("yyyy-MM-dd").parse(startdate);
|
|
|
+ Date bdate = new SimpleDateFormat("yyyy-MM-dd").parse(enddate);
|
|
|
+ return daysOfTian(smdate,bdate);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatString(String dateStr, int days) throws Exception {
|
|
|
+ Date smdate = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
|
|
|
+ if (days > 90) {
|
|
|
+ return format(smdate, "MM月dd日");
|
|
|
+ } else {
|
|
|
+ return format(smdate, "YYYY年MM月dd日");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据时间来拼接
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param hhmmss
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date timeStitching(Date date, String hhmmss) throws Exception {
|
|
|
+ String yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd").format(date);
|
|
|
+ Date datefor = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(yyyyMMdd + " " + hhmmss);
|
|
|
+ return datefor;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //判断选择的日期是否是本月
|
|
|
+ public static boolean isThisMonth(Date date) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
|
|
+ String param = sdf.format(date);//参数时间
|
|
|
+ String now = sdf.format(new Date());//当前时间
|
|
|
+ if (param.equals(now)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getMonthDate(Date smdate, int month) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(smdate);
|
|
|
+ cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getYearDate(Date smdate, int year) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(smdate);
|
|
|
+ cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + year);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static Map<String, Object> getDifferenceTimeHourMap(Date one, Date two) {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ long day = 0;
|
|
|
+ long hour = 0;
|
|
|
+ long min = 0;
|
|
|
+ long sec = 0;
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ day = diff / (24 * 60 * 60 * 1000);
|
|
|
+ hour = (diff / (60 * 60 * 1000) - day * 24);
|
|
|
+ min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
|
|
|
+ sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
|
|
|
+ map.put("day", day < 10 ? "0" + day : day);
|
|
|
+ map.put("hour", hour < 10 ? "0" + hour : hour);
|
|
|
+ map.put("min", min < 10 ? "0" + min : min);
|
|
|
+ map.put("sec", sec < 10 ? "0" + sec : sec);
|
|
|
+ map.put("millisecond", diff);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ //两时间差
|
|
|
+ public static long getDifferenceTimeNum(Date one, Date two) {
|
|
|
+ long time1 = one.getTime();
|
|
|
+ long time2 = two.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ return diff;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static boolean isSameDate(Date date1, Date date2) {
|
|
|
+ Calendar cal1 = Calendar.getInstance();
|
|
|
+ cal1.setTime(date1);
|
|
|
+ Calendar cal2 = Calendar.getInstance();
|
|
|
+ cal2.setTime(date2);
|
|
|
+ boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
|
|
|
+ .get(Calendar.YEAR);
|
|
|
+ boolean isSameMonth = isSameYear
|
|
|
+ && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
|
|
|
+ boolean isSameDate = isSameMonth
|
|
|
+ && cal1.get(Calendar.DAY_OF_MONTH) == cal2
|
|
|
+ .get(Calendar.DAY_OF_MONTH);
|
|
|
+ return isSameDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param sformat 时间格式
|
|
|
+ * @param beforeDay 如果为负数即当前时间前beforeDay天,如果为正数则为当前时间后beforeDay天
|
|
|
+ * @return 方法说明:
|
|
|
+ * <p>
|
|
|
+ * 创立日期:2018年5月16日 下午3:51:09
|
|
|
+ * 创建人:yangcan
|
|
|
+ */
|
|
|
+ public static String getBeforeDate(String sformat, int beforeDay) {
|
|
|
+ Calendar calendar = Calendar.getInstance(); //得到日历
|
|
|
+ calendar.setTime(new Date());//把当前时间赋给日历
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, beforeDay); //设置为当前时间前beforeDay天
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(sformat);
|
|
|
+ String dateString = formatter.format(calendar.getTime());
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param date 传入时间
|
|
|
+ * @param hour 传入时间加小时
|
|
|
+ * @param minute 传入时间加分钟
|
|
|
+ * @return 方法说明:返回传入时间加小时,分钟后的时间
|
|
|
+ * <p>
|
|
|
+ * 创立日期:2018年6月10日 下午3:19:32
|
|
|
+ * 创建人:yangcan
|
|
|
+ */
|
|
|
+ public static Date getAddHour(Date date, int hour, int minute) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.HOUR, hour);
|
|
|
+ cal.add(Calendar.MINUTE, minute);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Map<String, Object>> nearWeek() {
|
|
|
+ List<Map<String, Object>> mlist = new ArrayList<Map<String, Object>>();
|
|
|
+ String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
|
|
|
+ for (int i = -6; i <= 0; i++) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(5, i);
|
|
|
+ int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (w < 0){
|
|
|
+ w = 0;
|
|
|
+ }
|
|
|
+ if (i == 0) {
|
|
|
+ map.put("dateTime", "今");
|
|
|
+ } else {
|
|
|
+ map.put("dateTime", new SimpleDateFormat("dd").format(cal.getTime()));
|
|
|
+ }
|
|
|
+ map.put("time", new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
|
|
|
+ map.put("dateWeek", weekDays[w]);
|
|
|
+ mlist.add(map);
|
|
|
+ }
|
|
|
+ return mlist;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int diffDate(java.util.Date endDate, java.util.Date startDate) {
|
|
|
+ return (int) ((getMillis(endDate) - getMillis(startDate)) / 86400000L);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long getMillis(java.util.Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ return c.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个日期之间相差的天数
|
|
|
+ *
|
|
|
+ * @param smdate 较小的时间
|
|
|
+ * @param bdate 较大的时间
|
|
|
+ * @return 相差天数
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static int daysBetween(Date smdate, Date bdate) throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ smdate = sdf.parse(sdf.format(smdate));
|
|
|
+ bdate = sdf.parse(sdf.format(bdate));
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(smdate);
|
|
|
+ long time1 = cal.getTimeInMillis();
|
|
|
+ cal.setTime(bdate);
|
|
|
+ long time2 = cal.getTimeInMillis();
|
|
|
+ long between_days = (time2 - time1) / (1000 * 3600 * 24);
|
|
|
+
|
|
|
+ return Integer.parseInt(String.valueOf(between_days));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两段日期重叠的天数
|
|
|
+ *
|
|
|
+ * 这里共有2个时间段(b1-----e1)【b2-----e2】,4个时间点;
|
|
|
+ * 相当于两条线段(b代表起点,e代表端点,b<=e),4个端点。
|
|
|
+ * 可分3种情况:
|
|
|
+ * 1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此时,重合天数为零。
|
|
|
+ * 2.相交。
|
|
|
+ * 情况一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
|
|
|
+ * 情况二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
|
|
|
+ * 3.包含:计算较短的时间段日期长度。
|
|
|
+ * (b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
|
|
|
+ * 【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
|
|
|
+ *
|
|
|
+ * @param begindate1 开始日期
|
|
|
+ * @param enddate1 结束日期
|
|
|
+ * @param begindate2 开始日期
|
|
|
+ * @param enddate2 结束日期
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer getDayCoincidence(Date begindate1, Date enddate1, Date begindate2, Date enddate2) throws ParseException {
|
|
|
+ //转换为天判断
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ begindate1 = sdf.parse(sdf.format(begindate1));
|
|
|
+ enddate1 = sdf.parse(sdf.format(enddate1));
|
|
|
+ begindate2 = sdf.parse(sdf.format(begindate2));
|
|
|
+ enddate2 = sdf.parse(sdf.format(enddate2));
|
|
|
+ long b1 = begindate1.getTime();
|
|
|
+ long e1 = enddate1.getTime();
|
|
|
+ long b2 = begindate2.getTime();
|
|
|
+ long e2 = enddate2.getTime();
|
|
|
+ assert (b1 < e1 && b2 < e2);
|
|
|
+ Integer coincidenceDay;
|
|
|
+ if (b1 <= b2 && e1 >= e2) {
|
|
|
+ System.out.println("1包含2:(b1---【b2-----e2】--e1)");
|
|
|
+ coincidenceDay = daysBetween(begindate2, enddate2) + 1;
|
|
|
+ } else if (b1 >= b2 && e1 <= e2) {
|
|
|
+ System.out.println("2包含1:【b2---(b1-----e1)--e2】");
|
|
|
+ coincidenceDay = daysBetween(begindate1, enddate1) + 1;
|
|
|
+ } else if (b1 >= b2 && b1 <= e2 && e2 <= e1) {
|
|
|
+ System.out.println("相交:【b2---(b1---e2】----e1)");
|
|
|
+ coincidenceDay = daysBetween(begindate1, enddate2) + 1;
|
|
|
+ } else if (b1 <= b2 && e1 <= e2 && e1 >= b2) {
|
|
|
+ System.out.println("相交:(b1---【b2---e1)----e2】");
|
|
|
+ coincidenceDay = daysBetween(begindate2, enddate1) + 1;
|
|
|
+ } else if (e1 < b2 || b1 > e2) {
|
|
|
+ coincidenceDay = 0;
|
|
|
+ } else {
|
|
|
+ System.out.println("意料外的日期组合,无法计算重合天数!");
|
|
|
+ coincidenceDay = null;
|
|
|
+ }
|
|
|
+ //取绝对值
|
|
|
+ return coincidenceDay != null ? Math.abs(coincidenceDay) : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较两个日期大小(只精确到日)
|
|
|
+ *
|
|
|
+ * @param smallDate 小日期
|
|
|
+ * @param bigDate 大日期
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean compareTwoDate(Date smallDate, Date bigDate) {
|
|
|
+ String smallString = DateFormatUtils.format(smallDate, "yyyy-MM-dd");
|
|
|
+ String bigString = DateFormatUtils.format(bigDate, "yyyy-MM-dd");
|
|
|
+ if (smallString.compareTo(bigString) <= 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期是星期几
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param style
|
|
|
+ * @return 当前日期是星期几
|
|
|
+ */
|
|
|
+ public static String getWeekOfDate(Date date, int style) {
|
|
|
+ String[] weekZhouDays = {"周天", "周一", "周二", "周三", "周四", "周五", "周六"};
|
|
|
+ String[] weekXingqiDays = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (w < 0) {
|
|
|
+ w = 0;
|
|
|
+ }
|
|
|
+ if (1 == style) {
|
|
|
+ return weekZhouDays[w];
|
|
|
+ } else {
|
|
|
+ return weekXingqiDays[w];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给定月份,得到当月最后一天的零点
|
|
|
+ */
|
|
|
+ public static String getLastTime(String date) throws Exception{
|
|
|
+ Calendar cal=Calendar.getInstance();
|
|
|
+ cal.setTime(new SimpleDateFormat("yyyy-MM").parse(date));
|
|
|
+ int lastDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH,lastDay);
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String time=sdf.format(cal.getTime())+" 24:00:00";
|
|
|
+ return time;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给定月份,得到当月第一天一天的零点
|
|
|
+ */
|
|
|
+ public static String getBeginTime(String date) throws Exception{
|
|
|
+ Calendar cal=Calendar.getInstance();
|
|
|
+ cal.setTime(new SimpleDateFormat("yyyy-MM").parse(date));
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH,1);
|
|
|
+ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String time=sdf.format(cal.getTime())+" 00:00:00";
|
|
|
+ return time;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //得到日历列表,包含周几
|
|
|
+ public static List<String[]> nextCalendarList() {
|
|
|
+ List<String[]> calendarList=new ArrayList<String[]>();
|
|
|
+ Date d = nextMonthFirstDate();
|
|
|
+ Date date = getMonthStart(d);
|
|
|
+ Date monthEnd = getMonthEnd(d);
|
|
|
+ while (!date.after(monthEnd)) {
|
|
|
+ String[] dataStrs={DateHelper.format(date, "yyyy-MM-dd"),dateToWeek(date)};
|
|
|
+ calendarList.add(dataStrs);
|
|
|
+ date = getNext(date);
|
|
|
+ }
|
|
|
+ return calendarList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //取得上个月的1号
|
|
|
+ public static Date theMonthFirstDate(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.add(Calendar.MONTH, -1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //取得下个月的1号
|
|
|
+ public static Date nextMonthFirstDate() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.add(Calendar.MONTH, 1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ //当月的开始日期
|
|
|
+ public static Date getMonthStart(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int index = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ calendar.add(Calendar.DATE, (1 - index));
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+ //当月的结束日期
|
|
|
+ private static Date getMonthEnd(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.MONTH, 1);
|
|
|
+ int index = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ calendar.add(Calendar.DATE, (-index));
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+ //下一个日期
|
|
|
+ private static Date getNext(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ //周几
|
|
|
+ public static String dateToWeek(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ return String.valueOf(weekDay[w]) ;
|
|
|
+ }
|
|
|
+
|
|
|
+ //时间戳转换为标准日期
|
|
|
+
|
|
|
+ public static String getStandTime(Date date){
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ return format.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static boolean dateQualsByMonth(Date dateOne, Date dateTwo) {
|
|
|
+ Calendar calendarOne = Calendar.getInstance();
|
|
|
+ calendarOne.setTime(dateOne);
|
|
|
+ Calendar calendarTwo = Calendar.getInstance();
|
|
|
+ calendarTwo.setTime(dateTwo);
|
|
|
+ return calendarOne.get(Calendar.YEAR) == calendarTwo.get(Calendar.YEAR) && calendarOne.get(Calendar.MONTH) == calendarTwo.get(Calendar.MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isLastDayOfMonth(Date curMonthDate,Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+
|
|
|
+ Calendar maxCalendar = Calendar.getInstance();
|
|
|
+ maxCalendar.setTime(curMonthDate);
|
|
|
+ return calendar.get(Calendar.DAY_OF_MONTH) == maxCalendar
|
|
|
+ .getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getNextYear(int num) {
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ LocalDate nextYear = today.plus(num, ChronoUnit.YEARS);
|
|
|
+ return nextYear.getYear() + "";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|