package com.hengtong.boxcode.utils.tool;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import com.boxcode.utils.exception.BizException;
/**
* 日期处理
*
* @author admin
*
*/
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";
/** 时间格式(MM-dd) */
public final static String DATE_MD_PATTERN = "MMdd";
/** 时间格式(yy-MM) */
public final static String DATE_YM_PATTERN = "yyMM";
/** 时间格式(yy) */
public final static String DATE_YY_PATTERN = "yy";
/** 时间格式(yyyy) */
public final static String DATE_YYYY_PATTERN = "yyyy";
/** 时间格式(HH:mm) */
public final static String TIME_HM_PATTERN = "HH:mm";
/**
* 日期格式化
*
* @param date 日期,format需要格式化的格式
* @return 返回format格式日期
*/
public static String format(Date date,String format) {
return format(date, format);
}
/**
* 日期格式化
*
* @param date 日期
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
* @return 返回格式日期
*/
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 (strDate == null || "".endsWith(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();
}
public static Date getCurrStart(Date date) {
String currYmd = format(date, DATE_PATTERN);
return stringToDate(currYmd + " 00:00:00", DATE_TIME_PATTERN);
}
public static Date getCurrStart(String dateYmd) {
return stringToDate(dateYmd + " 00:00:00", DATE_TIME_PATTERN);
}
public static Date getCurrEnd(Date date) {
String currYmd = format(date, DATE_PATTERN);
return stringToDate(currYmd + " 23:59:59", DATE_TIME_PATTERN);
}
public static Date getCurrEnd(String dateYmd) {
return stringToDate(dateYmd + " 23:59:59", DATE_TIME_PATTERN);
}
/**
* 时间处理
* @param oldDate
* @return
*/
public static String dealDateFormat(String oldDate) {
Date date1 = null;
DateFormat df2 = null;
try {
oldDate= oldDate.replace("Z", " UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date date = df.parse(oldDate);
SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
date1 = df1.parse(date.toString());
df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
e.printStackTrace();
}
return df2.format(date1);
}
/**
* 获取昨天时间
* @return
*/
public static String yesterdayDate() {
DateFormat dateFormat=new SimpleDateFormat(DATE_PATTERN);
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,-24);
return dateFormat.format(calendar.getTime());
}
/**
* 获取i天前时间
* @return
*/
public static String sevendayDate(int i) {
DateFormat dateFormat=new SimpleDateFormat(DATE_PATTERN);
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,-24*i);
return dateFormat.format(calendar.getTime());
}
/**
* 获取当天时间
* @return
*/
public static String todayDate() {
DateFormat dateFormat=new SimpleDateFormat(DATE_PATTERN);
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,0);
return dateFormat.format(calendar.getTime());
}
public static void judgmentDate(String date1, String date2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
Date start;
try {
start = sdf.parse(date1);
Date end = sdf.parse(date2);
long cha = end.getTime() - start.getTime();
if(cha<0){
throw new BizException("查询时间不允许相同!");
}
double result = cha * 1.0 / (1000 * 60 * 60);
if(result<=24){
}else{
throw new BizException("查询时间段不允许超过24小时!");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* 时间处理
* @param oldDate
* @return
*/
public static String dealDateFormat2(String oldDate) {
Date date1 = null;
DateFormat df2 = null;
try {
oldDate= oldDate.replace("Z", " UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date date = df.parse(oldDate);
SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
date1 = df1.parse(date.toString());
df2 = new SimpleDateFormat("yyyy-MM-dd");
} catch (ParseException e) {
e.printStackTrace();
}
return df2.format(date1);
}
}
package com.hengtong.boxcode.utils.exception;
public class BizException extends RuntimeException {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 5373675591332073697L;
// 异常信息
private String msg;
private int code = 500;
public BizException(String msg) {
super(msg);
this.msg = msg;
}
public BizException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public BizException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public BizException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}