|
@@ -0,0 +1,785 @@
|
|
|
+package com.caimei365.order.utils;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.GregorianCalendar;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 日期工具类, 继承org.apache.commons.lang3.time.DateUtils类
|
|
|
+ * @author LG
|
|
|
+ * @date 2016年3月22日
|
|
|
+ * @version 1.0
|
|
|
+ */
|
|
|
+public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
|
|
+
|
|
|
+
|
|
|
+ //fixme lwt 已经与www的 DateUtils 整合
|
|
|
+ /**
|
|
|
+ * 英文简写(默认)如:2010-12-01
|
|
|
+ */
|
|
|
+ public static String FORMAT_SHORT = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前日期字符串 格式(yyyy-MM-dd)
|
|
|
+ */
|
|
|
+ public static String getDate() {
|
|
|
+ return getDate("yyyy-MM-dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String[] parsePatterns = {
|
|
|
+ "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
|
|
+ "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
|
|
|
+ "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
|
|
|
+ */
|
|
|
+ public static String getDate(String pattern) {
|
|
|
+ return DateFormatUtils.format(new Date(), pattern);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
|
|
|
+ */
|
|
|
+ public static String formatDate(Date date, Object... pattern) {
|
|
|
+ String formatDate = null;
|
|
|
+ if (pattern != null && pattern.length > 0) {
|
|
|
+ formatDate = DateFormatUtils.format(date, pattern[0].toString());
|
|
|
+ } else {
|
|
|
+ formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
|
|
|
+ }
|
|
|
+ return formatDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public static String formatDateTime(Date date) {
|
|
|
+ return formatDate(date, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前时间字符串 格式(HH:mm:ss)
|
|
|
+ */
|
|
|
+ public static String getTime() {
|
|
|
+ return formatDate(new Date(), "HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
|
|
|
+ */
|
|
|
+ public static String getDateTime() {
|
|
|
+ return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前年份字符串 格式(yyyy)
|
|
|
+ */
|
|
|
+ public static String getYear() {
|
|
|
+ return formatDate(new Date(), "yyyy");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前月份字符串 格式(MM)
|
|
|
+ */
|
|
|
+ public static String getMonth() {
|
|
|
+ return formatDate(new Date(), "MM");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当天字符串 格式(dd)
|
|
|
+ */
|
|
|
+ public static String getDay() {
|
|
|
+ return formatDate(new Date(), "dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到当前星期字符串 格式(E)星期几
|
|
|
+ */
|
|
|
+ public static String getWeek() {
|
|
|
+ return formatDate(new Date(), "E");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期型字符串转化为日期 格式
|
|
|
+ * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
|
|
|
+ * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
|
|
|
+ * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
|
|
|
+ */
|
|
|
+ public static Date parseDate(Object str) {
|
|
|
+ if (str == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return parseDate(str.toString(), parsePatterns);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去的天数
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long pastDays(Date date) {
|
|
|
+ long t = new Date().getTime()-date.getTime();
|
|
|
+ return t/(24*60*60*1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去的小时
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long pastHour(Date date) {
|
|
|
+ long t = new Date().getTime()-date.getTime();
|
|
|
+ return t/(60*60*1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取过去的分钟
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long pastMinutes(Date date) {
|
|
|
+ long t = new Date().getTime()-date.getTime();
|
|
|
+ return t/(60*1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为时间(天,时:分:秒.毫秒)
|
|
|
+ * @param timeMillis
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String formatDateTime(long timeMillis){
|
|
|
+ long day = timeMillis/(24*60*60*1000);
|
|
|
+ long hour = (timeMillis/(60*60*1000)-day*24);
|
|
|
+ long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
|
|
|
+ long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
|
|
|
+ long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
|
|
|
+ return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个日期之间的天数
|
|
|
+ *
|
|
|
+ * @param before
|
|
|
+ * @param after
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static double getDistanceOfTwoDate(Date before, Date after) {
|
|
|
+ long beforeTime = before.getTime();
|
|
|
+ long afterTime = after.getTime();
|
|
|
+ return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个月的天数
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getDaysOfMonth(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在日期上增加天数
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期
|
|
|
+ * @param n
|
|
|
+ * 要增加的天数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addDay(Date date, int n) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.DATE, n);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * @param args
|
|
|
+// * @throws ParseException
|
|
|
+// */
|
|
|
+// public static void main(String[] args) throws ParseException {
|
|
|
+// System.out.println(formatDate(parseDate("2010/3/6")));
|
|
|
+// System.out.println(getDate("yyyy年MM月dd日 E"));
|
|
|
+// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
|
|
|
+// System.out.println(time/(24*60*60*1000));
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 英文全称 如:2010-12-01 23:15:06
|
|
|
+ */
|
|
|
+ public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ /**
|
|
|
+ * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
|
|
|
+ */
|
|
|
+ public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
|
|
|
+ /**
|
|
|
+ * 中文简写 如:2010年12月01日
|
|
|
+ */
|
|
|
+ public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
|
|
|
+ /**
|
|
|
+ * 中文全称 如:2010年12月01日 23时15分06秒
|
|
|
+ */
|
|
|
+ public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
|
|
|
+ /**
|
|
|
+ * 精确到毫秒的完整中文时间
|
|
|
+ */
|
|
|
+ public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得默认的 date pattern
|
|
|
+ */
|
|
|
+ public static String getDatePattern() {
|
|
|
+ return FORMAT_LONG;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据预设格式返回当前日期
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNow() {
|
|
|
+ return format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户格式返回当前日期
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNow(String format) {
|
|
|
+ return format(new Date(), format);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用预设格式格式化日期
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(Date date) {
|
|
|
+ return format(date, getDatePattern());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用用户格式格式化日期
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期
|
|
|
+ * @param pattern
|
|
|
+ * 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String format(Date date, String pattern) {
|
|
|
+ String returnValue = "";
|
|
|
+ if (date != null) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
|
+ returnValue = df.format(date);
|
|
|
+ }
|
|
|
+ return (returnValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用预设格式提取字符串日期
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * 日期字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date parse(String strDate) {
|
|
|
+ return parse(strDate, getDatePattern());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用用户格式提取字符串日期
|
|
|
+ *
|
|
|
+ * @param strDate
|
|
|
+ * 日期字符串
|
|
|
+ * @param pattern
|
|
|
+ * 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date parse(String strDate, String pattern) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat(pattern);
|
|
|
+ try {
|
|
|
+ return df.parse(strDate);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在日期上增加数个整月
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期
|
|
|
+ * @param n
|
|
|
+ * 要增加的月数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addMonth(Date date, int n) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.MONTH, n);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在日期上增加描述
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期
|
|
|
+ * @param n
|
|
|
+ * 要增加的天数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addSecond(Date date, int n) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.SECOND, n);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间戳
|
|
|
+ */
|
|
|
+ public static String getTimeString() {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ return df.format(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日期年份
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getYear(Date date) {
|
|
|
+ return format(date).substring(0, 4);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按默认格式的字符串距离今天的天数
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期字符串
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int countDays(String date) {
|
|
|
+ long t = Calendar.getInstance().getTime().getTime();
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(parse(date));
|
|
|
+ long t1 = c.getTime().getTime();
|
|
|
+ return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按用户格式字符串距离今天的天数(超过24小时才可以使用)
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * 日期字符串
|
|
|
+ * @param format
|
|
|
+ * 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int countDays(String date, String format) {
|
|
|
+ long t = Calendar.getInstance().getTime().getTime();
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(parse(date, format));
|
|
|
+ long t1 = c.getTime().getTime();
|
|
|
+ return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String firstDayOfMonth(Date date){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, calendar
|
|
|
+ .getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.SECOND,0);
|
|
|
+ calendar.set(Calendar.MINUTE,0);
|
|
|
+ return DateUtils.format(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String lastDayOfMonth(Date date){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, calendar
|
|
|
+ .getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.SECOND,59);
|
|
|
+ calendar.set(Calendar.MINUTE,59);
|
|
|
+ return DateUtils.format(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int currentMonth(){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ return calendar.get(Calendar.MONTH)+1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int currentYear(){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ return calendar.get(Calendar.YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int currentMonthLastDay(){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int LastDayOfMonth(Date date){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ return calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getDay4Date(Date date){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ return calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean afterNow(Date date){
|
|
|
+ return new Date().before(date);
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// *
|
|
|
+// * @param agrs
|
|
|
+// * @throws ParseException
|
|
|
+// */
|
|
|
+// public static void main(String[] agrs) throws ParseException {
|
|
|
+// //System.out.println(DateUtils.format(DateUtils.parse("2015-5-19 10:49:29"), "yyyy年MM月"));
|
|
|
+// System.out.println(DateUtils.firstDayOfMonth(parse("2015-02-01",FORMAT_SHORT)));
|
|
|
+// System.out.println(DateUtils.lastDayOfMonth(parse("2015-02-01",FORMAT_SHORT)));
|
|
|
+// System.out.println(currentYear());
|
|
|
+// System.out.println(currentMonthLastDay());
|
|
|
+// }
|
|
|
+
|
|
|
+ // 获取上周的开始时间
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public static Date getBeginDayOfLastWeek() {
|
|
|
+ Date date = new Date();
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ 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 - 7);
|
|
|
+ return getDayStartTime(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取上周的结束时间
|
|
|
+ public static Date getEndDayOfLastWeek() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(getBeginDayOfLastWeek());
|
|
|
+ cal.add(Calendar.DAY_OF_WEEK, 6);
|
|
|
+ Date weekEndSta = cal.getTime();
|
|
|
+ return getDayEndTime(weekEndSta);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取某个日期的开始时间
|
|
|
+ public static Timestamp getDayStartTime(Date d) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ if (null != d) {
|
|
|
+ calendar.setTime(d);
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取某个日期的结束时间
|
|
|
+ public static Timestamp getDayEndTime(Date d) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ if (null != d) {
|
|
|
+ calendar.setTime(d);
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取上月的开始时间
|
|
|
+ public static Date getBeginDayOfLastMonth() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(getNowYear(), getNowMonth() - 2, 1);
|
|
|
+ return getDayStartTime(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取上月的结束时间
|
|
|
+ public static Date getEndDayOfLastMonth() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(getNowYear(), getNowMonth() - 2, 1);
|
|
|
+ int day = calendar.getActualMaximum(5);
|
|
|
+ calendar.set(getNowYear(), getNowMonth() - 2, day);
|
|
|
+ return getDayEndTime(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取今年是哪一年
|
|
|
+ public static Integer getNowYear() {
|
|
|
+ Date date = new Date();
|
|
|
+ GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
|
|
|
+ gc.setTime(date);
|
|
|
+ return Integer.valueOf(gc.get(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取本月是哪一月
|
|
|
+ public static int getNowMonth() {
|
|
|
+ Date date = new Date();
|
|
|
+ GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
|
|
|
+ gc.setTime(date);
|
|
|
+ return gc.get(2) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取昨天的开始时间
|
|
|
+ public static Date getBeginDayOfYesterday() {
|
|
|
+ Calendar cal = new GregorianCalendar();
|
|
|
+ cal.setTime(getDayBegin());
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取昨天的结束时间
|
|
|
+ public static Date getEndDayOfYesterDay() {
|
|
|
+ Calendar cal = new GregorianCalendar();
|
|
|
+ cal.setTime(getDayEnd());
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当天的开始时间
|
|
|
+ public static Date getDayBegin() {
|
|
|
+ Calendar cal = new GregorianCalendar();
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当天的结束时间
|
|
|
+ public static Date getDayEnd() {
|
|
|
+ Calendar cal = new GregorianCalendar();
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ cal.set(Calendar.MINUTE, 59);
|
|
|
+ cal.set(Calendar.SECOND, 59);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Author ye.qin
|
|
|
+ * @Description //TODO 返回两个时间字符串的时间差(秒)
|
|
|
+ * @Date 2018\12\29 0029 15:56
|
|
|
+ * @Param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int betweenDate(String endTime,String startTime,String YMDHMS){
|
|
|
+ Date time1 = DateUtils.parse(endTime,YMDHMS);
|
|
|
+ Date time2 = DateUtils.parse(startTime,YMDHMS);
|
|
|
+ int a = (int) (time1.getTime() / 1000);
|
|
|
+ int b = (int) (time2.getTime() / 1000);
|
|
|
+ System.out.print(a-b);
|
|
|
+ return a-b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Author ye.qin
|
|
|
+ * @Description //TODO 获取指定格式的时间
|
|
|
+ * @Date 2018\12\29 0029 16:26
|
|
|
+ * @Param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTargetTime(String time,String YMDHMS,String targetFormat){
|
|
|
+ Date a = parse(time,YMDHMS);
|
|
|
+ String b = format(a,targetFormat);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * date2比date1多的天数
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int differentDays(String date)
|
|
|
+ {
|
|
|
+ Calendar cal1 = Calendar.getInstance();
|
|
|
+ cal1.setTime(parseDate(date));
|
|
|
+
|
|
|
+ Calendar cal2 = Calendar.getInstance();
|
|
|
+ cal2.setTime(new Date());
|
|
|
+ int day1= cal1.get(Calendar.DAY_OF_YEAR);
|
|
|
+ int day2 = cal2.get(Calendar.DAY_OF_YEAR);
|
|
|
+
|
|
|
+ int year1 = cal1.get(Calendar.YEAR);
|
|
|
+ int year2 = cal2.get(Calendar.YEAR);
|
|
|
+ if(year1 != year2)
|
|
|
+ {
|
|
|
+ int timeDistance = 0 ;
|
|
|
+ for(int i = year1 ; i < year2 ; i ++)
|
|
|
+ {
|
|
|
+ if(i%4==0 && i%100!=0 || i%400==0) //闰年
|
|
|
+ {
|
|
|
+ timeDistance += 366;
|
|
|
+ }
|
|
|
+ else //不是闰年
|
|
|
+ {
|
|
|
+ timeDistance += 365;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return timeDistance + (day2-day1) ;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return day2-day1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Author ye.qin
|
|
|
+ * @Description //TODO 格式化文章时间
|
|
|
+ * @Date 2019\1\14 0014 15:48
|
|
|
+ * @Param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getArticleTime(String deployTime, String YMDHMS) {
|
|
|
+ final String TARGETTIME = "yyyy年MM月dd日";
|
|
|
+ final String HOURMINUTE = "HH:mm";
|
|
|
+ String time1 = "";
|
|
|
+ String currentTime = DateUtils.format(new Date(),YMDHMS);
|
|
|
+ int minuteFlag = DateUtils.betweenDate(currentTime,deployTime,YMDHMS);
|
|
|
+ int dayFlag = DateUtils.differentDays(deployTime);
|
|
|
+ if(minuteFlag > 0){
|
|
|
+ if(minuteFlag < 3*60){
|
|
|
+ time1 = "刚刚";
|
|
|
+ }else if(minuteFlag < 5*60){
|
|
|
+ time1= "3分钟前";
|
|
|
+ }else if(minuteFlag < 10*60){
|
|
|
+ time1 = "5分钟前";
|
|
|
+ }else if(minuteFlag < 20*60){
|
|
|
+ time1 = "10分钟前";
|
|
|
+ }else if(minuteFlag < 30*60){
|
|
|
+ time1 = "20分钟前";
|
|
|
+ }else if(minuteFlag < 60*60){
|
|
|
+ time1 = "30分钟前";
|
|
|
+ }else if(dayFlag == 0){
|
|
|
+ time1 = "今天 "+DateUtils.getTargetTime(deployTime,YMDHMS,HOURMINUTE);
|
|
|
+ }else if(dayFlag == 1){
|
|
|
+ time1 = "昨天 "+DateUtils.getTargetTime(deployTime,YMDHMS,HOURMINUTE);
|
|
|
+ }else{
|
|
|
+ time1 = DateUtils.getTargetTime(deployTime,YMDHMS,TARGETTIME);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return time1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDatePoor(Date endDate, Date nowDate) {
|
|
|
+
|
|
|
+ long nd = 1000 * 24 * 60 * 60;
|
|
|
+ long nh = 1000 * 60 * 60;
|
|
|
+ long nm = 1000 * 60;
|
|
|
+ // long ns = 1000;
|
|
|
+ // 获得两个时间的毫秒时间差异
|
|
|
+ long diff = endDate.getTime() - nowDate.getTime();
|
|
|
+ // 计算差多少天
|
|
|
+ long day = diff / nd;
|
|
|
+ // 计算差多少小时
|
|
|
+ long hour = diff % nd / nh;
|
|
|
+ // 计算差多少分钟
|
|
|
+ long min = diff % nd % nh / nm;
|
|
|
+ // 计算差多少秒//输出结果
|
|
|
+ // long sec = diff % nd % nh % nm / ns;
|
|
|
+ return day + "天" + hour + "小时" + min + "分钟";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getHourPoor(Date endDate, Date nowDate) {
|
|
|
+
|
|
|
+
|
|
|
+ long nh = 1000 * 60 * 60;
|
|
|
+ long nm = 1000 * 60;
|
|
|
+ // long ns = 1000;
|
|
|
+ // 获得两个时间的毫秒时间差异
|
|
|
+ long diff = endDate.getTime() - nowDate.getTime();
|
|
|
+
|
|
|
+ // 计算差多少小时
|
|
|
+ long hour = diff / nh;
|
|
|
+ // 计算差多少分钟
|
|
|
+ long min = diff % nh / nm;
|
|
|
+ // 计算差多少秒//输出结果
|
|
|
+ // long sec = diff % nd % nh % nm / ns;
|
|
|
+ return hour + "小时" + min + "分钟";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getGroupEndTime(Date endTime,String YMDHMS) {
|
|
|
+ final String HOURMINUTE = "HH:mm";
|
|
|
+ String time = formatDate(endTime,YMDHMS);
|
|
|
+ String time1 = "";
|
|
|
+ String currentTime = DateUtils.format(new Date(),YMDHMS);
|
|
|
+ int minuteFlag = DateUtils.betweenDate(time,currentTime,YMDHMS);
|
|
|
+ int dayFlag = DateUtils.differentDays(time);
|
|
|
+ if(minuteFlag > 0){
|
|
|
+
|
|
|
+ if(minuteFlag < 60*60*24*3){
|
|
|
+ time1 = getHourPoor(endTime,new Date());
|
|
|
+ }else{
|
|
|
+ time1 = "剩余"+(-dayFlag)+"天";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return time1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param nowTime 当前时间
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @return
|
|
|
+ * @author sunran 判断当前时间在时间区间内
|
|
|
+ */
|
|
|
+ public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
|
|
|
+ if (nowTime.getTime() == startTime.getTime()
|
|
|
+ || nowTime.getTime() == endTime.getTime()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ Calendar date = Calendar.getInstance();
|
|
|
+ date.setTime(nowTime);
|
|
|
+
|
|
|
+ Calendar begin = Calendar.getInstance();
|
|
|
+ begin.setTime(startTime);
|
|
|
+
|
|
|
+ Calendar end = Calendar.getInstance();
|
|
|
+ end.setTime(endTime);
|
|
|
+
|
|
|
+ if (date.after(begin) && date.before(end)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|