前言
在項目中常常會使用到日期處理的方法,花費了一點時間整理下我使用過的方法,整合成工具類,方便之後使用。java
工具類
package cc.vvxtoys.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * * @author vvxtoys * @date 2018年1月6日下午9:48:02 * */ public class DateUtils { private static Calendar cal = Calendar.getInstance(); private static String fm = "";//format month private static String fd = "";//format day /** * @description 判斷是否爲閏年 */ public static boolean isLeapYear(int year){ return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } /** * * @description 獲取月天數 */ public static String getMonthDays(int year,int month){ String [] days = {"31", null, "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"}; if(isLeapYear(year)){ days[1]="29"; }else{ days[1]="28"; } return days[month-1]; } /** * @description 獲取時間在當前年爲第幾周 */ public static int getWeekOfYear(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cal.setFirstDayOfWeek(Calendar.MONDAY); String firstDay = date.substring(0, 4) + "-01-01"; cal.setTime(sdf.parse(firstDay)); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if (dayOfWeek == 2) { cal.setTime(sdf.parse(date)); int num = cal.get(Calendar.WEEK_OF_YEAR); return num; } else { cal.setTime(sdf.parse(date)); int num = cal.get(Calendar.WEEK_OF_YEAR) - 1; return num; } } /** * * @description 天計算 */ public static String addDays(String date,int num){ try{ cal.setTime(toDate(date)); cal.add(Calendar.DATE, num); toFormatDate(); }catch(Exception e){ e.printStackTrace(); } return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 星期 */ public static String addWeeks(String date,int num){ try { cal.setTime(toDate(date)); cal.add(Calendar.WEEK_OF_YEAR, num); toFormatDate(); } catch (ParseException e) { e.printStackTrace(); } return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 月 */ public static String addMonths(String date,int num){ try { cal.setTime(toDate(date)); cal.add(Calendar.MONTH, num); toFormatDate(); } catch (ParseException e) { e.printStackTrace(); } return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 季度 */ public static String addQuarters(String date,int num){ try { cal.setTime(toDate(date)); cal.add(Calendar.MONTH, num*3); toFormatDate(); } catch (ParseException e) { e.printStackTrace(); } return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 年 */ public static String addYears(String date,int num){ try { cal.setTime(toDate(date)); cal.add(Calendar.YEAR, num); toFormatDate(); } catch (ParseException e) { e.printStackTrace(); } return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 日期轉換 */ public static Object translateDate(Object time, String pattern) { Object date = new Object(); if (isEmpty(pattern)) { pattern = "yyyy-MM-dd"; } if (isEmpty(time)) { time = "1970-1-1"; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { if (time instanceof String) { date = sdf.parse((String) time); } else if (time instanceof Date) { date = sdf.format(time); } else { throw new Exception("轉換失敗"); } } catch (Exception e) { e.printStackTrace(); } return date; } /** * * @description 當前日期爲本週第幾天 */ public static int getDayOfWeek(String date){ try{ cal.setTime(toDate(date)); cal.setFirstDayOfWeek(Calendar.MONDAY); }catch(ParseException e){ e.printStackTrace(); } return cal.get(Calendar.DAY_OF_WEEK)==1?7:cal.get(Calendar.DAY_OF_WEEK)-1; } /** * * @description 週一 */ public static String getStartDayOfThisWeek(String date){ return addDays(date, -(getDayOfWeek(date)-1)); } /** * * @description 週日 */ public static String getEndDayOfThisWeek(String date){ return addDays(date,(7-getDayOfWeek(date))); } //獲取當前時間 public static String getCurrentDate(){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } //當前時間 public static String getCurrentDate(String pattern){ Date date = new Date(); if(isEmpty(pattern)){ pattern = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(date); } /** * * @description 時間間隔 月 季度 年 *3 *12 */ public static int getSubDays(int num){ Date date = new Date(); cal.setTime(date); cal.add(Calendar.MONTH, num); long time = cal.getTimeInMillis() - date.getTime(); return (int) (time / (1000 * 60 * 60 * 24)); } /** * * @description 獲取第幾周開始時間 */ public static String getStartDayOfWeek(int year,int num){ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.set(Calendar.YEAR, year); cal.set(Calendar.WEEK_OF_YEAR, num); toFormatDate(); return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 獲取第幾周結束時間 */ public static String getEndDayOfWeek(int year,int num){ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.set(Calendar.YEAR, year); cal.set(Calendar.WEEK_OF_YEAR, num); cal.add(Calendar.DAY_OF_WEEK, 6); toFormatDate(); return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } /** * * @description 獲取第幾月開始時間 */ public static String getStartDayOfMonth(int year,int num){ String month = num < 10 ? "0" + num : String.valueOf(num); return year + "-" + month + "-" + "01"; } /** * * @description 獲取第幾月結束時間 */ public static String getEndDayOfMonth(int year,int num){ cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, num - 1); cal.set(Calendar.DATE, 1); cal.add(Calendar.MONTH, 1); cal.add(Calendar.DAY_OF_YEAR, -1); toFormatDate(); return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } //獲取第幾季度開始時間 public static String getStartDayOfQuarter(int year,int num){ num = 3 * num - 2; String month = num < 10 ? "0" + num : String.valueOf(num); return year + "-" + month + "-" + "01"; } //獲取第幾季度結束時間 public static String getEndDayOfQuarter(int year,int num){ cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, 3 * num - 1); cal.set(Calendar.DATE, 1); cal.add(Calendar.MONTH, 1); cal.add(Calendar.DAY_OF_YEAR, -1); toFormatDate(); return cal.get(Calendar.YEAR) + "-"+fm+"-"+fd; } //時間間隔 public static int intervalDay(String d1,String d2){ int num = 0; try{ long second = toDate(d2).getTime()-toDate(d1).getTime(); num=(int)(Math.abs(second)/(1000 * 60 * 60 * 24)); }catch(ParseException e){ e.printStackTrace(); } return num; } private static void toFormatDate(){ int month = (cal.get(Calendar.MONTH) + 1); fm = month<10?"0"+month:String.valueOf(month); int day = cal.get(Calendar.DAY_OF_MONTH); fd = day<10?"0"+day:String.valueOf(day); } private static Date toDate(String date) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.parse(date); } private static boolean isEmpty(Object obj){ return obj==null||obj.toString().trim().equals(""); } }