1. 程式人生 > 其它 >常用公共工具類——日期處理類

常用公共工具類——日期處理類

/**
 * Copyright (c) 2016-2019 人人開源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版權所有,侵權必究!
 */

package io.renren.common.utils;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat; import java.util.Date; /** * 日期處理 * * @author Mark [email protected] */ public class DateUtils { /** 時間格式(yyyy-MM-dd) */ public final static String DATE_PATTERN = "yyyy-MM-dd"; /** 時間格式(yyyy-MM-dd HH:mm:ss) */ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/** * 日期格式化 日期格式為:yyyy-MM-dd * @param date 日期 * @return 返回yyyy-MM-dd格式日期 */ public static String format(Date date) { return format(date, DATE_PATTERN); } /** * 日期格式化 日期格式為:yyyy-MM-dd * @param date 日期 * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN *
@return 返回yyyy-MM-dd格式日期 */ public static String format(Date date, String pattern) { if(date != null){ SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } return null; } /** * 字串轉換成日期 * @param strDate 日期字串 * @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN */ public static Date stringToDate(String strDate, String pattern) { if (StringUtils.isBlank(strDate)){ return null; } DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); return fmt.parseLocalDateTime(strDate).toDate(); } /** * 根據週數,獲取開始日期、結束日期 * @param week 週期 0本週,-1上週,-2上上週,1下週,2下下週 * @return 返回date[0]開始日期、date[1]結束日期 */ public static Date[] getWeekStartAndEnd(int week) { DateTime dateTime = new DateTime(); LocalDate date = new LocalDate(dateTime.plusWeeks(week)); date = date.dayOfWeek().withMinimumValue(); Date beginDate = date.toDate(); Date endDate = date.plusDays(6).toDate(); return new Date[]{beginDate, endDate}; } /** * 對日期的【秒】進行加/減 * * @param date 日期 * @param seconds 秒數,負數為減 * @return 加/減幾秒後的日期 */ public static Date addDateSeconds(Date date, int seconds) { DateTime dateTime = new DateTime(date); return dateTime.plusSeconds(seconds).toDate(); } /** * 對日期的【分鐘】進行加/減 * * @param date 日期(當前時間) * @param minutes 分鐘數,負數為減 * @return 加/減幾分鐘後的日期 */ public static Date addDateMinutes(Date date, int minutes) { DateTime dateTime = new DateTime(date); return dateTime.plusMinutes(minutes).toDate(); } /** * 對日期的【小時】進行加/減 * * @param date 日期 * @param hours 小時數,負數為減 * @return 加/減幾小時後的日期 */ public static Date addDateHours(Date date, int hours) { DateTime dateTime = new DateTime(date); return dateTime.plusHours(hours).toDate(); } /** * 對日期的【天】進行加/減 * * @param date 日期 * @param days 天數,負數為減 * @return 加/減幾天後的日期 */ public static Date addDateDays(Date date, int days) { DateTime dateTime = new DateTime(date); return dateTime.plusDays(days).toDate(); } /** * 對日期的【周】進行加/減 * * @param date 日期 * @param weeks 週數,負數為減 * @return 加/減幾周後的日期 */ public static Date addDateWeeks(Date date, int weeks) { DateTime dateTime = new DateTime(date); return dateTime.plusWeeks(weeks).toDate(); } /** * 對日期的【月】進行加/減 * * @param date 日期 * @param months 月數,負數為減 * @return 加/減幾月後的日期 */ public static Date addDateMonths(Date date, int months) { DateTime dateTime = new DateTime(date); return dateTime.plusMonths(months).toDate(); } /** * 對日期的【年】進行加/減 * * @param date 日期 * @param years 年數,負數為減 * @return 加/減幾年後的日期 */ public static Date addDateYears(Date date, int years) { DateTime dateTime = new DateTime(date); return dateTime.plusYears(years).toDate(); } }
一個小小後端的爬行痕跡