|
@@ -0,0 +1,651 @@
|
|
|
+package com.rongwei.bscommon.sys.utils;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author lokey
|
|
|
+ * @date 2017/8/29
|
|
|
+ */
|
|
|
+public class TimeUtils {
|
|
|
+
|
|
|
+
|
|
|
+ private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
|
|
|
+
|
|
|
+ private static SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
+
|
|
|
+ private static SimpleDateFormat MonthFormat = new SimpleDateFormat("yyyy年MM月");
|
|
|
+
|
|
|
+ private static SimpleDateFormat DayFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
+
|
|
|
+ private static SimpleDateFormat Month2Date = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ private static SimpleDateFormat YearFormat = new SimpleDateFormat("yyyy");
|
|
|
+
|
|
|
+ private static SimpleDateFormat MonthAndDay = new SimpleDateFormat("MM月dd日");
|
|
|
+
|
|
|
+ // public static void main(String[] args) throws ParseException {
|
|
|
+ // Calendar calendar = Calendar.getInstance();
|
|
|
+ // calendar.setTime(new Date());
|
|
|
+ // calendar.set(Calendar.DAY_OF_MONTH,15);
|
|
|
+ // calendar.set(Calendar.HOUR_OF_DAY,7);
|
|
|
+ // Date min = calendar.getTime();
|
|
|
+ // calendar.set(Calendar.HOUR_OF_DAY,21);
|
|
|
+ // Date max = calendar.getTime();
|
|
|
+ //
|
|
|
+ // System.out.println(stampToDate(min));
|
|
|
+ // System.out.println(stampToDate(max));
|
|
|
+ // compareByDate(min,max);
|
|
|
+ // }
|
|
|
+
|
|
|
+ public static String stampToDate(Date date) {
|
|
|
+ String res;
|
|
|
+ res = simpleDateFormat.format(date);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatTime(Date date) {
|
|
|
+ String res;
|
|
|
+ res = timeFormat.format(date);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dayTime(Date date) {
|
|
|
+ String res;
|
|
|
+ res = DayFormat.format(date);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dayMonthTime(Date date) {
|
|
|
+ String res;
|
|
|
+ res = MonthAndDay.format(date);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String month2String(Date date) {
|
|
|
+ return MonthFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String month2DateStr(Date date) {
|
|
|
+ return Month2Date.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String yearDateStr(Date date) {
|
|
|
+ return YearFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getMouth() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ return cal.getTime().getTime() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getMouth(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ return cal.getTime().getTime() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDateString(Date date) {
|
|
|
+ String res;
|
|
|
+ res = DateFormat.format(date);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * (整型)java10位时间戳转换为时间Date
|
|
|
+ * @param strTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date TimestampToDate(Integer strTime){
|
|
|
+ long temp = (long)strTime*1000;
|
|
|
+ Timestamp ts = new Timestamp(temp);
|
|
|
+ Date date = new Date();
|
|
|
+ try {
|
|
|
+ date = ts;
|
|
|
+ System.out.println(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * (字符串类型)java10位时间戳转换为时间Date
|
|
|
+ */
|
|
|
+ public static Date StringTimestampToDate(String strTime){
|
|
|
+ long temp = Integer.parseInt(strTime)*1000;
|
|
|
+ Timestamp ts = new Timestamp(temp);
|
|
|
+ Date date = new Date();
|
|
|
+ try {
|
|
|
+ date = ts;
|
|
|
+ System.out.println(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Date类型转换为10位时间戳
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer DateToTimestamp(Date time){
|
|
|
+ Timestamp ts = new Timestamp(time.getTime());
|
|
|
+
|
|
|
+ return (int) ((ts.getTime())/1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * string类型转换为long类型 strTime的时间格式和formatType的时间格式必须相同
|
|
|
+ *
|
|
|
+ * @param strTime
|
|
|
+ * strTime要转换的String类型的时间
|
|
|
+ * @param formatType
|
|
|
+ * 时间格式
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static long stringToLong(String strTime, String formatType) throws ParseException {
|
|
|
+ // String类型转成date类型
|
|
|
+ Date date = stringToDate(strTime, formatType);
|
|
|
+ if (date == null) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ // date类型转成long类型
|
|
|
+ long currentTime = dateToLong(date);
|
|
|
+ return currentTime;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * string类型转换为date类型 strTime的时间格式必须要与formatType的时间格式相同
|
|
|
+ *
|
|
|
+ * @param strTime
|
|
|
+ * strTime要转换的string类型的时间
|
|
|
+ * @param formatType
|
|
|
+ * formatType要转换的格式,eg:yyyy-MM-dd
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date stringToDate(String strTime, String formatType) throws ParseException {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat(formatType);
|
|
|
+ Date date;
|
|
|
+ date = formatter.parse(strTime);
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Date stringToDate(String strTime) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = new Date();
|
|
|
+ try {
|
|
|
+ date = formatter.parse(strTime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date stringNewToDate(String strTime) {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date date = new Date();
|
|
|
+ try {
|
|
|
+ date = formatter.parse(strTime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * date类型转换为long类型
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 要转换的date类型的时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long dateToLong(Date date) {
|
|
|
+ return date.getTime() / 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * long类型转换为String类型
|
|
|
+ *
|
|
|
+ * @param currentTime
|
|
|
+ * 要转换的long类型的时间
|
|
|
+ * @param formatType
|
|
|
+ * 要转换的string类型的时间格式
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static String longToString(long currentTime, String formatType) throws ParseException {
|
|
|
+ Date date = longToDate(currentTime, formatType); // long类型转成Date类型
|
|
|
+ String strTime = dateToString(date, formatType); // date类型转成String
|
|
|
+ return strTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * long转换为Date类型
|
|
|
+ *
|
|
|
+ * @param currentTime
|
|
|
+ * 要转换的long类型的时间
|
|
|
+ * @param formatType
|
|
|
+ * 要转换的string类型的时间格式
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date longToDate(long currentTime, String formatType) throws ParseException {
|
|
|
+ Date dateOld = new Date(currentTime * 1000); // 根据long类型的毫秒数声明一个date类型的时间
|
|
|
+ String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
|
|
|
+ Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * date类型转换为String类型
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * Date类型的时间
|
|
|
+ * @param formatType
|
|
|
+ * 要转换的string类型的时间格式
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static String dateToString(Date date, String formatType) {
|
|
|
+ return new SimpleDateFormat(formatType).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到指定月的天数
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * @param month
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转化日期,获取日期带月,eg:2017-09-01
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static StringBuilder getDate(String date, Integer i) {
|
|
|
+
|
|
|
+ return i < 10 ? new StringBuilder(date).append("-0").append(i) : new StringBuilder(date).append("-").append(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSpecifiedDayAfter(String specifiedDay) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = Month2Date.parse(specifiedDay);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ c.setTime(date);
|
|
|
+ int day = c.get(Calendar.DATE);
|
|
|
+ c.set(Calendar.DATE, day + 1);
|
|
|
+
|
|
|
+ return Month2Date.format(c.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date OneYearLater(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.set(Calendar.YEAR, c.get(Calendar.YEAR) + 1);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date DaysLater(Date date, int days) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Boolean compareByDate(Date minDate, Date maxDate) {
|
|
|
+ if (minDate == null || maxDate == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Long min = setDateZero(minDate);
|
|
|
+ Long max = setDateZero(maxDate);
|
|
|
+ System.out.println(min);
|
|
|
+ System.out.println(max);
|
|
|
+ if (min <= max) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Long setDateZero(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ return calendar.getTime().getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两时间差的毫秒数
|
|
|
+ *
|
|
|
+ * @param startTime
|
|
|
+ * @param endTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Long getTimeCheck(Date startTime, Date endTime) {
|
|
|
+ return endTime.getTime() - startTime.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间相加
|
|
|
+ *
|
|
|
+ * @param startTime
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date timeAdd(Date startTime, Long time) {
|
|
|
+ return stringToDate(simpleDateFormat.format(startTime.getTime() + time));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间相减
|
|
|
+ *
|
|
|
+ * @param startTime
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date timeReduce(Date startTime, Long time) {
|
|
|
+ return stringToDate(simpleDateFormat.format(startTime.getTime() - time));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当月第一天时间戳
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long getFirstDayInMonth() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ return calendar.getTimeInMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为日期SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2Date(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为时间SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2Time(Long time) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");//要转换的时间格式
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为天SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2Day(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为月份SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2DateTime(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为时间去除秒SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2TimeDeleteSeconds(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为月份SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2MonthAndDay(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为年份SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2Year(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为年份SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStamp2MonthAndDayTime(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("M月d日HH:mm");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为日期SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeStampMonthAndDay(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过时间秒毫秒数判断两个时间的间隔
|
|
|
+ * @param date1
|
|
|
+ * @param date2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int differentDaysByMillisecond(Date date1,Date date2) {
|
|
|
+ int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
|
|
|
+ return days;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date parseNormalDate(String dateStr) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ try {
|
|
|
+ return format.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转换为天SUSV
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String timeNewStamp2Day(Long time) {
|
|
|
+ //要转换的时间格式
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
|
|
|
+ Date date;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(sdf.format(time));
|
|
|
+ return sdf.format(date);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个日期相差的月数
|
|
|
+ */
|
|
|
+ public static int getMonthDiff(Date d1, Date d2) {
|
|
|
+ Calendar c1 = Calendar.getInstance();
|
|
|
+
|
|
|
+ Calendar c2 = Calendar.getInstance();
|
|
|
+
|
|
|
+ c1.setTime(d1);
|
|
|
+
|
|
|
+ c2.setTime(d2);
|
|
|
+
|
|
|
+ int year1 = c1.get(Calendar.YEAR);
|
|
|
+
|
|
|
+ int year2 = c2.get(Calendar.YEAR);
|
|
|
+
|
|
|
+ int month1 = c1.get(Calendar.MONTH);
|
|
|
+
|
|
|
+ int month2 = c2.get(Calendar.MONTH);
|
|
|
+
|
|
|
+ int day1 = c1.get(Calendar.DAY_OF_MONTH);
|
|
|
+
|
|
|
+ int day2 = c2.get(Calendar.DAY_OF_MONTH);
|
|
|
+
|
|
|
+ // 获取年的差值
|
|
|
+
|
|
|
+ int yearInterval = year1 - year2;
|
|
|
+
|
|
|
+ // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
|
|
|
+
|
|
|
+ if (month1 < month2 || month1 == month2 && day1 < day2) {
|
|
|
+ yearInterval--;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取月数差值
|
|
|
+
|
|
|
+ int monthInterval = (month1 + 12) - month2;
|
|
|
+
|
|
|
+ if (day1 < day2) {
|
|
|
+ monthInterval--;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ monthInterval %= 12;
|
|
|
+
|
|
|
+ int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
|
|
|
+
|
|
|
+ return monthsDiff;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|