時間處理工具類

package com.linkage.module.liposs.system.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.log4j.Logger;

public class DateTimeUtil
{
   
   private static final Logger LOG = Logger.getLogger(DateTimeUtil.class);
   
   private static ThreadLocal<Date> dateLocal = new ThreadLocal<Date>() {
      
      @Override
      protected Date initialValue ()
      {
         return new Date();
      }
   };
   
   private static ThreadLocal<Calendar> calendarLocal = new ThreadLocal<Calendar>() {
      
      @Override
      protected Calendar initialValue ()
      {
         return Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
      }
   };
   
   // 獲取當月或指定時間的的第一天
   /**
    * 獲取指定時間月的第一天 如果date 爲null 則返回當前月的第一天
    * 
    * @param date
    * @return Date
    */
   public static Date firstDayOfMonth (Date date) throws Exception
   {
      Calendar cal = calendarLocal.get();
      if (date == null)
      {
         date = dateLocal.get();
      }
      cal.setTime(date);
      int value = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
      cal.set(Calendar.DAY_OF_MONTH, value);
      return cal.getTime();
   }
   
   /**
    * 通過時間date 獲取此時間的前幾天或者後幾天的時間date 如果date爲null 則返回當前時間的前幾天或者後幾天date
    * 
    * @param date
    *            指定時間date
    * @param count
    *            過去退幾天(負數), 未來前進幾天(正數)
    * @return date
    */
   public static Date getDateByDate (Date date, int count) throws Exception
   {
      Calendar ca = calendarLocal.get();
      if (date == null)
      {
         date = dateLocal.get();
      }
      ca.setTime(date);
      ca.add(Calendar.DAY_OF_MONTH, count);
      return ca.getTime();
   }
   
   /**
    * 根據long時間 獲取date 如果long爲0則返回當前時間date
    * 
    * @param longTime
    * @return
    */
   public static Date getDateBylong (long longTime) throws Exception
   {
      Date date = dateLocal.get();
      if (longTime == 0)
      {
         return date;
      } else
      {
         date.setTime(longTime * 1000L);
         return date;
      }
   }
   
   /**
    * 獲取時間date 如果str爲null或者"" 則返回當前時間date
    * 
    * @param str
    *            時間字符串
    * @param format
    *            時間字符串格式(如str爲:2018_05_29 12:12:00; 則format即爲:yyyy_MM_dd
    *            HH:mm:ss)
    * @return date
    */
   public static Date getDateByString (String str, String format) throws Exception
   {
      Date date = null;
      if (str == null || "".equals(str))
      {
         return dateLocal.get();
      } else
      {
         SimpleDateFormat sdf = new SimpleDateFormat(format);
         try
         {
            date = sdf.parse(str);
         } catch (ParseException e)
         {
            LOG.error("【時間轉換錯誤】" + e);
         }
      }
      return date;
   }
   
   /**
    * 通過時間date 獲取指定時間(或當前時間 即date爲null) 所在周的星期幾 date
    * 
    * @param date
    *            指定時間date
    * @param count
    *            所在周的星期幾(1~7)週一爲 1
    * @return date
    */
   public static Date getDayOfWeek (Date date, int count) throws Exception
   {
      if (date == null)
      {
         date = dateLocal.get();
      }
      Calendar ca = calendarLocal.get();
      ca.setTime(date);
      switch (count) {
         case 1:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            break;
         case 2:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
            break;
         case 3:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
            break;
         case 4:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
            break;
         case 5:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
            break;
         case 6:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            break;
         case 7:
            ca.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            break;
         default:
            break;
      }
      return ca.getTime();
   }
   
   /**
    * 獲取某一天的結束時間long 如果date爲null 則返回當天的結束時間 long
    * 
    * @param date
    * @return
    */
   public static long getEndOfDay (Date date) throws Exception
   {
      if (date == null)
      {
         date = dateLocal.get();
      }
      String newTime = getStringByDate(date, "yyyy_MM_dd");
      date = getDateByString(newTime, "yyyy_MM_dd");
      return date.getTime() / 1000L + 86399L;
   }
   
   /**
    * 根據時間字符串獲取某一天的結束時間long 如果str爲null或"" 則返回當天的結束時間 long
    * 
    * @param str
    *            時間字符串
    * @param format
    *            時間字符串的格式(如str爲:2018_05_29 12:12:00; 則format即爲:yyyy_MM_dd
    *            HH:mm:ss)
    * @return
    */
   public static long getEndOfDay (String str, String format) throws Exception
   {
      Date date = null;
      if (str == null || "".equals(str))
      {
         date = dateLocal.get();
      } else
      {
         date = getDateByString(str, format);
      }
      String newTime = getStringByDate(date, "yyyy_MM_dd");
      date = getDateByString(newTime, "yyyy_MM_dd");
      return date.getTime() / 1000L + 86399L;
   }
   
   /**
    * 根據時間date 獲取long 如果date爲null則返回當前時間long
    * 
    * @param date
    * @return
    */
   public static long getLongByDate (Date date) throws Exception
   {
      long temp = 0;
      if (date == null)
      {
         temp = dateLocal.get().getTime() / 1000L;
      } else
      {
         temp = date.getTime() / 1000L;
      }
      return temp;
   }
   
