日期操作工具類,涵蓋了大部分的日期操作
package sg.biyong.utils;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 日期操作工具類
*/
public class DateUtils {
/**
* 日期轉(zhuǎn)換- String -> Date
*
* @param dateString 字符串時(shí)間
* @return Date類型信息
* @throws Exception 拋出異常
*/
public static Date parseString2Date(String dateString) throws Exception {
if (dateString == null) {
return null;
}
return parseString2Date(dateString, "yyyy-MM-dd");
}
/**
* 日期轉(zhuǎn)換- String -> Date
*
* @param dateString 字符串時(shí)間
* @param pattern 格式模板
* @return Date類型信息
* @throws Exception 拋出異常
*/
public static Date parseString2Date(String dateString, String pattern) throws Exception {
if (dateString == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateString);
return date;
}
/**
* 日期轉(zhuǎn)換 Date -> String
*
* @param date Date類型信息
* @return 字符串時(shí)間
* @throws Exception 拋出異常
*/
public static String parseDate2String(Date date) throws Exception {
if (date == null) {
return null;
}
return parseDate2String(date, "yyyy-MM-dd");
}
/**
* 日期轉(zhuǎn)換 Date -> String
*
* @param date Date類型信息
* @param pattern 格式模板
* @return 字符串時(shí)間
* @throws Exception 拋出異常
*/
public static String parseDate2String(Date date, String pattern) throws Exception {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String strDate = sdf.format(date);
return strDate;
}
/**
* 獲取當(dāng)前日期的本周一是幾號
*
* @return 本周一的日期
*/
public static Date getThisWeekMonday() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// 獲得當(dāng)前日期是一個(gè)星期的第幾天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 設(shè)置一個(gè)星期的第一天,按中國的習(xí)慣一個(gè)星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 獲得當(dāng)前日期是一個(gè)星期的第幾天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根據(jù)日歷的規(guī)則,給當(dāng)前日期減去星期幾與一個(gè)星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 獲取當(dāng)前日期周的最后一天
*
* @return 當(dāng)前日期周的最后一天
*/
public static Date getSundayOfThisWeek() {
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
c.add(Calendar.DATE, -dayOfWeek + 7);
return c.getTime();
}
/**
* 根據(jù)日期區(qū)間獲取月份列表
*
* @param minDate 開始時(shí)間
* @param maxDate 結(jié)束時(shí)間
* @return 月份列表
* @throws Exception
*/
public static List<String> getMonthBetween(String minDate, String maxDate, String format) throws Exception {
ArrayList<String> result = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
SimpleDateFormat sdf2 = new SimpleDateFormat(format);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf2.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
return result;
}
/**
* 根據(jù)日期獲取年度中的周索引
*
* @param date 日期
* @return 周索引
* @throws Exception
*/
public static Integer getWeekOfYear(String date) throws Exception {
Date useDate = parseString2Date(date);
Calendar cal = Calendar.getInstance();
cal.setTime(useDate);
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* 根據(jù)年份獲取年中周列表
*
* @param year 年分
* @return 周列表
* @throws Exception
*/
public static Map<Integer, String> getWeeksOfYear(String year) throws Exception {
Date useDate = parseString2Date(year, "yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(useDate);
//獲取年中周數(shù)量
int weeksCount = cal.getWeeksInWeekYear();
Map<Integer, String> mapWeeks = new HashMap<>(55);
for (int i = 0; i < weeksCount; i++) {
cal.get(Calendar.DAY_OF_YEAR);
mapWeeks.put(i + 1, parseDate2String(getFirstDayOfWeek(cal.get(Calendar.YEAR), i)));
}
return mapWeeks;
}
/**
* 獲取某年的第幾周的開始日期
*
* @param year 年分
* @param week 周索引
* @return 開始日期
* @throws Exception
*/
public static Date getFirstDayOfWeek(int year, int week) throws Exception {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
/**
* 獲取某年的第幾周的結(jié)束日期
*
* @param year 年份
* @param week 周索引
* @return 結(jié)束日期
* @throws Exception
*/
public static Date getLastDayOfWeek(int year, int week) throws Exception {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
/**
* 獲取當(dāng)前時(shí)間所在周的開始日期
*
* @param date 當(dāng)前時(shí)間
* @return 開始時(shí)間
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c.getTime();
}
/**
* 獲取當(dāng)前時(shí)間所在周的結(jié)束日期
*
* @param date 當(dāng)前時(shí)間
* @return 結(jié)束日期
*/
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
return c.getTime();
}
//獲得上周一的日期
public static Date geLastWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, -7);
return cal.getTime();
}
//獲得本周一的日期
public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 獲得當(dāng)前日期是一個(gè)星期的第幾天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 設(shè)置一個(gè)星期的第一天,按中國的習(xí)慣一個(gè)星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 獲得當(dāng)前日期是一個(gè)星期的第幾天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根據(jù)日歷的規(guī)則葡盗,給當(dāng)前日期減去星期幾與一個(gè)星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
//獲得下周一的日期
public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
//獲得今天日期
public static Date getToday(){
return new Date();
}
//獲得本月一日的日期
public static Date getFirstDay4ThisMonth(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,1);
return calendar.getTime();
}
public static void main(String[] args) {
try {
System.out.println("本周一" + parseDate2String(getThisWeekMonday()));
System.out.println("本月一日" + parseDate2String(getFirstDay4ThisMonth()));
} catch (Exception e) {
e.printStackTrace();
}
}
}