|
@@ -0,0 +1,452 @@
|
|
|
+package com.xynet.marketing.utils;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.ParsePosition;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Copyright © 2018 xynet Tech Ltd. All rights reserved
|
|
|
+ * @author: sund
|
|
|
+ * @date: 2018年2月9日 下午3:21:58
|
|
|
+ * @remark:日期工具类
|
|
|
+ */
|
|
|
+public class DateUtil {
|
|
|
+ protected static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
|
|
|
+ static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");//yyyyMMddHHmmss
|
|
|
+ static SimpleDateFormat ymdhms = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ /**
|
|
|
+ * 将Date类型转换为字符串
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期类型
|
|
|
+ * @return 日期字符串
|
|
|
+ */
|
|
|
+ public static String format(Date date) {
|
|
|
+ return format(date, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Date类型转换为字符串
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期类型
|
|
|
+ * @param pattern
|
|
|
+ * 字符串格式
|
|
|
+ * @return 日期字符串
|
|
|
+ */
|
|
|
+ public static String format(Date date, String pattern) {
|
|
|
+ if (date == null) {
|
|
|
+ return "null";
|
|
|
+ }
|
|
|
+ if (pattern == null || pattern.equals("") || pattern.equals("null")) {
|
|
|
+ pattern = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat(pattern).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串转换为Date类型
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 字符串类型
|
|
|
+ * @return 日期类型(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public static Date format(String date) {
|
|
|
+ return format(date, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串转换为Date类型
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 字符串类型
|
|
|
+ * @param pattern
|
|
|
+ * 格式
|
|
|
+ * @return 日期类型
|
|
|
+ */
|
|
|
+ public static Date format(String date, String pattern) {
|
|
|
+ if (pattern == null || pattern.equals("") || pattern.equals("null")) {
|
|
|
+ pattern = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ }
|
|
|
+ if (date == null || date.equals("") || date.equals("null")) {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+ Date d = null;
|
|
|
+ try {
|
|
|
+ d = new SimpleDateFormat(pattern).parse(date);
|
|
|
+ } catch (ParseException pe) {
|
|
|
+ }
|
|
|
+ return d;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 两天之间的天数
|
|
|
+ public static int daysBetween(String startDate, String endDate)
|
|
|
+ throws ParseException {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date start = sdf.parse(startDate);
|
|
|
+ Date end = sdf.parse(endDate);
|
|
|
+ return daysBetween(end,start);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 求两个日期之间的天数
|
|
|
+ */
|
|
|
+ public static int daysBetween(Date startDate,Date endDate)
|
|
|
+ {
|
|
|
+ //一天24小时*60分钟*60秒*1000毫秒
|
|
|
+ return (int) ((endDate.getTime() -
|
|
|
+ startDate.getTime()-10) /(24*60*60*1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 求两个时间之间的时间
|
|
|
+ * @param startDate 开始时间
|
|
|
+ * @param endDate 结束时间
|
|
|
+ * @param type 类型(1:秒 2:分 3:时 4:日)
|
|
|
+ */
|
|
|
+ public static int daysBetweenV2(String startDate,String endDate,int type)
|
|
|
+ throws ParseException{
|
|
|
+ int h = 1;
|
|
|
+ int m = 1;
|
|
|
+ int s = 1;
|
|
|
+ if (type == 1) {
|
|
|
+ s = 1;
|
|
|
+ }
|
|
|
+ if (type == 2) {
|
|
|
+ s = 60;
|
|
|
+ }
|
|
|
+ if (type == 3) {
|
|
|
+ m = 60;
|
|
|
+ s = 60;
|
|
|
+ }
|
|
|
+ if (type == 4) {
|
|
|
+ m = 60;
|
|
|
+ s = 60;
|
|
|
+ h = 24;
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date start = sdf.parse(startDate);
|
|
|
+ Date end = sdf.parse(endDate);
|
|
|
+ //一天24小时*60分钟*60秒*1000毫秒
|
|
|
+ return (int) ((end.getTime() -
|
|
|
+ start.getTime()) /(h*m*s*1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 时间格式的转换
|
|
|
+ */
|
|
|
+ public static Date formatUtc(String str)
|
|
|
+ {
|
|
|
+ String strTime = str.replace("Z", " UTC");
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
|
|
|
+ Date dateTime = null;
|
|
|
+ try {
|
|
|
+ dateTime = format.parse(strTime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 求某一日期加N个小时后的时间
|
|
|
+ */
|
|
|
+ public static String dateAddHours(Date startDate,int hours,String pattern)
|
|
|
+ {
|
|
|
+ //一天24小时*60分钟*60秒*1000毫秒
|
|
|
+ return format(new Date(startDate.getTime()+hours*60*60*1000),pattern);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否在同一个月
|
|
|
+ * @param startDate yyyy-MM-dd
|
|
|
+ * @param endDate yyyy-MM-dd
|
|
|
+ * @return false:不在同一个月内,true在同一个月内
|
|
|
+ */
|
|
|
+ public static boolean isMonth(String startDate,String endDate){
|
|
|
+ if(margin(startDate, endDate)>31){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int startMonth = Integer.parseInt(startDate.substring(5, 7));
|
|
|
+ int endMonth = Integer.parseInt(endDate.substring(5, 7));
|
|
|
+ if(startMonth==endMonth){
|
|
|
+ return true;
|
|
|
+ }else{
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 计算开始日期和结束日期差
|
|
|
+ * @param startDate yyyy-MM-dd
|
|
|
+ * @param endDate yyyy-MM-dd
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static int margin(String startDate,String endDate){
|
|
|
+ ParsePosition pos = new ParsePosition(0);
|
|
|
+ ParsePosition pos2 = new ParsePosition(0);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date ds = sdf.parse(startDate, pos);
|
|
|
+ Date de = sdf.parse(endDate, pos2);
|
|
|
+ long l = de.getTime()-ds.getTime();
|
|
|
+ int margin = (int)(l/24*60*60*1000);
|
|
|
+ return margin;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断时间跨度是否相差一个月
|
|
|
+ * @param startTime yyyyMMdd
|
|
|
+ * @param endTime
|
|
|
+ */
|
|
|
+ public static boolean isDifference(String startTime,String endTime){
|
|
|
+ SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ try {
|
|
|
+ Date d1 = f.parse(startTime);
|
|
|
+ Date d2 = f.parse(endTime);
|
|
|
+ //Date d2 = f.parse("20120101");
|
|
|
+ long day = (d2.getTime()-d1.getTime())/1000/60/60/24 + 1;
|
|
|
+ System.out.println("d1和d2相差" + day + "天。");
|
|
|
+ int month = isMaxMonth(startTime);
|
|
|
+ System.out.println(month);
|
|
|
+ if(day > month){
|
|
|
+ return false;
|
|
|
+ }else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断这年的某月有多少天
|
|
|
+ * @param startTime 时间 yyyyMMdd
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int isMaxMonth(String startTime) {
|
|
|
+ int year = 0;
|
|
|
+ int month = 0;
|
|
|
+ int days= 0 ;
|
|
|
+ year = Integer.parseInt(startTime.substring(0, 4));
|
|
|
+ month = Integer.parseInt(startTime.substring(4, 6));
|
|
|
+ switch(month){
|
|
|
+ case 2:
|
|
|
+ boolean flag=(year%4==0&&year%100!=100)||year%400==0;
|
|
|
+ if(flag){
|
|
|
+ days=29;
|
|
|
+ }else{
|
|
|
+ days=28;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ case 6:
|
|
|
+ case 9:
|
|
|
+ case 11:
|
|
|
+ days=30;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ days=31;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return days;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回序列化
|
|
|
+ * @param date
|
|
|
+ * @param index
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int timexlh(Date date,int index) {
|
|
|
+ return (date.getHours()*60+date.getMinutes())/index;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天的yyyyMMdd
|
|
|
+
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int yesterdayyyMMdd(Date date,int index) {
|
|
|
+ Date yesterday = new Date(date.getTime() - 86400000L*index);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String time = format.format(yesterday);
|
|
|
+ return Integer.parseInt(time);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取前天的yyyyMMdd
|
|
|
+
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int beforeYesterdayyyMMdd(Date date) {
|
|
|
+ Date yesterday = new Date(date.getTime() - 172800000l);
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String time = format.format(yesterday);
|
|
|
+ return Integer.parseInt(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取今天的yyyyMMdd
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int todayyyMMdd(Date date) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ String time = format.format(date);
|
|
|
+ return Integer.parseInt(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args){
|
|
|
+ String date = getMaxyesterdayDate();
|
|
|
+ System.out.println(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间字符串转intger
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer stringToInt(String str){
|
|
|
+ try {
|
|
|
+ //2019-08-08
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ String year = str.substring(0, 4);
|
|
|
+ String moth = str.substring(5, 7);
|
|
|
+ String day = str.substring(8, 10);
|
|
|
+ sb.append(year);
|
|
|
+ sb.append(moth);
|
|
|
+ sb.append(day);
|
|
|
+ return Integer.parseInt(sb.toString());
|
|
|
+ }catch (Exception e){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间转字符串yyyyMMddHHmmss
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String dateToString(Date date){
|
|
|
+ String format = ymdhms.format(date);
|
|
|
+ return format;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date stringToDate(String str) throws ParseException {
|
|
|
+ Date parse = ymdhms.parse(str);
|
|
|
+ return parse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天最大 的时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getMaxyesterdayDate(){
|
|
|
+ Date date = new Date();
|
|
|
+ long time = date.getTime() - 1000*60*60;
|
|
|
+ String format = sdf.format(time);
|
|
|
+ System.out.println(format);
|
|
|
+ return format;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回两个日期之间的所有日期
|
|
|
+ * @param startTime
|
|
|
+ * @param endTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getDays(String startTime,String endTime){
|
|
|
+ List<String> days = new ArrayList<String>();
|
|
|
+ try {
|
|
|
+ Date start = ymd.parse(startTime);
|
|
|
+ Date end = ymd.parse(endTime);
|
|
|
+
|
|
|
+ Calendar cldStart = Calendar.getInstance();
|
|
|
+ cldStart.setTime(start);
|
|
|
+
|
|
|
+ Calendar cldEnd = Calendar.getInstance();
|
|
|
+ cldEnd.setTime(end);
|
|
|
+ cldEnd.add(Calendar.DATE, +1);
|
|
|
+
|
|
|
+ while (cldStart.before(cldEnd)) {
|
|
|
+ days.add(ymd.format(cldStart.getTime()));
|
|
|
+ cldStart.add(Calendar.DAY_OF_YEAR, +1);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return days;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**+
|
|
|
+ * 获取从当前时间开始 加减后的时间(hep 用)
|
|
|
+ * @param year int 年
|
|
|
+ * @param month int 月
|
|
|
+ * @param day int 天
|
|
|
+ * @param hour int 时
|
|
|
+ * @param minute int 分
|
|
|
+ * @param type int 返回格式 1:"yyyy-MM-dd HH:mm:ss" 2:"yyyy-MM-dd" 3:"yyyyMMdd" 4:"yyyy-MM-dd HH:mm:00"
|
|
|
+ * 5:"yyyyMMddHHmmss" 6:"yyyy-MM"
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getUserDefinedTime(int year,int month, int day, int hour, int minute, int type, Date settime){
|
|
|
+ String format = "";
|
|
|
+ switch (type) {
|
|
|
+ case 1:
|
|
|
+ format = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ format = "yyyy-MM-dd";
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ format = "yyyyMMdd";
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ format = "yyyy-MM-dd HH:mm:00";
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ format = "yyyyMMddHHmmss";
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ format = "yyyy-MM";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Calendar cal=Calendar.getInstance();
|
|
|
+ if (settime != null) {
|
|
|
+ cal.setTime(settime);
|
|
|
+ }
|
|
|
+ if (year != 0) {
|
|
|
+ cal.add(Calendar.YEAR,+ year);
|
|
|
+ }
|
|
|
+ if (month != 0) {
|
|
|
+ cal.add(Calendar.MONTH, + month);
|
|
|
+ }
|
|
|
+ if (day != 0) {
|
|
|
+ cal.add(Calendar.DATE,+ day);
|
|
|
+ }
|
|
|
+ if (hour != 0) {
|
|
|
+ cal.add(Calendar.HOUR,+ hour);
|
|
|
+ }
|
|
|
+ if (minute != 0) {
|
|
|
+ cal.add(Calendar.MINUTE,+ minute);
|
|
|
+ }
|
|
|
+ Date d=cal.getTime();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ String time = sdf.format(d);
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+}
|