java时间工具类

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.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 时间工具类
 * 
 * @author jacky
 *
 * 2018年5月17日 下午6:26:13
 */
public class DateUtil {

   private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
   
   public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

   public static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM");
   // 数据库使用的日期格式
   public static SimpleDateFormat dateFormatDB = new SimpleDateFormat("yyyyMMdd");

   public static SimpleDateFormat dataTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   /**
    * yyyy-MM-dd
    */
   public static final String Y_M_D = "yyyy-MM-dd";
   /**
    * MM月dd日
    */
   public static final String M_D_CN = "MM月dd日";
   /**
    * MM-dd
    */
   public static final String M_D = "MM-dd";
   /**
    * yyyy-MM-dd HH:mm
    */
   public static final String Y_M_D_HM = "yyyy-MM-dd HH:mm";
   /**
    * yyyy-MM-dd HH:mm:ss
    */
   public static final String Y_M_D_HMS = "yyyy-MM-dd HH:mm:ss";

   /**
    * yyMMdd
    */
   public static final String YMD_S = "yyMMdd";
   /**
    * yyyyMMdd
    */
   public static final String YMD = "yyyyMMdd";
   /**
    * yyMM
    */
   public static final String YM = "yyMM";
   /**
    * yyyyMMddHHmm
    */
   public static final String YMDHM = "yyyyMMddHHmm";
   /**
    * yyyyMMddHHmmss
    */
   public static final String YMDHMS = "yyyyMMddHHmmss";
   /**
    * yyyy/MM/dd
    */
   public static final String ymd = "yyyy/MM/dd";
   /**
    * yyyy/MM/dd HH:mm
    */
   public static final String ymd_HM = "yyyy/MM/dd HH:mm";
   /**
    * yyyy/MM/dd HH:mm:ss
    */
   public static final String ymd_HMS = "yyyy/MM/dd HH:mm:ss";
   /**
    * HHmmss
    */
   public static String HMSS = "HHmmss";
   /**
    * HH:mm:ss
    */
   public static String H_M_SS = "HH:mm:ss";
   /**
    * HH:mm
    */
   public static String H_M= "HH:mm";
   

   /**
    * 
    * @param dateTime
    *            yyyy-MM-dd HH:mm:ss
    * @return unix 时间
    */
   public static String getUnixTimeStamp(String dateTime) {

      Calendar c = Calendar.getInstance();
      try {
         c.setTime(dataTimeFormat.parse(dateTime));
      } catch (ParseException e) {
         e.printStackTrace();
      }

      return (c.getTimeInMillis() / 1000) + "";
   }

   public static String getNowStr() {
      return dataTimeFormat.format(new Date());
   }

   public static Date getNow() {
      return new Date();
   }

   public static String getNowStr(String dateTimeFormat) {

      return new SimpleDateFormat(dateTimeFormat).format(new Date());
   }

   /**
    * 功能:传入时间按所需格式返回时间字符串
    * 
    * @param date
    *            java.util.Date格式
    * @param format
    *            yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日 HH时mm分ss秒
    * @return
    */
   public static String format(Date date, String format) {
      String result = "";
      try {
         if (date == null) {
            date = new Date();// 如果时间为空,则默认为当前时间
         }
         if ("".equals(format) || null == format) {// 默认格式化形式
            format = "yyyy-MM-dd";
         }
         DateFormat df = new SimpleDateFormat(format);
         result = df.format(date);
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }

   /**
    * 功能:传入时间字符串按所需格式返回时间
    * 
    * @param dateStr
    *            时间字符串
    * @param format
    *            跟传入dateStr时间的格式必须一样 yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日
    *            HH时mm分ss秒
    * @return
    */
   public static Date format(String dateStr, String format) {
      if ("".equals(dateStr) || null == dateStr) {
         return new Date();
      }
      if ("".equals(format) || null == format) {
         format = "yyyy-MM-dd";
      }
      Date date = null;
      try {
         DateFormat f = new SimpleDateFormat(format);
         date = f.parse(dateStr);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      return date;

   }

   /**
    * 功能:时间字符串格式转换
    * 
    * @param dateStr
    *            时间字符串
    * @param format
    *            时间字符串的格式
    * @param toFormat
    *            格式为的格式
    * @return
    */
   public static String format(String dateStr, String format, String toFormat) {
      return format(format(dateStr, format), toFormat);
   }

   /**
    * yyyy-MM-dd 转化成 yyyy-MM-dd HH:mm:dd 格式, dateStr 字符串时间 format 指定的时分秒
    */
   public static String formatTime(String dateStr, String format) {

      if (null == dateStr || "".equals(dateStr)) {
         return null;
      }

      if (null == format || "".equals(format)) {
         return null;
      }

      String time = dateStr + " " + format;

      try {

         dateStr = dataTimeFormat.format(dataTimeFormat.parse(time));

      } catch (ParseException e) {

         e.printStackTrace();
      }

      return dateStr;

   }

   /**
    * 计算时间差
    * 
    * @return 返回两个时间秒的差别
    */
   public static long timeDiff(String sysDate, String uavHeartbeatDate) {
      long diff = 0L;
      try {
         Date d1 = dataTimeFormat.parse(sysDate);
         Date d2 = dataTimeFormat.parse(uavHeartbeatDate);
         diff = d1.getTime() - d2.getTime();
      } catch (Exception e) {
         LOGGER.error("字符串日期格式错误!!!");
         e.printStackTrace();
      }
      return diff / 1000;
   }
   
   
   /**
    * 获取当前时间戳
    * @param date
    * @return
    */
   public static String formatHMSS() {
      return format(new Date(), HMSS);
   }
   
   
   public static String formatYMD() {
      return format(new Date(), YMD);
   }
   
   /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    
    /* 
     * 将时间戳转换为时间
     */
    public static String stampToFormatDate(String s, String format){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    
    /**
     * 获取系统时间偏移n天的时间,比如:当前日期2018-08-15,偏移7天[2018-08-15, ..., 2018-08-20, 2018-08-21]
     * @return
     */
    public static List<String> getOffsetDaysFromNowTime(int offset){
       List<String> dataList = new ArrayList<>();
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       Calendar calendar = Calendar.getInstance();
       for (int i = 0; i < offset; i++) {
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + i);
          String day = dateFormat.format(calendar.getTime());
             dataList.add(day);
      }System.out.println(dataList);
       return dataList;
    }
    

   public static void main(String[] args) {
      String strDate = formatYMD();
      String strTime = formatHMSS();
      String strDateTime = strDate + " " + strTime;
      System.out.println(strDateTime);
      System.out.println(format("10:00:00", "HH:mm:ss", "HH:mm"));
      System.out.println(stampToFormatDate("1530781200000", Y_M_D));
      getOffsetDaysFromNowTime(7);
      System.out.println(getNowStr(YM));
   }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值