|
@@ -0,0 +1,345 @@
|
|
|
|
+package com.lsw.utils;
|
|
|
|
+
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author 李书文
|
|
|
|
+ * @description 时间工具类
|
|
|
|
+ * @time 2016年8月13日
|
|
|
|
+ */
|
|
|
|
+public class TimeUtil {
|
|
|
|
+ private static final long ONE_MINUTE = 60;
|
|
|
|
+ private static final long ONE_HOUR = 3600;
|
|
|
|
+ private static final long ONE_DAY = 86400;
|
|
|
|
+ private static final long ONE_MONTH = 2592000;
|
|
|
|
+ private static final long ONE_YEAR = 31104000;
|
|
|
|
+ public static List<Map<String, Object>> list;
|
|
|
|
+ private final static ThreadLocal<SimpleDateFormat> timeFormater = new ThreadLocal<SimpleDateFormat>() {
|
|
|
|
+ @Override
|
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
|
|
|
|
+ @Override
|
|
|
|
+ protected SimpleDateFormat initialValue() {
|
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 格式化日期(精确到秒)
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getNow() {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ return format.format(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 格式化日期(精确到秒)
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getMonth() {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy");
|
|
|
|
+ return format.format(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 格式化日期(精确到秒)
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getTIME() {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
+ return format.format(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 格式化日期(精确到天)
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getDay() {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
+ return format.format(new Date());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getDayFormat() {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
|
+ return format.format(new Date());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取时分
|
|
|
|
+ public static String getHour() {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("HH:mm");
|
|
|
|
+ return format.format(System.currentTimeMillis());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static List<Map<String, Object>> printWeekdays() {
|
|
|
|
+ list = new ArrayList<Map<String, Object>>();
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ for (int i = 0; i < 7; i++) {
|
|
|
|
+ printDay(calendar);
|
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String daysToDate(String createDate, int day) {
|
|
|
|
+ String endDate = null;
|
|
|
|
+ try {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
+ Date date = format.parse(createDate);
|
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
|
+ ca.setTime(date);
|
|
|
|
+ ca.add(Calendar.DATE, day);
|
|
|
|
+ endDate = format.format(ca.getTime());
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return endDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String printDay(Calendar calendar) {
|
|
|
|
+ SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
+ SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM");
|
|
|
|
+ SimpleDateFormat dateFormat3 = new SimpleDateFormat("dd");
|
|
|
|
+ SimpleDateFormat dateFormat4 = new SimpleDateFormat("EE");
|
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
+ map.put("year", dateFormat1.format(calendar.getTime()));
|
|
|
|
+ map.put("month", dateFormat2.format(calendar.getTime()).replaceAll("^(0+)", "") + "月");
|
|
|
|
+ map.put("day", dateFormat3.format(calendar.getTime()));
|
|
|
|
+ map.put("week", dateFormat4.format(calendar.getTime()).replaceAll("^(星期+)", ""));
|
|
|
|
+ list.add(map);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将日期格式转换成yyyy-MM-dd的字符串格式 返回值如:2016-8-13
|
|
|
|
+ *
|
|
|
|
+ * @param time 要转换的日期
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static <T> T dateToString(Date time) {
|
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // 定义将日期格式要换成的格式
|
|
|
|
+ String stringTime = formatter.format(time);
|
|
|
|
+ return (T) stringTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将字符串转为日期类型
|
|
|
|
+ *
|
|
|
|
+ * @param date
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static Date toDate(String date) {
|
|
|
|
+ return toDate(date, dateFormater.get());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Date toDate(String date, SimpleDateFormat dateFormater) {
|
|
|
|
+ try {
|
|
|
|
+ return dateFormater.parse(date);
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 距离今天多久
|
|
|
|
+ *
|
|
|
|
+ * @param date
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String fromToday(Date date) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+
|
|
|
|
+ long time = date.getTime() / 1000;
|
|
|
|
+ long now = new Date().getTime() / 1000;
|
|
|
|
+ long ago = now - time;
|
|
|
|
+ if (ago <= ONE_HOUR)
|
|
|
|
+ return ago / ONE_MINUTE + "分钟前";
|
|
|
|
+ else if (ago <= ONE_DAY)
|
|
|
|
+ return ago / ONE_HOUR + "小时前";
|
|
|
|
+ else if (ago <= ONE_DAY * 2)
|
|
|
|
+ return "昨天";
|
|
|
|
+ else if (ago <= ONE_DAY * 3)
|
|
|
|
+ return "前天";
|
|
|
|
+ else if (ago <= ONE_MONTH) {
|
|
|
|
+ long day = ago / ONE_DAY;
|
|
|
|
+ return day + "天前";
|
|
|
|
+ } else if (ago <= ONE_YEAR) {
|
|
|
|
+ long month = ago / ONE_MONTH;
|
|
|
|
+ long day = ago % ONE_MONTH / ONE_DAY;
|
|
|
|
+ return month + "个月前";
|
|
|
|
+ } else {
|
|
|
|
+ long year = ago / ONE_YEAR;
|
|
|
|
+ int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1
|
|
|
|
+ return year + "年前";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取时间差
|
|
|
|
+ *
|
|
|
|
+ * @param startTime 开始时间
|
|
|
|
+ * @param endTime 结束时间
|
|
|
|
+ * @param format 时间格式 例如:yyyy-MM-dd HH:mm:ss
|
|
|
|
+ * @param str 计算 d:计算天数,h:计算小时,m:计算分钟
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static Long dateDiff(String startTime, String endTime, String format, String str) {
|
|
|
|
+ // 按照传入的格式生成一个simpledateformate对象
|
|
|
|
+ SimpleDateFormat sd = new SimpleDateFormat(format);
|
|
|
|
+ long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
|
|
|
+ long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
|
|
|
+ long nm = 1000 * 60;// 一分钟的毫秒数
|
|
|
|
+ long ns = 1000;// 一秒钟的毫秒数
|
|
|
|
+ long diff;
|
|
|
|
+ long day = 0;
|
|
|
|
+ long hour = 0;
|
|
|
|
+ long min = 0;
|
|
|
|
+ long sec = 0;
|
|
|
|
+ // 获得两个时间的毫秒时间差异
|
|
|
|
+ try {
|
|
|
|
+ diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
|
|
|
|
+ day = diff / nd;// 计算差多少天
|
|
|
|
+ hour = diff % nd / nh + day * 24;// 计算差多少小时
|
|
|
|
+ min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
|
|
|
|
+ sec = diff % nd % nh % nm / ns;// 计算差多少秒
|
|
|
|
+ // 输出结果
|
|
|
|
+ System.out.println(
|
|
|
|
+ "时间相差:" + day + "天" + (hour - day * 24) + "小时" + (min - day * 24 * 60) + "分钟" + sec + "秒。");
|
|
|
|
+ System.out.println("hour=" + hour + ",min=" + min);
|
|
|
|
+ if (str.equalsIgnoreCase("d")) {
|
|
|
|
+ return day;
|
|
|
|
+ } else if (str.equalsIgnoreCase("h")) {
|
|
|
|
+ return hour;
|
|
|
|
+ } else {
|
|
|
|
+ return min;
|
|
|
|
+ }
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ if (str.equalsIgnoreCase("h")) {
|
|
|
|
+ return hour;
|
|
|
|
+ } else {
|
|
|
|
+ return min;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断给定字符串时间是否为今日
|
|
|
|
+ *
|
|
|
|
+ * @param date
|
|
|
|
+ * @return boolean
|
|
|
|
+ */
|
|
|
|
+ public static boolean isToday(String date) {
|
|
|
|
+ boolean b = false;
|
|
|
|
+ Date time = toDate(date);
|
|
|
|
+ Date today = new Date();
|
|
|
|
+ if (time != null) {
|
|
|
|
+ String nowDate = dateFormater.get().format(today);
|
|
|
|
+ String timeDate = dateFormater.get().format(time);
|
|
|
|
+ if (nowDate.equals(timeDate)) {
|
|
|
|
+ b = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return b;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取当前日期是星期几
|
|
|
|
+ *
|
|
|
|
+ * @param date
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static int getWeekOfDate(Date date) {
|
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
|
+ cal.setTime(date);
|
|
|
|
+ int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
|
+ if (w < 0)
|
|
|
|
+ w = 0;
|
|
|
|
+ return w;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断当前日期是否是本周
|
|
|
|
+ *
|
|
|
|
+ * @param date
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean isThisWeek(Date date) {
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
+ int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
+ int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
|
|
|
|
+ if (paramWeek == currentWeek) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断用户的设备时区是否为东八区(中国) 2016年8月15日
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean isInEasternEightZones() {
|
|
|
|
+ boolean defaultVaule = true;
|
|
|
|
+ if (TimeZone.getDefault() == TimeZone.getTimeZone("GMT+08"))
|
|
|
|
+ defaultVaule = true;
|
|
|
|
+ else
|
|
|
|
+ defaultVaule = false;
|
|
|
|
+ return defaultVaule;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据不同时区,转换时间 2016年8月15日
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static Date transformTime(Date date, TimeZone oldZone, TimeZone newZone) {
|
|
|
|
+ Date finalDate = null;
|
|
|
|
+ if (date != null) {
|
|
|
|
+ int timeOffset = oldZone.getOffset(date.getTime()) - newZone.getOffset(date.getTime());
|
|
|
|
+ finalDate = new Date(date.getTime() - timeOffset);
|
|
|
|
+ }
|
|
|
|
+ return finalDate;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断是否是过去的日期
|
|
|
|
+ *
|
|
|
|
+ * @param str 输入的日期
|
|
|
|
+ * @return true 早于现在,false 晚于现在
|
|
|
|
+ */
|
|
|
|
+ public static boolean isLaterThanNow(String str) {
|
|
|
|
+
|
|
|
|
+ boolean flag = false;
|
|
|
|
+ Date nowDate = new Date();
|
|
|
|
+ Date pastDate = null;
|
|
|
|
+ //格式化日期
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.CHINA);
|
|
|
|
+ //在日期字符串非空时执行
|
|
|
|
+ try {
|
|
|
|
+ //将字符串转为日期格式,如果此处字符串为非合法日期就会抛出异常。
|
|
|
|
+ pastDate = sdf.parse(str);
|
|
|
|
+ //调用Date里面的before方法来做判断
|
|
|
|
+ flag = pastDate.before(nowDate);
|
|
|
|
+ } catch (ParseException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ }
|
|
|
|
+}
|