最好用的时间工具类

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.collections4.SplitMapUtils;

import javax.xml.bind.SchemaOutputResolver;

/**
 * date: 2018年12月13日 下午6:55:00 author: lk
 **/
public class DateUtil {
   static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 要转换的时间格式

   public static String timeStamp2Date(Long timeLong) {

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 要转换的时间格式
      Date date;
      try {
         date = sdf.parse(sdf.format(timeLong));
         return sdf.format(date);
      } catch (ParseException e) {
         e.printStackTrace();
         return null;
      }
   }
   /**
    *
    * @param firstLoginTime 最小的时间
    * @param nowTime 传递最大的时间
    * @return
    */

   public static JSONObject getDistanceTime(String firstLoginTime, String nowTime) {
      JSONObject dataMap = new JSONObject();
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date d1;
      try {
         d1 = df.parse(nowTime);
         Date d2 = df.parse(firstLoginTime);// 用户初次登录时间
         long diff = d1.getTime() - d2.getTime(); 当前的时间减去我初次登陆的时间如果大于等于2小时
         long seconds = diff / (1000); // 共计秒数
         long days = diff / (1000 * 60 * 60 * 24);
         long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
         long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
         dataMap.put("hours", hours);
         dataMap.put("minutes", minutes);
         dataMap.put("seconds", seconds);
         dataMap.put("days", days);

         return dataMap;
      } catch (ParseException e) {
      } // 当前时间
      return dataMap;

   }

