|
@@ -0,0 +1,1792 @@
|
|
|
+package com.example.demo.util;
|
|
|
+
|
|
|
+import com.kcim.common.enums.DateStyleEnum;
|
|
|
+import com.kcim.common.enums.WeekEnum;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class DateUtils {
|
|
|
+ public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ public static final SimpleDateFormat monthSdf = new SimpleDateFormat("yyyy-MM");
|
|
|
+ public static final SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy");
|
|
|
+ public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用的一个DateFormat
|
|
|
+ */
|
|
|
+ public final static SimpleDateFormat commonDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理时分秒的DateFormat
|
|
|
+ */
|
|
|
+ public final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 不显示秒的DateFormat
|
|
|
+ */
|
|
|
+ public final static SimpleDateFormat noSecondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取SimpleDateFormat
|
|
|
+ *
|
|
|
+ * @param parttern 日期格式
|
|
|
+ * @return SimpleDateFormat对象
|
|
|
+ * @throws RuntimeException 异常:非法日期格式
|
|
|
+ */
|
|
|
+ private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
|
|
|
+ return new SimpleDateFormat(parttern);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期中的某数值。如获取月份
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param dateType 日期格式
|
|
|
+ * @return 数值
|
|
|
+ */
|
|
|
+ public static int getInteger(Date date, int dateType) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ return calendar.get(dateType);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前时间转换.
|
|
|
+ *
|
|
|
+ * @return 时间字符串
|
|
|
+ */
|
|
|
+ public static String getDateString() {
|
|
|
+ Date date = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|
|
+ String strDate = sdf.format(date);
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间的字符串
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param simpleDateFormat
|
|
|
+ * @return 时间字符串
|
|
|
+ */
|
|
|
+ public static String getDateString(Date date, SimpleDateFormat simpleDateFormat) {
|
|
|
+ String strDate = simpleDateFormat.format(date);
|
|
|
+ return strDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期中某类型的某数值。如增加日期
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param dateType 类型
|
|
|
+ * @param amount 数值
|
|
|
+ * @return 计算后日期字符串
|
|
|
+ */
|
|
|
+ private static String addInteger(String date, int dateType, int amount) {
|
|
|
+ String dateString = null;
|
|
|
+ DateStyleEnum dateStyle = getDateStyle(date);
|
|
|
+ if (dateStyle != null) {
|
|
|
+ Date myDate = StringToDate(date, dateStyle);
|
|
|
+ myDate = addInteger(myDate, dateType, amount);
|
|
|
+ dateString = DateToString(myDate, dateStyle);
|
|
|
+ }
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期中某类型的某数值。如增加日期
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param dateType 类型
|
|
|
+ * @param amount 数值
|
|
|
+ * @return 计算后日期字符串
|
|
|
+ */
|
|
|
+ private static String addInteger(String date, DateStyleEnum dateStyle, int dateType, int amount) {
|
|
|
+ String dateString = null;
|
|
|
+ if (dateStyle != null) {
|
|
|
+ Date myDate = StringToDate(date, dateStyle);
|
|
|
+ myDate = addInteger(myDate, dateType, amount);
|
|
|
+ dateString = DateToString(myDate, dateStyle);
|
|
|
+ }
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期中某类型的某数值。如增加日期
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param dateType 类型
|
|
|
+ * @param amount 数值
|
|
|
+ * @return 计算后日期
|
|
|
+ */
|
|
|
+ private static Date addInteger(Date date, int dateType, int amount) {
|
|
|
+ Date myDate = null;
|
|
|
+ if (date != null) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(dateType, amount);
|
|
|
+ myDate = calendar.getTime();
|
|
|
+ }
|
|
|
+ return myDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周开始时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getBeginDayOfWeek(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
|
|
|
+ if (dayofweek == 1) {
|
|
|
+ dayofweek += 7;
|
|
|
+ }
|
|
|
+ cal.add(Calendar.DATE, 2 - dayofweek);
|
|
|
+ return getDayStartTime(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个日期的开始时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Timestamp getDayStartTime(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ if (null != date) {
|
|
|
+ calendar.setTime(date);
|
|
|
+ }
|
|
|
+ calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ return new Timestamp(calendar.getTimeInMillis());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本周结束时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getEndDayOfWeek(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getBeginDayOfWeek(date));
|
|
|
+ cal.add(Calendar.DAY_OF_WEEK, 6);
|
|
|
+ Date weekEndSta = cal.getTime();
|
|
|
+ return getDayEndTime(weekEndSta);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个日期的结束时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Date getDayEndTime(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ if(null != date){
|
|
|
+ calendar.setTime(date);
|
|
|
+ }
|
|
|
+ calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ return new Timestamp(calendar.getTimeInMillis());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取精确的日期
|
|
|
+ *
|
|
|
+ * @param timestamps 时间long集合
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ private static Date getAccurateDate(List<Long> timestamps) {
|
|
|
+ Date date = null;
|
|
|
+ long timestamp = 0;
|
|
|
+ Map<Long, long[]> map = new HashMap<Long, long[]>();
|
|
|
+ List<Long> absoluteValues = new ArrayList<Long>();
|
|
|
+ if (timestamps != null && timestamps.size() > 0) {
|
|
|
+ if (timestamps.size() > 1) {
|
|
|
+ for (int i = 0; i < timestamps.size(); i++) {
|
|
|
+ for (int j = i + 1; j < timestamps.size(); j++) {
|
|
|
+ long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j));
|
|
|
+ absoluteValues.add(absoluteValue);
|
|
|
+ long[] timestampTmp = {timestamps.get(i), timestamps.get(j)};
|
|
|
+ map.put(absoluteValue, timestampTmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 有可能有相等的情况。如2012-11和2012-11-01。时间戳是相等的
|
|
|
+ long minAbsoluteValue = -1;
|
|
|
+ if (!absoluteValues.isEmpty()) {
|
|
|
+ // 如果timestamps的size为2,这是差值只有一个,因此要给默认值
|
|
|
+ minAbsoluteValue = absoluteValues.get(0);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < absoluteValues.size(); i++) {
|
|
|
+ for (int j = i + 1; j < absoluteValues.size(); j++) {
|
|
|
+ if (minAbsoluteValue > absoluteValues.get(j)) {
|
|
|
+ minAbsoluteValue = absoluteValues.get(j);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (minAbsoluteValue != -1) {
|
|
|
+ long[] timestampsLastTmp = map.get(minAbsoluteValue);
|
|
|
+ if (absoluteValues.size() > 1) {
|
|
|
+ timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
|
|
|
+ } else if (absoluteValues.size() == 1) {
|
|
|
+ // 当timestamps的size为2,需要与当前时间作为参照
|
|
|
+ long dateOne = timestampsLastTmp[0];
|
|
|
+ long dateTwo = timestampsLastTmp[1];
|
|
|
+ if ((Math.abs(dateOne - dateTwo)) < 100000000000L) {
|
|
|
+ timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
|
|
|
+ } else {
|
|
|
+ long now = new Date().getTime();
|
|
|
+ if (Math.abs(dateOne - now) <= Math.abs(dateTwo - now)) {
|
|
|
+ timestamp = dateOne;
|
|
|
+ } else {
|
|
|
+ timestamp = dateTwo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ timestamp = timestamps.get(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (timestamp != 0) {
|
|
|
+ date = new Date(timestamp);
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断字符串是否为日期字符串
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return true or false
|
|
|
+ */
|
|
|
+ public static boolean isDate(String date) {
|
|
|
+ boolean isDate = false;
|
|
|
+ if (date != null) {
|
|
|
+ if (StringToDate(date) != null) {
|
|
|
+ isDate = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return isDate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期字符串的日期风格。失敗返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 日期风格
|
|
|
+ */
|
|
|
+ public static DateStyleEnum getDateStyle(String date) {
|
|
|
+ DateStyleEnum dateStyle = null;
|
|
|
+ Map<Long, DateStyleEnum> map = new HashMap<Long, DateStyleEnum>();
|
|
|
+ List<Long> timestamps = new ArrayList<Long>();
|
|
|
+ for (DateStyleEnum style : DateStyleEnum.values()) {
|
|
|
+ Date dateTmp = StringToDate(date, style.getValue());
|
|
|
+ if (dateTmp != null) {
|
|
|
+ timestamps.add(dateTmp.getTime());
|
|
|
+ map.put(dateTmp.getTime(), style);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dateStyle = map.get(getAccurateDate(timestamps).getTime());
|
|
|
+ return dateStyle;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为日期。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static Date StringToDate(String date) {
|
|
|
+ DateStyleEnum dateStyle = null;
|
|
|
+ return StringToDate(date, dateStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为日期。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param parttern 日期格式
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static Date StringToDate(String date, String parttern) {
|
|
|
+ Date myDate = null;
|
|
|
+ if (date != null) {
|
|
|
+ try {
|
|
|
+ myDate = getDateFormat(parttern).parse(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return myDate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为日期。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param dateStyle 日期风格
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static Date StringToDate(String date, DateStyleEnum dateStyle) {
|
|
|
+ Date myDate = null;
|
|
|
+ if (dateStyle == null) {
|
|
|
+ List<Long> timestamps = new ArrayList<Long>();
|
|
|
+ for (DateStyleEnum style : DateStyleEnum.values()) {
|
|
|
+ Date dateTmp = StringToDate(date, style.getValue());
|
|
|
+ if (dateTmp != null) {
|
|
|
+ timestamps.add(dateTmp.getTime());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ myDate = getAccurateDate(timestamps);
|
|
|
+ } else {
|
|
|
+ myDate = StringToDate(date, dateStyle.getValue());
|
|
|
+ }
|
|
|
+ return myDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期转化为日期字符串。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param parttern 日期格式
|
|
|
+ * @return 日期字符串
|
|
|
+ */
|
|
|
+ public static String DateToString(Date date, String parttern) {
|
|
|
+ String dateString = null;
|
|
|
+ if (date != null) {
|
|
|
+ try {
|
|
|
+ dateString = getDateFormat(parttern).format(date);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期转化为日期字符串。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param dateStyle 日期风格
|
|
|
+ * @return 日期字符串
|
|
|
+ */
|
|
|
+ public static String DateToString(Date date, DateStyleEnum dateStyle) {
|
|
|
+ String dateString = null;
|
|
|
+ if (dateStyle != null) {
|
|
|
+ dateString = DateToString(date, dateStyle.getValue());
|
|
|
+ }
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为另一日期字符串。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 旧日期字符串
|
|
|
+ * @param parttern 新日期格式
|
|
|
+ * @return 新日期字符串
|
|
|
+ */
|
|
|
+ public static String StringToString(String date, String parttern) {
|
|
|
+ return StringToString(date, null, parttern);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为另一日期字符串。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 旧日期字符串
|
|
|
+ * @param dateStyle 新日期风格
|
|
|
+ * @return 新日期字符串
|
|
|
+ */
|
|
|
+ public static String StringToString(String date, DateStyleEnum dateStyle) {
|
|
|
+ return StringToString(date, null, dateStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为另一日期字符串。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 旧日期字符串
|
|
|
+ * @param olddParttern 旧日期格式
|
|
|
+ * @param newParttern 新日期格式
|
|
|
+ * @return 新日期字符串
|
|
|
+ */
|
|
|
+ public static String StringToString(String date, String olddParttern, String newParttern) {
|
|
|
+ String dateString = null;
|
|
|
+ if (olddParttern == null) {
|
|
|
+ DateStyleEnum style = getDateStyle(date);
|
|
|
+ if (style != null) {
|
|
|
+ Date myDate = StringToDate(date, style.getValue());
|
|
|
+ dateString = DateToString(myDate, newParttern);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Date myDate = StringToDate(date, olddParttern);
|
|
|
+ dateString = DateToString(myDate, newParttern);
|
|
|
+ }
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将日期字符串转化为另一日期字符串。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 旧日期字符串
|
|
|
+ * @param olddDteStyle 旧日期风格
|
|
|
+ * @param newDateStyle 新日期风格
|
|
|
+ * @return 新日期字符串
|
|
|
+ */
|
|
|
+ public static String StringToString(String date, DateStyleEnum olddDteStyle, DateStyleEnum newDateStyle) {
|
|
|
+ String dateString = null;
|
|
|
+ if (olddDteStyle == null) {
|
|
|
+ DateStyleEnum style = getDateStyle(date);
|
|
|
+ dateString = StringToString(date, style.getValue(), newDateStyle.getValue());
|
|
|
+ } else {
|
|
|
+ dateString = StringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
|
|
|
+ }
|
|
|
+ return dateString;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的年份。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param yearAmount 增加数量。可为负数
|
|
|
+ * @return 增加年份后的日期字符串
|
|
|
+ */
|
|
|
+ public static String addYear(String date, int yearAmount) {
|
|
|
+ return addInteger(date, Calendar.YEAR, yearAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的年份。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param yearAmount 增加数量。可为负数
|
|
|
+ * @return 增加年份后的日期
|
|
|
+ */
|
|
|
+ public static Date addYear(Date date, int yearAmount) {
|
|
|
+ return addInteger(date, Calendar.YEAR, yearAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的月份。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param yearAmount 增加数量。可为负数
|
|
|
+ * @return 增加月份后的日期字符串
|
|
|
+ */
|
|
|
+ public static String addMonth(String date, int yearAmount) {
|
|
|
+ return addInteger(date, Calendar.MONTH, yearAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的月份。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param yearAmount 增加数量。可为负数
|
|
|
+ * @return 增加月份后的日期
|
|
|
+ */
|
|
|
+ public static Date addMonth(Date date, int yearAmount) {
|
|
|
+ return addInteger(date, Calendar.MONTH, yearAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的天数。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param dayAmount 增加数量。可为负数
|
|
|
+ * @return 增加天数后的日期字符串
|
|
|
+ * @deprecated use {@link #addDay(String, int, DateStyleEnum)}
|
|
|
+ */
|
|
|
+ @Deprecated
|
|
|
+ public static String addDay(String date, int dayAmount) {
|
|
|
+ return addInteger(date, Calendar.DATE, dayAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的天数。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param dayAmount 增加数量。可为负数
|
|
|
+ * @param style 日期样式
|
|
|
+ * @return 增加天数后的日期字符串
|
|
|
+ */
|
|
|
+ public static String addDay(String date, int dayAmount, DateStyleEnum style) {
|
|
|
+ return addInteger(date, style, Calendar.DATE, dayAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的天数。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param dayAmount 增加数量。可为负数
|
|
|
+ * @return 增加天数后的日期
|
|
|
+ */
|
|
|
+ public static Date addDay(Date date, int dayAmount) {
|
|
|
+ return addInteger(date, Calendar.DATE, dayAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的小时。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param hourAmount 增加数量。可为负数
|
|
|
+ * @return 增加小时后的日期字符串
|
|
|
+ */
|
|
|
+ public static String addHour(String date, int hourAmount) {
|
|
|
+ return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的小时。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param hourAmount 增加数量。可为负数
|
|
|
+ * @return 增加小时后的日期
|
|
|
+ */
|
|
|
+ public static Date addHour(Date date, int hourAmount) {
|
|
|
+ return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的分钟。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param hourAmount 增加数量。可为负数
|
|
|
+ * @return 增加分钟后的日期字符串
|
|
|
+ */
|
|
|
+ public static String addMinute(String date, int hourAmount) {
|
|
|
+ return addInteger(date, Calendar.MINUTE, hourAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的分钟。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param hourAmount 增加数量。可为负数
|
|
|
+ * @return 增加分钟后的日期
|
|
|
+ */
|
|
|
+ public static Date addMinute(Date date, int hourAmount) {
|
|
|
+ return addInteger(date, Calendar.MINUTE, hourAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的秒钟。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param hourAmount 增加数量。可为负数
|
|
|
+ * @return 增加秒钟后的日期字符串
|
|
|
+ */
|
|
|
+ public static String addSecond(String date, int hourAmount) {
|
|
|
+ return addInteger(date, Calendar.SECOND, hourAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加日期的秒钟。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param hourAmount 增加数量。可为负数
|
|
|
+ * @return 增加秒钟后的日期
|
|
|
+ */
|
|
|
+ public static Date addSecond(Date date, int hourAmount) {
|
|
|
+ return addInteger(date, Calendar.SECOND, hourAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的年份。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 年份
|
|
|
+ */
|
|
|
+ public static int getYear(String date) {
|
|
|
+ return getYear(StringToDate(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的年份。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 年份
|
|
|
+ */
|
|
|
+ public static int getYear(Date date) {
|
|
|
+ return getInteger(date, Calendar.YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的月份。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 月份
|
|
|
+ */
|
|
|
+ public static int getMonth(String date) {
|
|
|
+ return getMonth(StringToDate(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的月份
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 月份
|
|
|
+ */
|
|
|
+ public static String getMonthStr(Date date) {
|
|
|
+ return monthSdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的月份。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 月份
|
|
|
+ */
|
|
|
+ public static int getMonth(Date date) {
|
|
|
+ return getInteger(date, Calendar.MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的天数。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 天
|
|
|
+ */
|
|
|
+ public static int getDay(String date) {
|
|
|
+ return getDay(StringToDate(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的天数。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 天
|
|
|
+ */
|
|
|
+ public static int getDay(Date date) {
|
|
|
+ return getInteger(date, Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的小时。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 小时
|
|
|
+ */
|
|
|
+ public static int getHour(String date) {
|
|
|
+ return getHour(StringToDate(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的小时。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 小时
|
|
|
+ */
|
|
|
+ public static int getHour(Date date) {
|
|
|
+ return getInteger(date, Calendar.HOUR_OF_DAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的分钟。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 分钟
|
|
|
+ */
|
|
|
+ public static int getMinute(String date) {
|
|
|
+ return getMinute(StringToDate(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的分钟。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 分钟
|
|
|
+ */
|
|
|
+ public static int getMinute(Date date) {
|
|
|
+ return getInteger(date, Calendar.MINUTE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的秒钟。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 秒钟
|
|
|
+ */
|
|
|
+ public static int getSecond(String date) {
|
|
|
+ return getSecond(StringToDate(date));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的秒钟。失败返回0。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 秒钟
|
|
|
+ */
|
|
|
+ public static int getSecond(Date date) {
|
|
|
+ return getInteger(date, Calendar.SECOND);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期 。默认yyyy-MM-dd格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static String getDate(String date) {
|
|
|
+ return StringToString(date, DateStyleEnum.YYYY_MM_DD);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期。默认yyyy-MM-dd格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static String getDate(Date date) {
|
|
|
+ return DateToString(date, DateStyleEnum.YYYY_MM_DD);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期。默认yyyy-MM-dd HH:mm:ss格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static String getDateTime(Date date) {
|
|
|
+ return DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期。默认yyyy-MM-dd HH:mm格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static String getDateTimeExcludeSec(Date date) {
|
|
|
+ return DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期。默认yyyy-MM-dd HH:mm格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 日期
|
|
|
+ */
|
|
|
+ public static Date getDateTimeExcludeSec(String date) {
|
|
|
+ return StringToDate(date, DateStyleEnum.YYYY_MM_DD_HH_MM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 时间
|
|
|
+ */
|
|
|
+ public static String getTime(String date) {
|
|
|
+ return StringToString(date, DateStyleEnum.HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 时间
|
|
|
+ */
|
|
|
+ public static String getTime(Date date) {
|
|
|
+ return DateToString(date, DateStyleEnum.HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的星期。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @return 星期
|
|
|
+ */
|
|
|
+ public static WeekEnum getWeek(String date) {
|
|
|
+ WeekEnum week = null;
|
|
|
+ DateStyleEnum dateStyle = getDateStyle(date);
|
|
|
+ if (dateStyle != null) {
|
|
|
+ Date myDate = StringToDate(date, dateStyle);
|
|
|
+ week = getWeek(myDate);
|
|
|
+ }
|
|
|
+ return week;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的星期。失败返回null。
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 星期
|
|
|
+ */
|
|
|
+ public static WeekEnum getWeek(Date date) {
|
|
|
+ WeekEnum week = null;
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ switch (weekNumber) {
|
|
|
+ case 0:
|
|
|
+ week = WeekEnum.SUNDAY;
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ week = WeekEnum.MONDAY;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ week = WeekEnum.TUESDAY;
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ week = WeekEnum.WEDNESDAY;
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ week = WeekEnum.THURSDAY;
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ week = WeekEnum.FRIDAY;
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ week = WeekEnum.SATURDAY;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return week;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个日期相差的天数
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param otherDate 另一个日期字符串
|
|
|
+ * @return 相差天数
|
|
|
+ */
|
|
|
+ public static int getIntervalDays(String date, String otherDate) {
|
|
|
+ return getIntervalDays(StringToDate(date), StringToDate(otherDate));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param date 日期
|
|
|
+ * @param otherDate 另一个日期mingt
|
|
|
+ * @return 相差天数
|
|
|
+ */
|
|
|
+ public static int getIntervalDays(Date date, Date otherDate) {
|
|
|
+ //date = DateUtil.StringToDate(DateUtil.getDate(date));
|
|
|
+ long time = Math.abs(date.getTime() - otherDate.getTime());
|
|
|
+ long day = time / (24 * 60 * 60 * 1000);
|
|
|
+ return (int) day;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param date
|
|
|
+ * @param otherDate
|
|
|
+ * @return 相差年数
|
|
|
+ */
|
|
|
+ public static long getIntervalYear(Date date, Date otherDate) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int yearNow = cal.get(Calendar.YEAR);
|
|
|
+ cal.setTime(otherDate);
|
|
|
+ int yearOther = cal.get(Calendar.YEAR);
|
|
|
+ long year = yearNow - yearOther;
|
|
|
+ return year;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取月份差
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param otherDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long getIntervalMonths(Date date, Date otherDate) {
|
|
|
+ long months;
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int yearNow = cal.get(Calendar.YEAR);
|
|
|
+ int monthNow = cal.get(Calendar.MONTH);
|
|
|
+ cal.setTime(otherDate);
|
|
|
+ int yearOther = cal.get(Calendar.YEAR);
|
|
|
+ int monthOther = cal.get(Calendar.MONTH);
|
|
|
+ long year = yearNow - yearOther;
|
|
|
+ if (year >= 1) {
|
|
|
+ months = year * 12 + monthNow - monthOther;
|
|
|
+ } else {
|
|
|
+ months = monthNow - monthOther;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return months;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getWeekFromDate(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ int week = c.get(Calendar.DAY_OF_WEEK);
|
|
|
+ switch (week) {
|
|
|
+ case 1:
|
|
|
+ return "星期日";
|
|
|
+ case 2:
|
|
|
+ return "星期一";
|
|
|
+ case 3:
|
|
|
+ return "星期二";
|
|
|
+ case 4:
|
|
|
+ return "星期三";
|
|
|
+ case 5:
|
|
|
+ return "星期四";
|
|
|
+ case 6:
|
|
|
+ return "星期五";
|
|
|
+ case 7:
|
|
|
+ return "星期六";
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定天数之前的时间(相对于当前天数)
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @param dayNo 当前时间的几天之前
|
|
|
+ * @return
|
|
|
+ * @Title: getLastDays
|
|
|
+ * @date 2014-3-26 下午11:23:57
|
|
|
+ * @author Lawrence
|
|
|
+ */
|
|
|
+ public static Date getLastTimeForDays(Date currentDate, Integer dayNo) {
|
|
|
+ if (null == currentDate || null == dayNo) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(currentDate);
|
|
|
+ c.add(Calendar.DATE, -dayNo);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定月数之前的时间(相对于当前天数)
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @param monthNo 当前时间的几月之前
|
|
|
+ * @return
|
|
|
+ * @Title: getLastDays
|
|
|
+ * @date 2014-3-26 下午11:23:57
|
|
|
+ * @author Lawrence
|
|
|
+ */
|
|
|
+ public static Date getLastTimeForMonths(Date currentDate, Integer monthNo) {
|
|
|
+ if (null == currentDate || null == monthNo) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(currentDate);
|
|
|
+ c.add(Calendar.MONTH, -monthNo);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定月数之前的时间(相对于当前天数)
|
|
|
+ *
|
|
|
+ * @param currentDate 当前时间
|
|
|
+ * @param yearNo 当前时间的几年之前
|
|
|
+ * @return
|
|
|
+ * @Title: getLastDays
|
|
|
+ * @date 2014-3-26 下午11:23:57
|
|
|
+ * @author Lawrence
|
|
|
+ */
|
|
|
+ public static Date getLastTimeForYears(Date currentDate, Integer yearNo) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(currentDate);
|
|
|
+ c.add(Calendar.YEAR, -yearNo);
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当天00:00:00
|
|
|
+ *
|
|
|
+ * @param currentDate
|
|
|
+ * @return
|
|
|
+ * @Title: getZeroPoint
|
|
|
+ * @Description: (这里用一句话描述这个方法的作用)
|
|
|
+ * @date 2014年10月10日 上午11:09:35
|
|
|
+ * @author Administrator
|
|
|
+ */
|
|
|
+ public static Date getZeroPoint(Date currentDate) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(currentDate);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0); // 把当前时间小时变成0
|
|
|
+ cal.set(Calendar.MINUTE, 0); // 把当前时间分钟变成0
|
|
|
+ cal.set(Calendar.SECOND, 0); // 把当前时间秒数变成0
|
|
|
+ cal.set(Calendar.MILLISECOND, 0); // 把当前时间毫秒变成0
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当天00:00:00
|
|
|
+ *
|
|
|
+ * @param currentDate
|
|
|
+ * @return
|
|
|
+ * @Title: getZeroPoint
|
|
|
+ * @Description: (这里用一句话描述这个方法的作用)
|
|
|
+ * @date 2014年10月10日 上午11:09:35
|
|
|
+ * @author Administrator
|
|
|
+ */
|
|
|
+ public static Date getZeroPoint(String currentDate) {
|
|
|
+ return getZeroPoint(DateUtils.StringToDate(currentDate, DateStyleEnum.YYYY_MM_DD));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的最后一秒时间
|
|
|
+ *
|
|
|
+ * @param currentDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastPoint(String currentDate) {
|
|
|
+ return getLastPoint(DateUtils.StringToDate(currentDate, DateStyleEnum.YYYY_MM_DD));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期的最后一秒时间
|
|
|
+ *
|
|
|
+ * @param currentDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastPoint(Date currentDate) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(currentDate);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 23); // 把当前时间小时变成
|
|
|
+ cal.set(Calendar.MINUTE, 59); // 把当前时间分钟变成
|
|
|
+ cal.set(Calendar.SECOND, 59); // 把当前时间秒数变成
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定分钟最后一秒时间
|
|
|
+ *
|
|
|
+ * @param currentDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getDateEndTime(Date currentDate) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(currentDate);
|
|
|
+ cal.set(Calendar.SECOND, 59); // 把当前时间秒数变成
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定分钟最后一秒时间
|
|
|
+ *
|
|
|
+ * @param currentDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getDateBegTime(Date currentDate) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(currentDate);
|
|
|
+ cal.set(Calendar.SECOND, 00); // 把当前时间秒数变成
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间比较大小工具类
|
|
|
+ *
|
|
|
+ * @param date1
|
|
|
+ * @param date2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Boolean compareDate(Date date1, Date date2) {
|
|
|
+ try {
|
|
|
+ if (date1.getTime() >= date2.getTime()) {
|
|
|
+ return true;
|
|
|
+ } else if (date1.getTime() < date2.getTime()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString(), e);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转化成日期
|
|
|
+ *
|
|
|
+ * @param millSec(毫秒数)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date LongToDate(Long millSec) {
|
|
|
+ return LongToDate(millSec, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把毫秒转化成日期
|
|
|
+ *
|
|
|
+ * @param parttern(日期格式,例如:MM/ dd/yyyy HH:mm:ss)
|
|
|
+ * @param millSec(毫秒数)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date LongToDate(Long millSec, String parttern) {
|
|
|
+ return new Date(millSec);
|
|
|
+ }
|
|
|
+
|
|
|
+/* public static void main(String[] args){
|
|
|
+// Long currentTime = System.currentTimeMillis();
|
|
|
+// System.out.println(LongToDate(currentTime));
|
|
|
+
|
|
|
+ // Date currentDate = new Date();
|
|
|
+ // currentDate = getZeroPoint(currentDate);
|
|
|
+ // //得到一天前
|
|
|
+ // Date dd = getLastTimeForDays(currentDate, 1);
|
|
|
+ // System.out.println(dd.toLocaleString());
|
|
|
+ // //得到前一周
|
|
|
+ // Date dw = getLastTimeForDays(currentDate, 7);
|
|
|
+ // System.out.println(dw.toLocaleString());
|
|
|
+ // //得到前一个月
|
|
|
+ // Date dm = getLastTimeForMonths(currentDate, 1);
|
|
|
+ // System.out.println(dm.toLocaleString());
|
|
|
+ // //得到前一年
|
|
|
+ // Date dy = getLastTimeForYears(currentDate, 1);
|
|
|
+ // System.out.println(dy.toLocaleString());
|
|
|
+ String date = DateToString(new Date(), "yyyyMMddHHmmss");
|
|
|
+ System.out.println(date);
|
|
|
+ System.out.println(getZeroPoint(new Date()));
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取时间getTime的后N位
|
|
|
+ *
|
|
|
+ * @param time 时间
|
|
|
+ * @return
|
|
|
+ * @Title: getTimeToSix
|
|
|
+ * @date 2016年9月21日 下午5:14:00
|
|
|
+ * @author Chris_He
|
|
|
+ */
|
|
|
+ public static String getTimeToNumber(Date time, Integer n) {
|
|
|
+ String s = String.valueOf(time.getTime());
|
|
|
+ return s.substring(s.length() - n, s.length());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间上下午
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @Title: getNowDateAmPm
|
|
|
+ * @date 2017年8月29日 下午5:08:37
|
|
|
+ * @author Chris_He
|
|
|
+ */
|
|
|
+ public static DateStyleEnum getNowDateAmPm() {
|
|
|
+ GregorianCalendar ca = new GregorianCalendar();
|
|
|
+ int i = ca.get(GregorianCalendar.AM_PM);
|
|
|
+ if (i == 0) {
|
|
|
+ return DateStyleEnum.AM;
|
|
|
+ } else {
|
|
|
+ return DateStyleEnum.PM;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getAfterMinuteDate(Date date, int amount) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.MINUTE, amount);
|
|
|
+ date = cal.getTime();
|
|
|
+// String theDate = sdf.format(date);
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getAfterSecondDate(Date date, int amount) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.SECOND, amount);
|
|
|
+ date = cal.getTime();
|
|
|
+// String theDate = sdf.format(date);
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatFull(Date date) {
|
|
|
+ if (date != null) {
|
|
|
+ return DATE_FORMAT_YYYY_MM_DD_HH_MM_SS.format(date);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取下一天00:00:00
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @Title: getNextDay
|
|
|
+ * @date 2018年12月22日 下午5:08:37
|
|
|
+ * @author Chris_He
|
|
|
+ */
|
|
|
+ public static Date getNextDay(Date date) {
|
|
|
+ return addDay(StringToDate(DateToString(date, DateStyleEnum.YYYY_MM_DD), DateStyleEnum.YYYY_MM_DD), 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// System.out.println(DateUtil.getYear(new Date()));
|
|
|
+ System.out.println(getHour("7:00"));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取下一天指定时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @Title: getNextDay
|
|
|
+ * @date 2018年12月22日 下午5:08:37
|
|
|
+ * @author Chris_He
|
|
|
+ */
|
|
|
+ public static Date getNextDayHour(Date date, int hour, int amount) {
|
|
|
+ Date myDate = null;
|
|
|
+ if (date != null) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DATE, amount);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, hour); // 把当前时间小时变成0
|
|
|
+ calendar.set(Calendar.MINUTE, 0); // 把当前时间分钟变成0
|
|
|
+ calendar.set(Calendar.SECOND, 0); // 把当前时间秒数变成0
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0); // 把当前时间毫秒变成0
|
|
|
+ myDate = calendar.getTime();
|
|
|
+ }
|
|
|
+ return myDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取yyyy-MM
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getMonthStr() {
|
|
|
+ return monthSdf.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间范围内所有天数 yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> getDayList(String startDate, String endDate) {
|
|
|
+ Date dBegin = getZeroPoint(startDate);
|
|
|
+ Date dEnd = getZeroPoint(endDate);
|
|
|
+ List<Date> dateList = new ArrayList<>();
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calBegin.setTime(dBegin);
|
|
|
+ Calendar calEnd = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calEnd.setTime(dEnd);
|
|
|
+ // 此日期是否在指定日期之后
|
|
|
+ dateList.add(dBegin);
|
|
|
+ while (dEnd.after(calBegin.getTime())) {
|
|
|
+ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
|
|
+ calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ dateList.add(calBegin.getTime());
|
|
|
+ }
|
|
|
+ return dateList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间范围内所有天 yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> getDayList(Date startDate, Date endDate) {
|
|
|
+ Date dBegin = getZeroPoint(startDate);
|
|
|
+ Date dEnd = getZeroPoint(endDate);
|
|
|
+ List<Date> dateList = new ArrayList<>();
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calBegin.setTime(dBegin);
|
|
|
+ Calendar calEnd = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calEnd.setTime(dEnd);
|
|
|
+ // 此日期是否在指定日期之后
|
|
|
+ dateList.add(dBegin);
|
|
|
+ while (dEnd.after(calBegin.getTime())) {
|
|
|
+ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
|
|
+ calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ dateList.add(calBegin.getTime());
|
|
|
+ }
|
|
|
+ return dateList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间范围内所有天 yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getDayStrList(Date startDate, Date endDate) {
|
|
|
+ Date dBegin = getZeroPoint(startDate);
|
|
|
+ Date dEnd = getZeroPoint(endDate);
|
|
|
+ List<String> dateList = new ArrayList<>();
|
|
|
+ Calendar calBegin = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calBegin.setTime(dBegin);
|
|
|
+ Calendar calEnd = Calendar.getInstance();
|
|
|
+ // 使用给定的 Date 设置此 Calendar 的时间
|
|
|
+ calEnd.setTime(dEnd);
|
|
|
+ // 此日期是否在指定日期之后
|
|
|
+ dateList.add(getDate(dBegin));
|
|
|
+ while (dEnd.after(calBegin.getTime())) {
|
|
|
+ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
|
|
+ calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ dateList.add(getDate(calBegin.getTime()));
|
|
|
+ }
|
|
|
+ return dateList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个时间段内所有月份
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> getMonthList(String startDate, String endDate) {
|
|
|
+ List<Date> result = new ArrayList<>();
|
|
|
+ Calendar min = Calendar.getInstance();
|
|
|
+ Calendar max = Calendar.getInstance();
|
|
|
+ min.setTime(StringToDate(startDate, DateStyleEnum.YYYY_MM));
|
|
|
+ min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
|
|
|
+ max.setTime(StringToDate(endDate, DateStyleEnum.YYYY_MM));
|
|
|
+ max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
|
|
|
+ Calendar curr = min;
|
|
|
+ while (curr.before(max)) {
|
|
|
+ result.add(curr.getTime());
|
|
|
+ curr.add(Calendar.MONTH, 1);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getSeason(Date date) {
|
|
|
+
|
|
|
+ int season = 0;
|
|
|
+
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ int month = c.get(Calendar.MONTH);
|
|
|
+ switch (month) {
|
|
|
+ case Calendar.JANUARY:
|
|
|
+ case Calendar.FEBRUARY:
|
|
|
+ case Calendar.MARCH:
|
|
|
+ season = 1;
|
|
|
+ break;
|
|
|
+ case Calendar.APRIL:
|
|
|
+ case Calendar.MAY:
|
|
|
+ case Calendar.JUNE:
|
|
|
+ season = 2;
|
|
|
+ break;
|
|
|
+ case Calendar.JULY:
|
|
|
+ case Calendar.AUGUST:
|
|
|
+ case Calendar.SEPTEMBER:
|
|
|
+ season = 3;
|
|
|
+ break;
|
|
|
+ case Calendar.OCTOBER:
|
|
|
+ case Calendar.NOVEMBER:
|
|
|
+ case Calendar.DECEMBER:
|
|
|
+ season = 4;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return season;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得季度月
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date[] getSeasonDate(Date date) {
|
|
|
+ Date[] season = new Date[3];
|
|
|
+
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+
|
|
|
+ int nSeason = getSeason(date);
|
|
|
+ if (nSeason == 1) {// 第一季度
|
|
|
+ c.set(Calendar.MONTH, Calendar.JANUARY);
|
|
|
+ season[0] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.FEBRUARY);
|
|
|
+ season[1] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.MARCH);
|
|
|
+ season[2] = c.getTime();
|
|
|
+ } else if (nSeason == 2) {// 第二季度
|
|
|
+ c.set(Calendar.MONTH, Calendar.APRIL);
|
|
|
+ season[0] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.MAY);
|
|
|
+ season[1] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.JUNE);
|
|
|
+ season[2] = c.getTime();
|
|
|
+ } else if (nSeason == 3) {// 第三季度
|
|
|
+ c.set(Calendar.MONTH, Calendar.JULY);
|
|
|
+ season[0] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.AUGUST);
|
|
|
+ season[1] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.SEPTEMBER);
|
|
|
+ season[2] = c.getTime();
|
|
|
+ } else if (nSeason == 4) {// 第四季度
|
|
|
+ c.set(Calendar.MONTH, Calendar.OCTOBER);
|
|
|
+ season[0] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.NOVEMBER);
|
|
|
+ season[1] = c.getTime();
|
|
|
+ c.set(Calendar.MONTH, Calendar.DECEMBER);
|
|
|
+ season[2] = c.getTime();
|
|
|
+ }
|
|
|
+ return season;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得季度第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getFirstDateOfSeason(Date date) {
|
|
|
+ return getFirstDateOfMonth(getSeasonDate(date)[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得季度最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDateOfSeason(Date date) {
|
|
|
+ return getLastDateOfMonth(getSeasonDate(date)[2]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得月第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getFirstDateOfMonth(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取得月最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDateOfMonth(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return c.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间范围内的每个季度开始时间
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> getSeasonDateList(Date startDate, Date endDate) {
|
|
|
+ List<Date> list = new ArrayList<>();
|
|
|
+ Date firstDateOfSeason = getFirstDateOfSeason(startDate);
|
|
|
+ list.add(firstDateOfSeason);
|
|
|
+ Date lastDateOfSeason = getFirstDateOfSeason(endDate);
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(firstDateOfSeason);
|
|
|
+ while (c.getTime().before(lastDateOfSeason)) {
|
|
|
+ c.add(Calendar.MONTH, 3);
|
|
|
+ list.add(c.getTime());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所在年第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfYear(Date date) {
|
|
|
+ final Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所在年第一天
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getFirstDayOfYear(String year) {
|
|
|
+ final Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(StringToDate(year, DateStyleEnum.YYYY));
|
|
|
+ cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所在年最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDayOfYear(Date date) {
|
|
|
+ final Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所在年最后一天
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastDayOfYear(String year) {
|
|
|
+ final Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(StringToDate(year, DateStyleEnum.YYYY));
|
|
|
+ cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时间范围内的每个年开始时间列表
|
|
|
+ *
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Date> getYearDateList(Date startDate, Date endDate) {
|
|
|
+ List<Date> list = new ArrayList<>();
|
|
|
+ Date firstDate = getFirstDayOfYear(startDate);
|
|
|
+ list.add(firstDate);
|
|
|
+ Date lastDate = getFirstDayOfYear(endDate);
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(firstDate);
|
|
|
+ while (c.getTime().before(lastDate)) {
|
|
|
+ c.add(Calendar.YEAR, 1);
|
|
|
+ list.add(c.getTime());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getChineseDate(Date date) {
|
|
|
+ String chineseDate = null;
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
|
|
|
+
|
|
|
+
|
|
|
+ return chineseDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static StringBuilder getChineseTime(Date date) {
|
|
|
+ String sign;
|
|
|
+ StringBuilder chineseTime = new StringBuilder();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
|
|
+ String time = sdf.format(date);
|
|
|
+
|
|
|
+ String[] split = time.split(":");
|
|
|
+
|
|
|
+
|
|
|
+ if(split[0].equals("00")){
|
|
|
+ chineseTime.append("零时");
|
|
|
+ }else{
|
|
|
+ String s = formatDigit(split[0]);
|
|
|
+ String substring1 = s.substring(0, 1);
|
|
|
+ String substring2 = s.substring(1, 2);
|
|
|
+ if(substring1.equals("零")){
|
|
|
+
|
|
|
+ }else if(substring1.equals("一")){
|
|
|
+ chineseTime.append("十");
|
|
|
+ }else{
|
|
|
+ chineseTime.append(substring1).append("十");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!substring2.equals("零")){
|
|
|
+ chineseTime.append(substring2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ chineseTime.append("时");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(split[1].equals("00")){
|
|
|
+ chineseTime.append("整");
|
|
|
+ }else {
|
|
|
+
|
|
|
+ String s = formatDigit(split[1]);
|
|
|
+
|
|
|
+ String substring1 = s.substring(0, 1);
|
|
|
+ String substring2 = s.substring(1, 2);
|
|
|
+
|
|
|
+ if(substring1.equals("零")){
|
|
|
+
|
|
|
+ }else if(substring1.equals("一")){
|
|
|
+ chineseTime.append("十");
|
|
|
+ }else{
|
|
|
+ chineseTime.append(substring1).append("十");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!substring2.equals("零")){
|
|
|
+ chineseTime.append(substring2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ chineseTime.append("分");
|
|
|
+ }
|
|
|
+//
|
|
|
+
|
|
|
+ return chineseTime;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String formatDigit(String sign) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for(int i=0;i<sign.length();i++){
|
|
|
+ String substring = sign.substring(i, i + 1);
|
|
|
+ switch (substring){
|
|
|
+ case "0":
|
|
|
+ sb.append("零");
|
|
|
+ break;
|
|
|
+ case "1":
|
|
|
+ sb.append("一");
|
|
|
+ break;
|
|
|
+ case "2":
|
|
|
+ sb.append("二");
|
|
|
+ break;
|
|
|
+ case "3":
|
|
|
+ sb.append("三");
|
|
|
+ break;
|
|
|
+ case "4":
|
|
|
+ sb.append("四");
|
|
|
+ break;
|
|
|
+ case "5":
|
|
|
+ sb.append("五");
|
|
|
+ break;
|
|
|
+ case "6":
|
|
|
+ sb.append("六");
|
|
|
+ break;
|
|
|
+ case "7":
|
|
|
+ sb.append("七");
|
|
|
+ break;
|
|
|
+ case "8":
|
|
|
+ sb.append("八");
|
|
|
+ break;
|
|
|
+ case "9":
|
|
|
+ sb.append("九");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ sign = sb.toString();
|
|
|
+
|
|
|
+
|
|
|
+ return sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取出当前日期到指定日期的周数据差值
|
|
|
+ *
|
|
|
+ * @param date 指日 定日期
|
|
|
+ * @param otherDate 当前日期
|
|
|
+ * @return 周数差
|
|
|
+ */
|
|
|
+ public static long intervalWeeks(Date date, Date otherDate) {
|
|
|
+ int intervalDays = getIntervalDays(date, otherDate) + 1;
|
|
|
+ int i = intervalDays % 7;
|
|
|
+ long weekNum = Math.abs((date.getTime() - otherDate.getTime()) / (7 * 60 * 60 * 24 * 1000));
|
|
|
+
|
|
|
+ weekNum += 1;
|
|
|
+
|
|
|
+ return weekNum;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static long intervalWeeks(String date, String otherDate) {
|
|
|
+ Date date1 = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
|
|
|
+ Date date2 = DateUtils.StringToDate(otherDate, DateStyleEnum.YYYY_MM_DD);
|
|
|
+
|
|
|
+
|
|
|
+ return intervalWeeks(date1, date2);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个时间相差多少小时多少分钟
|
|
|
+ *
|
|
|
+ * @param startTime
|
|
|
+ * @param endTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getDistanceHourMin(Date startTime, Date endTime) {
|
|
|
+ long hour = 0;
|
|
|
+ long min = 0;
|
|
|
+ try {
|
|
|
+ long time1 = startTime.getTime();
|
|
|
+ long time2 = endTime.getTime();
|
|
|
+ long diff;
|
|
|
+ if (time1 < time2) {
|
|
|
+ diff = time2 - time1;
|
|
|
+ } else {
|
|
|
+ diff = time1 - time2;
|
|
|
+ }
|
|
|
+ hour = (diff / (60 * 60 * 1000));
|
|
|
+ min = ((diff / (60 * 1000)) - hour * 60);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return hour + "小时" + min + "分";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date getDateHourMin(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return StringToDate(DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM), DateStyleEnum.YYYY_MM_DD_HH_MM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断一个日期是否是周六、周日
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static int isWeekend(String date) throws ParseException {
|
|
|
+ DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date bdate = format1.parse(date);
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(bdate);
|
|
|
+ if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
|
|
|
+ return 1;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|