|
|
@@ -0,0 +1,301 @@
|
|
|
+package me.zhengjie.utils;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 日期处理
|
|
|
+ *
|
|
|
+ * @author hui.deng
|
|
|
+ *
|
|
|
+ * @date 2016年12月21日 下午12:53:33
|
|
|
+ */
|
|
|
+public class DateFormatUtils {
|
|
|
+ /** 时间格式(yyyy-MM-dd) */
|
|
|
+ public final static String DATE_PATTERN = "yyyy-MM-dd";
|
|
|
+ public final static String DATE_PATTERN2 = "yyyy/MM/dd";
|
|
|
+ public final static String DATE_PATTERN3 = "yyyy.MM.dd";
|
|
|
+ public final static String DATE_PATTERN4 = "yyyyMMdd";
|
|
|
+
|
|
|
+ public final static String DATE_PATTERN6 = "yyyy/M/dd";
|
|
|
+ public final static String DATE_PATTERN7 = "yyyy/MM/d";
|
|
|
+ public final static String DATE_PATTERN8 = "yyyy/M/d";
|
|
|
+ /** 时间格式(yyyy-MM-dd HH:mm:ss) */
|
|
|
+ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ /** 时间格式(yyyy-MM-dd) */
|
|
|
+ public final static Pattern p1 = Pattern.compile("\\d{4}(\\-)\\d{2}(\\-)\\d{2}");
|
|
|
+
|
|
|
+ /** 时间格式(yyyy.MM.dd) */
|
|
|
+ public final static Pattern p3 = Pattern.compile("\\d{4}(\\.)\\d{2}(\\.)\\d{2}");
|
|
|
+ /** 时间格式(yyyyMMdd) */
|
|
|
+ public final static Pattern p4 = Pattern.compile("\\d{4}\\d{2}\\d{2}");
|
|
|
+ public final static Pattern p5 = Pattern.compile(
|
|
|
+ "[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d");
|
|
|
+ /** 时间格式(yyyy/MM/dd) */
|
|
|
+ public final static Pattern p2 = Pattern.compile("\\d{4}(\\/)\\d{2}(\\/)\\d{2}");
|
|
|
+ /** 时间格式(yyyy/MM/d) */
|
|
|
+ public final static Pattern p7 = Pattern.compile("\\d{4}(\\/)\\d{2}(\\/)\\d{1}");
|
|
|
+ /** 时间格式(yyyy/M/dd) */
|
|
|
+ public final static Pattern p6 = Pattern.compile("\\d{4}(\\/)\\d{1}(\\/)\\d{2}");
|
|
|
+ /** 时间格式(yyyy/M/d) */
|
|
|
+ public final static Pattern p8 = Pattern.compile("\\d{4}(\\/)\\d{1}(\\/)\\d{1}");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式化 日期格式为:yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 返回yyyy-MM-dd格式日期
|
|
|
+ */
|
|
|
+ public static String format(Date date) {
|
|
|
+ return format(date, DATE_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式化 日期格式为:yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 返回yyyy-MM-dd格式日期
|
|
|
+ */
|
|
|
+ public static String formatPattern(Date date, String parrern) {
|
|
|
+ return format(date, parrern);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式化 日期格式为:yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 返回yyyy-MM-dd HH:mm:ss格式日期
|
|
|
+ */
|
|
|
+ public static String formatDatetime(Date date) {
|
|
|
+ return format(date, DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到现在的时间 日期格式化 日期格式为:yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @return 返回yyyy-MM-dd HH:mm:ss格式日期
|
|
|
+ */
|
|
|
+ public static String nowTime() {
|
|
|
+ return format(new Date(), DATE_TIME_PATTERN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式化 日期格式为:yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param date 日期
|
|
|
+ * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
|
|
|
+ * @return 返回yyyy-MM-dd格式日期
|
|
|
+ */
|
|
|
+ public static String format(Date date, String pattern) {
|
|
|
+ if (date != null) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
|
+ return df.format(date);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取linux时间戳(精确到秒)
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNowTimeStamp() {
|
|
|
+ long time = System.currentTimeMillis();
|
|
|
+ String nowTimeStamp = String.valueOf(time / 1000);
|
|
|
+ return nowTimeStamp;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BigDecimal getBetweenYear(Date start, Date end) {
|
|
|
+ long time = end.getTime() - start.getTime();
|
|
|
+ BigDecimal year = new BigDecimal(time / (1000 * 60 * 60 * 24 * 365.0));
|
|
|
+ year = year.setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ return year;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式字符串转化为对应的date日期 目前支持格式: yyyy-MM-dd yyyy.MM.dd yyyy/MM/dd yyyyMMdd
|
|
|
+ * yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param timeStr
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date timeStrToDate(String timeStr) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ String format = null;
|
|
|
+ Matcher matcher = p1.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN;
|
|
|
+ }
|
|
|
+ matcher = p2.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN2;
|
|
|
+ }
|
|
|
+ matcher = p3.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN3;
|
|
|
+ }
|
|
|
+ matcher = p4.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN4;
|
|
|
+ }
|
|
|
+// matcher = p5.matcher(timeStr);
|
|
|
+// if (matcher.find()) {
|
|
|
+// format = DATE_PATTERN5;
|
|
|
+// }
|
|
|
+ /** 时间格式(yyyy/M/dd) */
|
|
|
+ matcher = p6.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN6;
|
|
|
+ }
|
|
|
+ matcher = p7.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN7;
|
|
|
+ }
|
|
|
+ matcher = p8.matcher(timeStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ format = DATE_PATTERN8;
|
|
|
+ }
|
|
|
+ if (format == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return sdf.parse(timeStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当天零时零分零秒
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date initDateByDay() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+ calendar.set(Calendar.HOUR, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上个季度的时间范围
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getLastQuarter() {
|
|
|
+
|
|
|
+ Calendar endCalendar = Calendar.getInstance();
|
|
|
+
|
|
|
+ endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
|
|
|
+ endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+
|
|
|
+ return endCalendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据时间得到季度
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getQuarter(Date date) {
|
|
|
+
|
|
|
+ Calendar endCalendar = Calendar.getInstance();
|
|
|
+ endCalendar.setTime(date);
|
|
|
+
|
|
|
+ endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
|
|
|
+ endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+
|
|
|
+ return endCalendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到月份加上季度
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getQuarterByDate(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ int year = calendar.get(Calendar.YEAR);
|
|
|
+ int month = calendar.get(Calendar.MONTH) + 1;
|
|
|
+ int quarter = 0;
|
|
|
+ if (month >= 1 && month <= 3) {
|
|
|
+ quarter = 1;
|
|
|
+ } else if (month >= 4 && month <= 6) {
|
|
|
+ quarter = 2;
|
|
|
+ } else if (month >= 7 && month <= 9) {
|
|
|
+ quarter = 3;
|
|
|
+ } else if (month >= 10 && month <= 12) {
|
|
|
+ quarter = 4;
|
|
|
+ }
|
|
|
+ return new String(year + "0" + quarter);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String[] getThreeQuarter() {
|
|
|
+ String[] three = new String[3];
|
|
|
+ Date lastquarter = getLastQuarter();
|
|
|
+ // 最近的第一季度
|
|
|
+ three[0] = getQuarterByDate(lastquarter);
|
|
|
+
|
|
|
+ Date lasttwoquarter = getQuarter(lastquarter);
|
|
|
+ // 最近的第二季度
|
|
|
+ three[1] = getQuarterByDate(lasttwoquarter);
|
|
|
+
|
|
|
+ Date lastThreequarter = getQuarter(lasttwoquarter);
|
|
|
+ // 最近的第三季度
|
|
|
+ three[2] = getQuarterByDate(lastThreequarter);
|
|
|
+ return three;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一插入日期格式为yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param timeStr
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static String format(String timeStr, String formatPattern) {
|
|
|
+ Date d = timeStrToDate(timeStr);
|
|
|
+ return formatPattern(d, formatPattern);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一插入日期格式为yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param timeStr 时间格式
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static String defaultFormat(String timeStr) {
|
|
|
+ Date d = timeStrToDate(timeStr);
|
|
|
+ return formatPattern(d, DATE_PATTERN);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getLastYear() {
|
|
|
+ SimpleDateFormat formats = new SimpleDateFormat("yyyy");
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.add(Calendar.YEAR, -1);
|
|
|
+ Date date = c.getTime();
|
|
|
+ return formats.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(timeStrToDate("1982/7/23"));
|
|
|
+ }
|
|
|
+}
|