   /**
	 * 当前时间家加几分钟或小时
	 * @param minute
	 * @return
	 */
	public static String  subNowTime(Long minute) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		System.out.println("当前时间:" + sdf.format(now));
		Date afterDate = new Date(now .getTime() -minute*100000);
		String time = sdf.format(afterDate);
		return time;

	}
	/**
	 * 当前时间家加几分钟或小时
	 * @param
	 * @return
	 */
	public static String addNowTime (Long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		System.out.println("当前时间:" + sdf.format(now));
		Date afterDate = new Date(now .getTime() +time*100000);
		String times = sdf.format(afterDate);
		return times;

	}

   public static  String  getBeforeDay()
   {

      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
      Date date=new Date();
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, -2);
      date = calendar.getTime();
      return  sdf.format(date);

   }
   public static int compare_date(String DATE1, String DATE2) {

      DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      try {
         Date dt1 = df.parse(DATE1);
         Date dt2 = df.parse(DATE2);
         if (dt1.getTime() > dt2.getTime()) {
            System.out.println(DATE1 + " 在" + DATE2 + "前");
            return 1;
         } else if (dt1.getTime() < dt2.getTime()) {

            System.out.println("" + DATE1 + "在" + DATE2 + "后");
            return -1;
         } else {
            return 0;
         }
      } catch (Exception exception) {
         exception.printStackTrace();
      }
      return 0;
   }


   public static List<Date> findDates(Date dBegin, Date dEnd) {
      List lDate = new ArrayList();
      lDate.add(dBegin);
      Calendar calBegin = Calendar.getInstance();
      // 使用给定的 Date 设置此 Calendar 的时间
      calBegin.setTime(dBegin);
      Calendar calEnd = Calendar.getInstance();
      // 使用给定的 Date 设置此 Calendar 的时间
      calEnd.setTime(dEnd);
      // 测试此日期是否在指定日期之后
      while (dEnd.after(calBegin.getTime())) {
         // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
         calBegin.add(Calendar.DAY_OF_MONTH, 1);
         lDate.add(calBegin.getTime());
      }
      return lDate;
   }

   /**
    * 这里不要随便删除 可能其他地方有用到
    *
    * @param dateStr
    * @return
    */
   public static Timestamp StringTimeStamp(String dateStr) {

//    dateStr = dateStr + " 11:49:45";

      Timestamp ts = new Timestamp(System.currentTimeMillis());
//    String tsStr = "2011-05-09 11:49:45";
      try {
         ts = Timestamp.valueOf(dateStr);
         return ts;

      } catch (Exception e) {
         e.printStackTrace();
      }
      return ts;
   }

   public static Timestamp StringTimeStamps(String dateStr) {

      Timestamp ts = new Timestamp(System.currentTimeMillis());
      try {
         ts = Timestamp.valueOf(dateStr);
         return ts;

      } catch (Exception e) {
         e.printStackTrace();
      }
      return ts;
   }

   public static String getNowDate() {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
      String nowDate = df.format(new Date());

      return nowDate;
   }

   public static String getNowTime() {
      DateFormat dateTimeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String strBeginDate = dateTimeformat.format(new Date());
      return strBeginDate;
   }

   public static Timestamp getNowTimes(String nowTime) {
      Timestamp ts = new Timestamp(System.currentTimeMillis());
      try {
         ts = Timestamp.valueOf(nowTime);
      } catch (Exception e) {
         e.printStackTrace();
      }

      return ts;
   }

   public static String getTimeInterval(Date date) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
      int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
      if (1 == dayWeek) {
         cal.add(Calendar.DAY_OF_MONTH, -1);
      }
      // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
      // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
      cal.setFirstDayOfWeek(Calendar.MONDAY);
      // 获得当前日期是一个星期的第几天
      int day = cal.get(Calendar.DAY_OF_WEEK);
      // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
      cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
      String imptimeBegin = sdf.format(cal.getTime());
      // System.out.println("所在周星期一的日期:" + imptimeBegin);
      cal.add(Calendar.DATE, 6);
      String imptimeEnd = sdf.format(cal.getTime());
      // System.out.println("所在周星期日的日期:" + imptimeEnd);
      return imptimeBegin + "," + imptimeEnd;
   }

   public static String getYesterDay() {
      Date date = new Date();
      // Calendar calendar =new GregorianCalendar();
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(calendar.DATE, -1);
      date = calendar.getTime();
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      String dateString = format.format(date);
      // System.out.println(dateString);
      return dateString;
   }

   public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate) throws Exception{

      SimpleDateFormat format = new SimpleDateFormat("HH:mm");

      Date now = format.parse(nowDate);
      Date start = format.parse(startDate);
      Date end = format.parse(endDate);

      long nowTime = now.getTime();
      long startTime = start.getTime();
      long endTime = end.getTime();
      return nowTime >= startTime && nowTime <= endTime;
   }

   /**
    * 时间戳转换成日期格式字符串
    *
    * @param seconds   精确到秒的字符串
    * @param format Str
    * @return
    */
   public static String timeStamp2Date(String seconds, String format) {
      if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
         return "";
      }
      if (format == null || format.isEmpty()) {
         format = "yyyy-MM-dd HH:mm:ss";
      }
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      return sdf.format(new Date(Long.valueOf(seconds + "000")));
   }

   /**
    * 日期格式字符串转换成时间戳
    *
    * @param date_str   字符串日期
    * @param format 如:yyyy-MM-dd HH:mm:ss
    * @return
    */
   public static String date2TimeStamp(String date_str, String format) {
      try {
         SimpleDateFormat sdf = new SimpleDateFormat(format);
         return String.valueOf(sdf.parse(date_str).getTime() / 1000);
      } catch (Exception e) {
         e.printStackTrace();
      }
      return "";
   }

   /**
    * 取得当前时间戳(精确到秒)
    *
    * @return
    */
   public static String timeStamp() {
      long time = System.currentTimeMillis();
      String t = String.valueOf(time / 1000);
      return t;
   }

   public static String timestampToStr(String tsStr) {
      Timestamp ts = new Timestamp(System.currentTimeMillis());
      DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
         // 方法一
         tsStr = sdf.format(ts);
         return tsStr;
      } catch (Exception e) {
         e.printStackTrace();
      }

      return tsStr;
   }

   public static String TimestampTostr(Timestamp time) {

      String times = "";
      DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      times = sdf.format(time);
      return times;
   }
   public static String Timestamps(Timestamp time) {

      String times = "";
      DateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
      times = sdf.format(time);
      return times;
   }
   public static String TimestampTostrYMD(Timestamp time) {

      String times = "";
      DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      times = sdf.format(time);
      return times;
   }


   public static Timestamp LongToStr(Long time) {
      Timestamp scurrtest = new Timestamp(time);
      return scurrtest;
   }



   public static String  getTomorrowDate() {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, 1);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
      String time = sdf.format(c.getTime());
      return time;
   }

   public static String  getTomorrowDateSpecial() {
      SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, 1);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
      String time = sdf.format(c.getTime());
      return time;

   }

   public static String  getYesterDayDateSpecial() {
      SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, -1);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
      String time = sdf.format(c.getTime());
      return time;

   }
   public static String  getNowDayDateSpecial() {
      SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, 0);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
      String time = sdf.format(c.getTime());
      return time;

   }
   public static Timestamp getNowTimeStamp() {
      return new Timestamp(System.currentTimeMillis());
   }

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值