溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java日期工具類DateUtils實例詳解

發(fā)布時間:2020-10-25 20:45:36 來源:腳本之家 閱讀:382 作者:IT狗探求 欄目:編程語言

在項目開發(fā)中,日期是我們必不可少的的一部分,本文將總結(jié)代碼開發(fā)中的關(guān)于日期常用的一些方法,以方便自己后期使用。下面直接上菜了:

package com.example.util; 
 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.List; 
 
/** 
 * 日期常用方法 
 * 
 * @author 
 * 
 */ 
public class DateUtils { 
 
  /** 
   * 常用變量 
   */ 
  public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss"; 
  public static final String DATE_FORMAT_YMD = "yyyy-MM-dd"; 
  public static final String DATE_FORMAT_HMS = "HH:mm:ss"; 
  public static final String DATE_FORMAT_HM = "HH:mm"; 
  public static final String DATE_FORMAT_YMDHM = "yyyy-MM-dd HH:mm"; 
  public static final String DATE_FORMAT_YMDHMS = "yyyyMMddHHmmss"; 
  public static final long ONE_DAY_MILLS = 3600000 * 24; 
  public static final int WEEK_DAYS = 7; 
  private static final int dateLength = DATE_FORMAT_YMDHM.length(); 
   
   
  /** 
   * 日期轉(zhuǎn)換為制定格式字符串 
   * 
   * @param time 
   * @param format 
   * @return 
   */ 
  public static String formatDateToString(Date time, String format) { 
    SimpleDateFormat sdf = new SimpleDateFormat(format); 
    return sdf.format(time); 
  } 
 
  /** 
   * 字符串轉(zhuǎn)換為制定格式日期 
   * (注意:當你輸入的日期是2014-12-21 12:12,format對應(yīng)的應(yīng)為yyyy-MM-dd HH:mm 
   * 否則異常拋出) 
   * @param date 
   * @param format 
   * @return 
   * @throws ParseException 
   *       @ 
   */ 
  public static Date formatStringToDate(String date, String format) { 
    SimpleDateFormat sdf = new SimpleDateFormat(format); 
    try { 
      return sdf.parse(date); 
    } catch (ParseException ex) { 
      ex.printStackTrace(); 
      throw new RuntimeException(ex.toString()); 
    } 
  } 
 
  /** 
   * 判斷一個日期是否屬于兩個時段內(nèi) 
   * @param time 
   * @param timeRange 
   * @return 
   */ 
  public static boolean isTimeInRange(Date time, Date[] timeRange) { 
    return (!time.before(timeRange[0]) && !time.after(timeRange[1])); 
  } 
 
  /** 
   * 從完整的時間截取精確到分的時間 
   * 
   * @param fullDateStr 
   * @return 
   */ 
  public static String getDateToMinute(String fullDateStr) { 
    return fullDateStr == null ? null 
        : (fullDateStr.length() >= dateLength ? fullDateStr.substring( 
            0, dateLength) : fullDateStr); 
  } 
 
  /** 
   * 返回指定年度的所有周。List中包含的是String[2]對象 string[0]本周的開始日期,string[1]是本周的結(jié)束日期。 
   * 日期的格式為YYYY-MM-DD 每年的第一個周,必須包含星期一且是完整的七天。 
   * 例如:2009年的第一個周開始日期為2009-01-05,結(jié)束日期為2009-01-11。 星期一在哪一年,那么包含這個星期的周就是哪一年的周。 
   * 例如:2008-12-29是星期一,2009-01-04是星期日,哪么這個周就是2008年度的最后一個周。 
   * 
   * @param year 
   *      格式 YYYY ,必須大于1900年度 小于9999年 
   * @return @ 
   */ 
  public static List<String[]> getWeeksByYear(final int year) { 
    int weeks = getWeekNumOfYear(year); 
    List<String[]> result = new ArrayList<String[]>(weeks); 
    int start = 1; 
    int end = 7; 
    for (int i = 1; i <= weeks; i++) { 
      String[] tempWeek = new String[2]; 
      tempWeek[0] = getDateForDayOfWeek(year, i, start); 
      tempWeek[1] = getDateForDayOfWeek(year, i, end); 
      result.add(tempWeek); 
    } 
    return result; 
  } 
 
  /** 
   * 計算指定年、周的上一年、周 
   * 
   * @param year 
   * @param week 
   * @return @ 
   */ 
  public static int[] getLastYearWeek(int year, int week) { 
    if (week <= 0) { 
      throw new IllegalArgumentException("周序號不能小于1!!"); 
    } 
    int[] result = { week, year }; 
    if (week == 1) { 
      // 上一年 
      result[1] -= 1; 
      // 最后一周 
      result[0] = getWeekNumOfYear(result[1]); 
    } else { 
      result[0] -= 1; 
    } 
    return result; 
  } 
 
