日期处理工具类
今天編寫一個日期處理工具類,便于大家開發過程中使用,將時間節省下來多陪陪你愛的人和愛你的人……
?
日期處理工具類
?
import java.util.ArrayList;import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.DateUtils;
public class DateUtil {
? ?public static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
? ?public static SimpleDateFormat timeFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
? ?public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
? ?public static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd");
? ?public static SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy.MM.dd");
? ?public static SimpleDateFormat monthAndDayFormat = new SimpleDateFormat("MM/dd");
? ?public static SimpleDateFormat yearAndMonthFormat = new SimpleDateFormat("yyyy-MM");
? ?public static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
? ?/**
? ? * 獲取:從指定日期開始,相隔指定【跨度月】的,最后一天(如當前3月,跨度2個月,返回4月30號)
? ? *
? ? * @param startDate
? ? * @param monthDuration
? ? * @return
? ? * @author caojun1@hisense.com
? ? */
? ?public static Date getDuraMonthLastDay(Date startDate, int monthDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?Calendar lastDate = Calendar.getInstance();
? ? ? ?lastDate.setTime(startDate);
? ? ? ?lastDate.add(Calendar.MONTH, monthDuration);// 加n個月
? ? ? ?lastDate.set(Calendar.DATE, 1);// 把日期設置為當月第一天
? ? ? ?lastDate.roll(Calendar.DAY_OF_YEAR, -1);// 日期回滾一天,也就是本月最后一天
? ? ? ?return lastDate.getTime();
? ?}
? ?/**
? ? * 獲取:從指定日期開始,相隔指定跨度【月】的,第一天(如當前3月,跨度2個月,返回4月1號)
? ? *
? ? * @param startDate
? ? * @param monthDuration
? ? * @return
? ? * @author caojun1@hisense.com
? ? */
? ?public static Date getDuraMonthFirstDay(Date startDate, int monthDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?Calendar lastDate = Calendar.getInstance();
? ? ? ?lastDate.setTime(startDate);
? ? ? ?lastDate.add(Calendar.MONTH, monthDuration - 1);// 加n-1個月
? ? ? ?lastDate.set(Calendar.DATE, 1);// 把日期設置為當月第一天
? ? ? ?return lastDate.getTime();
? ?}
? ?/**
? ? * 獲取:從指定日期開始,相隔指定跨度【自然周】的,最后一天
? ? *
? ? * @param startDate
? ? * @param weekDuration
? ? * @return
? ? */
? ?public static Date getDuraWeekLastDay(Date startDate, int weekDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?Calendar lastDate = Calendar.getInstance();
? ? ? ?lastDate.setTime(startDate);
? ? ? ?lastDate.add(Calendar.WEEK_OF_YEAR, weekDuration);// 加n個周
? ? ? ?lastDate.set(Calendar.DAY_OF_WEEK, 1);// 把日期設置為當周第一天(上周日)
? ? ? ?return lastDate.getTime();
? ?}
? ?/**
? ? * 獲取:從指定日期開始,相隔指定跨度【自然周】的,第一天
? ? *
? ? * @param startDate
? ? * @param weekDuration
? ? * @return
? ? */
? ?public static Date getDuraWeekFirstDay(Date startDate, int weekDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?Calendar lastDate = Calendar.getInstance();
? ? ? ?lastDate.setTime(startDate);
? ? ? ?lastDate.add(Calendar.WEEK_OF_YEAR, weekDuration);// 加n個周
? ? ? ?lastDate.set(Calendar.DAY_OF_WEEK, 2);// 把日期設置為當周第二天(本周一)
? ? ? ?return lastDate.getTime();
? ?}
? ?/**
? ? * 獲取:指定【跨度月】的、連續的月(其中,date的日,同為當前日)(順序:由近向遠期排序)
? ? *
? ? * @param startDate
? ? * ? ? ? ? ? ?可空(空時,為當前日期)
? ? * @param monthDuration
? ? * ? ? ? ? ? ?可負數(負數為前n個月)
? ? * @return
? ? */
? ?public static List<Date> getNextMonths2(Date startDate, int monthDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?List<Date> list = new ArrayList<Date>();
? ? ? ?int gap = 0;
? ? ? ?if (monthDuration > 0) {
? ? ? ? ? ?gap = 1;
? ? ? ?} else {
? ? ? ? ? ?gap = -1;
? ? ? ? ? ?monthDuration = 0 - monthDuration;
? ? ? ?}
? ? ? ?for (int i = 0; i < monthDuration; i++) {
? ? ? ? ? ?startDate = DateUtils.addMonths(startDate, gap);
? ? ? ? ? ?list.add(startDate);
? ? ? ?}
? ? ? ?return list;
? ?}
? ?/**
? ? * 獲取:指定【跨度月】的、連續的月(其中,date的日,同為當前日)(順序:時間倒序)
? ? *
? ? * @param startDate
? ? * ? ? ? ? ? ?可空(空時,為當前日期)
? ? * @param monthDuration
? ? * ? ? ? ? ? ?可負數(負數為前n個月)
? ? * @return
? ? */
? ?public static List<Date> getNextMonths(Date startDate, int monthDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?List<Date> list = new ArrayList<Date>();
? ? ? ?if (monthDuration > 0) {
? ? ? ? ? ?Date d;
? ? ? ? ? ?for (int i = 1; i < monthDuration + 1; i++) {
? ? ? ? ? ? ? ?d = DateUtils.addMonths(startDate, i);
? ? ? ? ? ? ? ?list.add(d);
? ? ? ? ? ?}
? ? ? ?} else {
? ? ? ? ? ?Date d;
? ? ? ? ? ?for (; monthDuration < 0; monthDuration++) {
? ? ? ? ? ? ? ?d = DateUtils.addMonths(startDate, monthDuration);
? ? ? ? ? ? ? ?list.add(d);
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return list;
? ?}
? ?/**
? ? * 獲取:指定【跨度周】的、連續的所有自然周(其中,date的周,同為當前周)
? ? *
? ? * @param startDate
? ? * ? ? ? ? ? ?可空(空時,為當前日期)
? ? * @param weekDuration
? ? * ? ? ? ? ? ?可負數(負數為前n個周)
? ? * @return
? ? */
? ?public static List<Date> getNextWeeks(Date startDate, int weekDuration) {
? ? ? ?if (startDate == null) {
? ? ? ? ? ?startDate = new Date();
? ? ? ?}
? ? ? ?List<Date> list = new ArrayList<Date>();
? ? ? ?int gap = 0;
? ? ? ?if (weekDuration > 0) {
? ? ? ? ? ?gap = 1;
? ? ? ?} else {
? ? ? ? ? ?gap = -1;
? ? ? ? ? ?weekDuration = 0 - weekDuration;
? ? ? ?}
? ? ? ?for (int i = 0; i < weekDuration; i++) {
? ? ? ? ? ?startDate = DateUtils.addWeeks(startDate, gap);
? ? ? ? ? ?list.add(startDate);
? ? ? ?}
? ? ? ?return list;
? ?}
}
?
解決各種Utils工具中使用SimpleDateFormat靜態變量 發生線程安全的問題
?
import java.text.ParseException;import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat ?{
? ?private final String formatPatten;
? ?public SimpleDateFormat(String patten) {
? ? ? ?formatPatten=patten;
? ?}
? ?public Date parse(String source) throws ParseException {
? ? ? ?return new SimpleDateFormat(formatPatten).parse(source);
? ?}
? ? public ?String format(Date date){
? ? ? ? return new SimpleDateFormat(formatPatten).format(date);
? ? }
}
?
轉載于:https://www.cnblogs.com/zhengjinsheng/p/11269723.html
總結
- 上一篇: Json解析工具类
- 下一篇: EnumSet详细讲解