|
@@ -0,0 +1,602 @@
|
|
|
+package com.xc.utils;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.lang.management.ManagementFactory;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 时间工具类
|
|
|
+ *2020-12-14
|
|
|
+ * @author qlm
|
|
|
+ */
|
|
|
+public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
|
|
+ private static Logger LOG = LoggerFactory.getLogger(DateUtils.class);
|
|
|
+
|
|
|
+ public static String YYYY = "yyyy";
|
|
|
+
|
|
|
+ public static String YYYY_MM = "yyyy-MM";
|
|
|
+
|
|
|
+ public static String YYYY_MM_DD = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ public static String HH_MM_SS = "HH:mm:ss";
|
|
|
+
|
|
|
+ public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
|
|
+ public static String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
|
|
|
+
|
|
|
+ public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ public static String YYYY_MM_DD_HH_MM_SS_sss = "yyyy-MM-dd HH:mm:ss.SSS";
|
|
|
+ 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"};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前Date型日期
|
|
|
+ *
|
|
|
+ * @return Date() 当前日期
|
|
|
+ */
|
|
|
+ public static Date getNowDate() {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断当前时间是否在时间段内
|
|
|
+ *
|
|
|
+ * @param startTime 开始 20:20
|
|
|
+ * @param endTime 结束 21:50
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean belongCalendar(String startTime, String endTime) {
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
|
|
|
+ Date now = null;
|
|
|
+ Date beginTimeDate = null;
|
|
|
+ Date endTimeDate = null;
|
|
|
+ try {
|
|
|
+ now = df.parse(df.format(new Date()));
|
|
|
+ beginTimeDate = df.parse(startTime);
|
|
|
+ endTimeDate = df.parse(endTime);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if(e.toString().length()>500){ LOG.error(e.toString().substring(0,450)); }else { LOG.error(e.toString()); }
|
|
|
+ }
|
|
|
+ return belongCalendar(now, beginTimeDate, endTimeDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断时间是否在时间段内
|
|
|
+ *
|
|
|
+ * @param nowTime 目标时间
|
|
|
+ * @param beginTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
|
|
|
+ Calendar date = Calendar.getInstance();
|
|
|
+ date.setTime(nowTime);
|
|
|
+
|
|
|
+ Calendar begin = Calendar.getInstance();
|
|
|
+ begin.setTime(beginTime);
|
|
|
+
|
|
|
+ Calendar end = Calendar.getInstance();
|
|
|
+ end.setTime(endTime);
|
|
|
+
|
|
|
+ if (date.after(begin) && date.before(end)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算两个时间差
|
|
|
+ */
|
|
|
+ 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 getDatePoorDay(String endDate, String nowDate) {
|
|
|
+ long nd = 1000 * 24 * 60 * 60;
|
|
|
+ long nh = 1000 * 60 * 60;
|
|
|
+ long nm = 1000 * 60;
|
|
|
+ // long ns = 1000;
|
|
|
+ // 获得两个时间的毫秒时间差异
|
|
|
+ long diff = formatToDate(endDate).getTime() - formatToDate(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 +"";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前日期, 默认格式为yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public static String getDate() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getHhMmSs() {
|
|
|
+ return dateTimeNow(HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrentTime() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized static String getCurrentDay() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getTime() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTimeNow() {
|
|
|
+ return dateTimeNow(YYYYMMDDHHMMSS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getTimeSSS() {
|
|
|
+ return dateTimeNow(YYYYMMDDHHMMSSSSS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTimeNow_YYYY_MM_DD_HH_MM_SS() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getCurrentTimeSSS() {
|
|
|
+ return dateTimeNow(YYYY_MM_DD_HH_MM_SS_sss);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTimeNow(final String format) {
|
|
|
+ return parseDateToStr(format, new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String dateTime(final Date date) {
|
|
|
+ return parseDateToStr(YYYY_MM_DD, date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String parseDateToStr(final String format, final Date date) {
|
|
|
+ return new SimpleDateFormat(format).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final Date dateTime(final String format, final String ts) {
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(format).parse(ts);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取服务器启动时间
|
|
|
+ */
|
|
|
+ public static Date getServerStartDate() {
|
|
|
+ long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
|
|
+ return new Date(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期型字符串转化为日期 格式
|
|
|
+ */
|
|
|
+ public static Date parseDate(Object str) {
|
|
|
+ if (str == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return parseDate(str.toString(), parsePatterns);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date strToDate(String dateStr) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ Date date = sDateFormat.parse(dateStr);
|
|
|
+ return date;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ if(e.toString().length()>500){ LOG.error(e.toString().substring(0,450)); }else { LOG.error(e.toString()); }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ //时间戳转时间
|
|
|
+ public static String stampToDate(String s, String format) throws Exception {
|
|
|
+ String res;
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
|
|
|
+ long lt = new Long(s);
|
|
|
+ Date date = new Date(lt);
|
|
|
+ res = simpleDateFormat.format(date);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取今日凌晨时间戳
|
|
|
+ public static long getEarlyThisMorningStamp() {
|
|
|
+ long nowTime = System.currentTimeMillis();
|
|
|
+ long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24);
|
|
|
+ return todayStartTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期路径 即年/月/日 如2018/08/08
|
|
|
+ */
|
|
|
+ public static final String datePath() {
|
|
|
+ Date now = new Date();
|
|
|
+ return DateFormatUtils.format(now, "yyyy/MM/dd");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期路径 即年/月/日 如20180808
|
|
|
+ */
|
|
|
+ public static final String dateTime() {
|
|
|
+ Date now = new Date();
|
|
|
+ return DateFormatUtils.format(now, "yyyyMMdd");
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 增加N天 ,可以是负数
|
|
|
+ * @param date
|
|
|
+ * @param dayNum
|
|
|
+ * @param formatStr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String addDay(Date date, int dayNum, String formatStr, int type) {
|
|
|
+ if (StringUtils.isBlank(formatStr)) {
|
|
|
+ formatStr = YYYY_MM_DD_HH_MM_SS;
|
|
|
+ }
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(formatStr);
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ c.add(type, dayNum);
|
|
|
+ Date m = c.getTime();
|
|
|
+ String mon = format.format(m);
|
|
|
+ return mon;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加N天 ,可以是负数
|
|
|
+ *
|
|
|
+ * @param date 字符串时间
|
|
|
+ * @param dayNum 天数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String addDay(String date, int dayNum) {
|
|
|
+ return addDay(formatToDateTime(date), dayNum, null, Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加n天
|
|
|
+ *
|
|
|
+ * @param dayNum
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String addDay(int dayNum) {
|
|
|
+ return addDay(new Date(), dayNum, null, Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 增加N月
|
|
|
+ * @param month
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String addMonth(int month) {
|
|
|
+ return addDay(new Date(), month, null, Calendar.MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 增加N年
|
|
|
+ * @param year
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String addYear(int year) {
|
|
|
+ return addDay(new Date(), year, null, Calendar.YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据格式化字符串 把时间进行格式化
|
|
|
+ *
|
|
|
+ * @param formatStr
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public synchronized static String formatDate(String formatStr, Date date) {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(formatStr);
|
|
|
+ String dateStr = format.format(date);
|
|
|
+ return dateStr;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public synchronized static Date formatToDate(String time) {
|
|
|
+ //规范时间格式
|
|
|
+ SimpleDateFormat pubTimeormatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ Date pubTimedate = null;
|
|
|
+ try {
|
|
|
+ pubTimedate = pubTimeormatter.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ pubTimeormatter = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ try {
|
|
|
+ pubTimedate = pubTimeormatter.parse(time);
|
|
|
+ } catch (ParseException e1) {
|
|
|
+ pubTimeormatter = new SimpleDateFormat(HH_MM_SS);
|
|
|
+ try {
|
|
|
+ pubTimedate = pubTimeormatter.parse(time);
|
|
|
+ } catch (ParseException e3) {
|
|
|
+ e3.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return pubTimedate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized static Date formatToDateTime(String time) {
|
|
|
+ //规范时间格式
|
|
|
+ SimpleDateFormat pubTimeormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date pubTimedate = null;
|
|
|
+ try {
|
|
|
+ pubTimedate = pubTimeormatter.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ if(e.toString().length()>500){ LOG.error(e.toString().substring(0,450)); }else { LOG.error(e.toString()); }
|
|
|
+ }
|
|
|
+ return pubTimedate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized static String addDaysWithHms(int days) {
|
|
|
+ return addDay(new Date(), days, null, Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized static int differentDays(Date date1, Date date2) {
|
|
|
+ Calendar cal1 = Calendar.getInstance();
|
|
|
+ cal1.setTime(date1);
|
|
|
+
|
|
|
+ Calendar cal2 = Calendar.getInstance();
|
|
|
+ cal2.setTime(date2);
|
|
|
+ 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 //不同年
|
|
|
+ {
|
|
|
+ System.out.println("判断day2 - day1 : " + (day2 - day1));
|
|
|
+ return day2 - day1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断当前时间是否在时间date2之前
|
|
|
+ //时间格式 2005-4-21 16:16:34
|
|
|
+ public static boolean isDateBefore(String date2) {
|
|
|
+ try {
|
|
|
+ Date date1 = new Date();
|
|
|
+ DateFormat df = DateFormat.getDateTimeInstance();
|
|
|
+ return date1.before(df.parse(date2 + " 00:00:00"));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ System.out.print("[SYS] " + e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2019-07-10 新增
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取任意时间的下一个月的固定天
|
|
|
+ * @param repeatDate // 时间
|
|
|
+ * @param format 传递过来的时间格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getPreMonthDay(String repeatDate,String format) {
|
|
|
+ Date repeatDateD = dateTime(format,repeatDate);
|
|
|
+ repeatDate = formatDate("yyyyMMdd",repeatDateD);
|
|
|
+ String lastMonth = "";
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ SimpleDateFormat dft = new SimpleDateFormat(format);
|
|
|
+ int year = Integer.parseInt(repeatDate.substring(0, 4));
|
|
|
+ String monthsString = repeatDate.substring(4, 6);
|
|
|
+ int month;
|
|
|
+ if ("0".equals(monthsString.substring(0, 1))) {
|
|
|
+ month = Integer.parseInt(monthsString.substring(1, 2));
|
|
|
+ } else {
|
|
|
+ month = Integer.parseInt(monthsString.substring(0, 2));
|
|
|
+ }
|
|
|
+ int day = Integer.parseInt(repeatDate.substring(6, 8));
|
|
|
+
|
|
|
+ cal.set(year,month,day);
|
|
|
+ lastMonth = dft.format(cal.getTime());
|
|
|
+ return lastMonth;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
|
|
|
+ *
|
|
|
+ * @param nowTime 当前时间
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @return
|
|
|
+ * @author jqlin
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isToday(String date) {
|
|
|
+ SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ if (date.equals(fmt.format(new Date()).toString())) { //格式化为相同格式
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取指定时间的时间戳
|
|
|
+ public static String timeInMillis(String str){
|
|
|
+ Date date = formatToDateTime(str);
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ long timestamp = cal.getTimeInMillis();
|
|
|
+ return timestamp +"";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 获取当天0点时间戳
|
|
|
+ public static Long dayTimeInMillis() {
|
|
|
+ Calendar calendar = Calendar.getInstance();// 获取当前日期
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ Long time = calendar.getTimeInMillis()/1000;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当天0点时间戳
|
|
|
+ public static Long dayTimeInMillis(String oldtime) {
|
|
|
+ Calendar calendar = Calendar.getInstance();// 获取当前日期
|
|
|
+ calendar.setTime(formatToDate(oldtime));
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ Long time = calendar.getTimeInMillis()/1000;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当周0点时间戳
|
|
|
+ public static Long weekTimeInMillis() {
|
|
|
+ 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_WEEK, Calendar.MONDAY);
|
|
|
+ return (cal.getTimeInMillis()/1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取指定周0点时间戳
|
|
|
+ public static Long weekTimeInMillis(String oldtime) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(formatToDate(oldtime));
|
|
|
+ cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ return (cal.getTimeInMillis()/1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当月0点时间戳
|
|
|
+ public static Long monthTimeInMillis() {
|
|
|
+ Calendar calendar = Calendar.getInstance();// 获取当前日期
|
|
|
+ calendar.add(Calendar.YEAR, 0);
|
|
|
+ calendar.add(Calendar.MONTH, 0);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ Long time = calendar.getTimeInMillis()/1000;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当月0点时间戳
|
|
|
+ public static Long monthTimeInMillis(String oldtime) {
|
|
|
+ Calendar calendar = Calendar.getInstance();// 获取当前日期
|
|
|
+ calendar.setTime(formatToDate(oldtime));
|
|
|
+ calendar.add(Calendar.YEAR, 0);
|
|
|
+ calendar.add(Calendar.MONTH, 0);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ Long time = calendar.getTimeInMillis()/1000;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+ // 获取当年0点时间戳
|
|
|
+ public static Long yearTimeInMillis() {
|
|
|
+ Calendar calendar = Calendar.getInstance();// 获取当前日期
|
|
|
+ calendar.add(Calendar.YEAR, 0);
|
|
|
+ calendar.add(Calendar.DATE, 0);
|
|
|
+ calendar.add(Calendar.MONTH, 0);
|
|
|
+ calendar.set(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ calendar.set(Calendar.HOUR, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ Long time = calendar.getTimeInMillis()/1000;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取当年0点时间戳
|
|
|
+ public static Long yearTimeInMillis(String oldtime) {
|
|
|
+ Calendar calendar = Calendar.getInstance();// 获取当前日期
|
|
|
+ calendar.setTime(formatToDate(oldtime));
|
|
|
+ calendar.add(Calendar.YEAR, 0);
|
|
|
+ calendar.add(Calendar.DATE, 0);
|
|
|
+ calendar.add(Calendar.MONTH, 0);
|
|
|
+ calendar.set(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ calendar.set(Calendar.HOUR, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ Long time = calendar.getTimeInMillis()/1000;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+}
|