  /** 
   * 下一個[周,年] 
   * 
   * @param year 
   * @param week 
   * @return @ 
   */ 
  public static int[] getNextYearWeek(int year, int week) { 
    if (week <= 0) { 
      throw new IllegalArgumentException("周序號不能小于1??!"); 
    } 
    int[] result = { week, year }; 
    int weeks = getWeekNumOfYear(year); 
    if (week == weeks) { 
      // 下一年 
      result[1] += 1; 
      // 第一周 
      result[0] = 1; 
    } else { 
      result[0] += 1; 
    } 
    return result; 
  } 
 
  /** 
   * 計算指定年度共有多少個周。(從周一開始) 
   * 
   * @param year 
   * @return @ 
   */ 
  public static int getWeekNumOfYear(final int year) { 
    return getWeekNumOfYear(year, Calendar.MONDAY); 
  } 
 
  /** 
   * 計算指定年度共有多少個周。 
   * 
   * @param year 
   *      yyyy 
   * @return @ 
   */ 
  public static int getWeekNumOfYear(final int year, int firstDayOfWeek) { 
    // 每年至少有52個周 ,最多有53個周。 
    int minWeeks = 52; 
    int maxWeeks = 53; 
    int result = minWeeks; 
    int sIndex = 4; 
    String date = getDateForDayOfWeek(year, maxWeeks, firstDayOfWeek); 
    // 判斷年度是否相符,如果相符說明有53個周。 
    if (date.substring(0, sIndex).equals(year)) { 
      result = maxWeeks; 
    } 
    return result; 
  } 
 
  public static int getWeeksOfWeekYear(final int year) { 
    Calendar cal = Calendar.getInstance(); 
    cal.setFirstDayOfWeek(Calendar.MONDAY); 
    cal.setMinimalDaysInFirstWeek(WEEK_DAYS); 
    cal.set(Calendar.YEAR, year); 
    return cal.getWeeksInWeekYear(); 
  } 
 
  /** 
   * 獲取指定年份的第幾周的第幾天對應(yīng)的日期yyyy-MM-dd(從周一開始) 
   * 
   * @param year 
   * @param weekOfYear 
   * @param dayOfWeek 
   * @return yyyy-MM-dd 格式的日期 @ 
   */ 
  public static String getDateForDayOfWeek(int year, int weekOfYear, 
      int dayOfWeek) { 
    return getDateForDayOfWeek(year, weekOfYear, dayOfWeek, Calendar.MONDAY); 
  } 
 