   /**
    * 字符串轉換成long類型 如果str爲null或"" 即獲取當前時間
    * 
    * @param str
    *            時間字符串
    * @param format
    *            需要將時間字符串str轉換成爲的格式(如str爲:2018_05_29 12:12:00;
    *            則format即爲:yyyy_MM_dd HH:mm:ss)
    * @return
    */
   public static long getLongByString (String str, String format) throws Exception
   {
      long longdate = 0L;
      if (str == null || "".equals(str))
      {
         longdate = dateLocal.get().getTime() / 1000L;
      } else
      {
         // SimpleDateFormat sdf = new SimpleDateFormat(format);
         longdate = getDateByString(str, format).getTime() / 1000L;
      }
      return longdate;
   }
   
   /**
    * 獲取某一天的開始時間long 如果date爲null 則返回當天的開始時間 long
    * 
    * @param date
    * @return
    */
   public static long getStartOfDay (Date date) throws Exception
   {
      if (date == null)
      {
         date = dateLocal.get();
      }
      String newTime = getStringByDate(date, "yyyy_MM_dd");
      date = getDateByString(newTime, "yyyy_MM_dd");
      return date.getTime() / 1000L;
   }
   
   /**
    * 根據時間字符串獲取某一天的開始時間long 如果str爲null或"" 則返回當天的開始時間 long
    * 
    * @param str
    *            時間字符串
    * @param format
    *            時間字符串str對應的的格式(如str爲:2018_05_29 12:12:00; 則format即爲:yyyy_MM_dd
    *            HH:mm:ss)
    * @return
    */
   public static long getStartOfDay (String str, String format) throws Exception
   {
      Date date = null;
      if (str == null || "".equals(str))
      {
         date = dateLocal.get();
      } else
      {
         date = getDateByString(str, format);
      }
      String newTime = getStringByDate(date, "yyyy_MM_dd");
      date = getDateByString(newTime, "yyyy_MM_dd");
      return date.getTime() / 1000L;
   }
   
   /**
    * 時間轉換 如果date爲null 即獲取當前時間
    * 
    * @param date
    * @param format
    *            需要將date轉換成爲的格式(如yyyy_MM_dd HH:mm:ss)
    * @return
    */
   public static String getStringByDate (Date date, String format) throws Exception
   {
      if (date == null)
      {
         date = dateLocal.get();
      }
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      String dateStr = "";
      dateStr = sdf.format(date);
      return dateStr;
   }
   
   /**
    * 根據long時間 獲取想要轉成的格式字符串 如果long爲0則返回當前時間字符串
    * 
    * @param longTime
    * @param format
    *            需要將longTime轉換成爲的格式(如yyyy_MM_dd HH:mm:ss)
    * @return
    */
   public static String getStringBylong (long longTime, String format) throws Exception
   {
      Date date = dateLocal.get();
      if (longTime != 0)
      {
         date.setTime(longTime * 1000L);
      }
      return getStringByDate(date, format);
   }
   
   /**
    * 獲取指定時間所在月的最後一天 如果date 爲null 則返回當前月的最後一天
    * 
    * @param date
    * @return Date
    */
   public static Date lastDayOfMonth (Date date) throws Exception
   {
      Calendar cal = null;
      if (date == null)
      {
         date = dateLocal.get();
      }
      cal = calendarLocal.get();
      cal.setTime(date);
      int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
      cal.getActualMinimum(Calendar.DAY_OF_MONTH);
      cal.set(Calendar.DAY_OF_MONTH, value);
      return cal.getTime();
   }
   
   /**
    * 獲取指定日期 所對應的一年中第幾周(如果dateStr爲 null 或者 "" 則返回當前時間所在的周)
    * 
    * @param str
    *            時間字符串(如:2018-05-29 12:12:00)
    * @param format
    *            與str對應的格式(如str爲:2018-05-29 12:12:00; format即爲:yyyy-MM-dd
    *            HH:mm:ss)
    * @return
    * @throws Exception
    */
   public static int getWeekOfYear (String str, String format) throws Exception
   {
      Date date = null;
      if (str == null || "".equals(str))
      {
         date = dateLocal.get();
      } else
      {
         date = getDateByString(str, format);
      }
      Calendar calendar = calendarLocal.get();
      calendar.setFirstDayOfWeek(Calendar.MONDAY);
      calendar.setTime(date);
      return calendar.get(Calendar.WEEK_OF_YEAR);
   }
   
   /**
    * 獲取指定日期是星期幾 如果date爲null 則獲取當前時間星期幾
    * 
    * @param date
    * @return 當前日期是星期幾 週日爲7
    */
   public static int getWeekOfDate (Date date) throws Exception
   {
      if (date == null)
      {
         date = dateLocal.get();
      }
      int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
      Calendar cal = calendarLocal.get();
      cal.setTime(date);
      int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
      if (w < 0)
         w = 0;
      return weekDays[w];
   }
   
   public static void main (String[] args)
   {
      try
      {
         // System.out.println(getWeekOfDate(getDateByString("20180528",
         // "yyyyMMdd")));
         System.out.println(getWeekOfYear("", ""));
      } catch (Exception e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章