123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- /**
- * Copyright (c) 2016-2019 人人开源 All rights reserved.
- * <p>
- * https://www.renren.io
- * <p>
- * 版权所有,侵权必究!
- */
- 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;
- import java.util.regex.Pattern;
- /**
- * 日期处理
- *
- * @author Mark 735032128@qq.com
- */
- 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();
- }
- /**
- * 常规自动日期格式识别
- * @param str 时间字符串
- * @return Date
- * @author dc
- */
- public static String getDateFormat(String str) {
- boolean year = false;
- Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
- if (pattern.matcher(str.substring(0, 4)).matches()) {
- year = true;
- }
- StringBuilder sb = new StringBuilder();
- int index = 0;
- if (!year) {
- if (str.contains("月") || str.contains("-") || str.contains("/") || str.contains(".")) {
- if (Character.isDigit(str.charAt(0))) {
- index = 1;
- }
- } else {
- index = 3;
- }
- }
- for (int i = 0; i < str.length(); i++) {
- char chr = str.charAt(i);
- if (Character.isDigit(chr)) {
- if (index == 0) {
- sb.append("y");
- }
- if (index == 1) {
- sb.append("M");
- }
- if (index == 2) {
- sb.append("d");
- }
- if (index == 3) {
- sb.append("H");
- }
- if (index == 4) {
- sb.append("m");
- }
- if (index == 5) {
- sb.append("s");
- }
- if (index == 6) {
- sb.append("S");
- }
- } else {
- if (i > 0) {
- char lastChar = str.charAt(i - 1);
- if (Character.isDigit(lastChar)) {
- index++;
- }
- }
- sb.append(chr);
- }
- }
- return sb.toString();
- }
- }
|