  /** 
   * 獲取指定年份的第幾周的第幾天對應(yīng)的日期yyyy-MM-dd,指定周幾算一周的第一天(firstDayOfWeek) 
   * 
   * @param year 
   * @param weekOfYear 
   * @param dayOfWeek 
   * @param firstDayOfWeek 
   *      指定周幾算一周的第一天 
   * @return yyyy-MM-dd 格式的日期 
   */ 
  public static String getDateForDayOfWeek(int year, int weekOfYear, 
      int dayOfWeek, int firstDayOfWeek) { 
    Calendar cal = Calendar.getInstance(); 
    cal.setFirstDayOfWeek(firstDayOfWeek); 
    cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); 
    cal.setMinimalDaysInFirstWeek(WEEK_DAYS); 
    cal.set(Calendar.YEAR, year); 
    cal.set(Calendar.WEEK_OF_YEAR, weekOfYear); 
    return formatDateToString(cal.getTime(), DATE_FORMAT_YMD); 
  } 
 
  /** 
   * 獲取指定日期星期幾 
   * 
   * @param datetime 
   * @throws ParseException 
   *       @ 
   */ 
  public static int getWeekOfDate(String datetime) throws ParseException { 
    Calendar cal = Calendar.getInstance(); 
    cal.setFirstDayOfWeek(Calendar.MONDAY); 
    cal.setMinimalDaysInFirstWeek(WEEK_DAYS); 
    Date date = formatStringToDate(datetime, DATE_FORMAT_YMD); 
    cal.setTime(date); 
    return cal.get(Calendar.DAY_OF_WEEK); 
 
  } 
 
  /** 
   * 計算某年某周內(nèi)的所有日期(從周一開始 為每周的第一天) 
   * 
   * @param yearNum 
   * @param weekNum 
   * @return @ 
   */ 
  public static List<String> getWeekDays(int yearNum, int weekNum) { 
    return getWeekDays(yearNum, weekNum, Calendar.MONDAY); 
  } 
 
  /** 
   * 計算某年某周內(nèi)的所有日期(七天) 
   * 
   * @param yearNum 
   * @param weekNum 
   * @return yyyy-MM-dd 格式的日期列表 
   */ 
  public static List<String> getWeekDays(int year, int weekOfYear, 
      int firstDayOfWeek) { 
    List<String> dates = new ArrayList<String>(); 
    int dayOfWeek = firstDayOfWeek; 
    for (int i = 0; i < WEEK_DAYS; i++) { 
      dates.add(getDateForDayOfWeek(year, weekOfYear, dayOfWeek++, 
          firstDayOfWeek)); 
    } 
    return dates; 
  } 
 
  /** 
   * 獲取目標日期的上周、或本周、或下周的年、周信息 
   * 
   * @param queryDate 
   *      傳入的時間 
   * @param weekOffset 
   *      -1:上周 0:本周 1:下周 
   * @param firstDayOfWeek 
   *      每周以第幾天為首日 
   * @return 
   * @throws ParseException 
   */ 
  public static int[] getWeekAndYear(String queryDate, int weekOffset, 
      int firstDayOfWeek) throws ParseException { 
 
    Date date = formatStringToDate(queryDate, DATE_FORMAT_YMD); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 
    calendar.setFirstDayOfWeek(firstDayOfWeek); 
    calendar.setMinimalDaysInFirstWeek(WEEK_DAYS); 
    int year = calendar.getWeekYear(); 
    int week = calendar.get(Calendar.WEEK_OF_YEAR); 
    int[] result = { week, year }; 
    switch (weekOffset) { 
    case 1: 
      result = getNextYearWeek(year, week); 
      break; 
    case -1: 
      result = getLastYearWeek(year, week); 
      break; 
    default: 
      break; 
    } 
    return result; 
  } 
 
  /** 
   * 計算個兩日期的天數(shù) 
   * 
   * @param startDate 
   *      開始日期字串 
   * @param endDate 
   *      結(jié)束日期字串 
   * @return 
   * @throws ParseException 
   */ 
  public static int getDaysBetween(String startDate, String endDate) 
      throws ParseException { 
    int dayGap = 0; 
    if (startDate != null && startDate.length() > 0 && endDate != null 
        && endDate.length() > 0) { 
      Date end = formatStringToDate(endDate, DATE_FORMAT_YMD); 
      Date start = formatStringToDate(startDate, DATE_FORMAT_YMD); 
      dayGap = getDaysBetween(start, end); 
    } 
    return dayGap; 
  } 
 
  private static int getDaysBetween(Date startDate, Date endDate) { 
    return (int) ((endDate.getTime() - startDate.getTime()) / ONE_DAY_MILLS); 
  } 
 
  /** 
   * 計算兩個日期之間的天數(shù)差 
   * @param startDate 
   * @param endDate 
   * @return 
   */ 
  public static int getDaysGapOfDates(Date startDate, Date endDate) { 
    int date = 0; 
    if (startDate != null && endDate != null) { 
      date = getDaysBetween(startDate, endDate); 
    } 
    return date; 
  } 
 
  /** 
   * 計算兩個日期之間的年份差距 
   * 
   * @param firstDate 
   * @param secondDate 
   * @return 
   */ 
 
  public static int getYearGapOfDates(Date firstDate, Date secondDate) { 
    if (firstDate == null || secondDate == null) { 
      return 0; 
    } 
    Calendar helpCalendar = Calendar.getInstance(); 
    helpCalendar.setTime(firstDate); 
    int firstYear = helpCalendar.get(Calendar.YEAR); 
    helpCalendar.setTime(secondDate); 
    int secondYear = helpCalendar.get(Calendar.YEAR); 
    return secondYear - firstYear; 
  } 
 
  /** 
   * 計算兩個日期之間的月份差距 
   * 
   * @param firstDate 
   * @param secondDate 
   * @return 
   */ 
  public static int getMonthGapOfDates(Date firstDate, Date secondDate) { 
    if (firstDate == null || secondDate == null) { 
      return 0; 
    } 
 
    return (int) ((secondDate.getTime() - firstDate.getTime()) 
        / ONE_DAY_MILLS / 30); 
 
  } 
 
  /** 
   * 計算是否包含當前日期 
   * 
   * @param datys 
   * @return 
   */ 
  public static boolean isContainCurrent(List<String> dates) { 
    boolean flag = false; 
    SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT_YMD); 
    Date date = new Date(); 
    String dateStr = fmt.format(date); 
    for (int i = 0; i < dates.size(); i++) { 
      if (dateStr.equals(dates.get(i))) { 
        flag = true; 
      } 
    } 
    return flag; 
  } 
 
  /** 
   * 從date開始計算time天后的日期 
   * 
   * @param date 
   * @param time 
   * @return 
   * @throws ParseException 
   */ 
  public static String getCalculateDateToString(String startDate, int time) 
      throws ParseException { 
    String resultDate = null; 
    if (startDate != null && startDate.length() > 0) { 
      Date date = formatStringToDate(startDate, DATE_FORMAT_YMD); 
      Calendar c = Calendar.getInstance(); 
      c.setTime(date); 
      c.set(Calendar.DAY_OF_YEAR, time); 
      date = c.getTime(); 
      resultDate = formatDateToString(date, DATE_FORMAT_YMD); 
    } 
    return resultDate; 
  } 
 
  /** 
   * 獲取從某日期開始計算,指定的日期所在該年的第幾周 
   * 
   * @param date 
   * @param admitDate 
   * @return 
   * @throws ParseException 
   *       @ 
   */ 
  public static int[] getYearAndWeeks(String date, String admitDate) 
      throws ParseException { 
    Calendar c = Calendar.getInstance(); 
    c.setTime(formatStringToDate(admitDate, DATE_FORMAT_YMD)); 
    int time = c.get(Calendar.DAY_OF_WEEK); 
    return getWeekAndYear(date, 0, time); 
  } 
 
  /** 
   * 獲取指定日期refDate,前或后一周的所有日期 
   * 
   * @param refDate 
   *      參考日期 
   * @param weekOffset 
   *      -1:上周 0:本周 1:下周 
   * @param startDate 
   *      哪天算一周的第一天 
   * @return yyyy-MM-dd 格式的日期 
   * @throws ParseException 
   *       @ 
   */ 
  public static List<String> getWeekDaysAroundDate(String refDate, 
      int weekOffset, String startDate) throws ParseException { 
    // 以startDate為一周的第一天 
    Calendar c = Calendar.getInstance(); 
    c.setTime(formatStringToDate(startDate, DATE_FORMAT_YMD)); 
    int firstDayOfWeek = c.get(Calendar.DAY_OF_WEEK); 
    // 獲取相應(yīng)周 
    int[] weekAndYear = getWeekAndYear(refDate, weekOffset, firstDayOfWeek); 
    // 獲取相應(yīng)周的所有日期 
    return getWeekDays(weekAndYear[1], weekAndYear[0], firstDayOfWeek); 
  } 
 
  /** 
   * 根據(jù)時間點獲取時間區(qū)間 
   * 
   * @param hours 
   * @return 
   */ 
  public static List<String[]> getTimePointsByHour(int[] hours) { 
    List<String[]> hourPoints = new ArrayList<String[]>(); 
    String sbStart = ":00:00"; 
    String sbEnd = ":59:59"; 
    for (int i = 0; i < hours.length; i++) { 
      String[] times = new String[2]; 
      times[0] = hours[i] + sbStart; 
      times[1] = (hours[(i + 1 + hours.length) % hours.length] - 1) 
          + sbEnd; 
      hourPoints.add(times); 
    } 
    return hourPoints; 
  } 
 
  /** 
   * 
   * 根據(jù)指定的日期,增加或者減少天數(shù) 
   * 
   * @param date 
   * @param amount 
   * @return 
   */ 
  public static Date addDays(Date date, int amount) { 
    return add(date, Calendar.DAY_OF_MONTH, amount); 
  } 
 
  /** 
   * 根據(jù)指定的日期,類型,增加或減少數(shù)量 
   * 
   * @param date 
   * @param calendarField 
   * @param amount 
   * @return 
   */ 
  public static Date add(Date date, int calendarField, int amount) { 
    if (date == null) { 
      throw new IllegalArgumentException("The date must not be null"); 
    } 
    Calendar c = Calendar.getInstance(); 
    c.setTime(date); 
    c.add(calendarField, amount); 
    return c.getTime(); 
  } 
 
  /** 
   * 獲取當前日期的最大日期 時間2014-12-21 23:59:59 
   * @return 
   */ 
  public static Date getCurDateWithMaxTime() { 
    Calendar c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, 23); 
    c.set(Calendar.MINUTE, 59); 
    c.set(Calendar.SECOND, 59); 
    return c.getTime(); 
  } 
 
  /** 
   * 獲取當前日期的最小日期時間 2014-12-21 00:00:00 
   * @return 
   */ 
  public static Date getCurDateWithMinTime() { 
    Calendar c = Calendar.getInstance(); 
    c.set(Calendar.HOUR_OF_DAY, 0); 
    c.set(Calendar.MINUTE, 0); 
    c.set(Calendar.SECOND, 0); 
    c.set(Calendar.MILLISECOND, 0); 
    return c.getTime(); 
  } 
 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI