package com.kld.yijie.web.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
/**
* Created by shanks on 15/4/21.
*/
public class DateUtil {
private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
private static String defaultDatePattern = "yyyy-MM-dd";
public static String secondDatePattern = "yyyy-MM-dd HH:mm:ss";
/**
* 格式:yyyy-MM-dd
*/
public static final int FMT_DATE_YYYY_MM_DD = 1;
/**
* 格式:yyyyMMdd
*/
public static final int FMT_DATE_YYYYMMDD = 2;
/**
* 格式:yyMMdd
*/
public static final int FMT_DATE_YYMMDD = 3;
/**
* 格式:yyyy
*/
public static final int FMT_DATE_YYYY = 4;
/**
* 格式:yyMM
*/
public static final int FMT_DATE_YYMM = 5;
/**
* 格式:yyyyMM
*/
public static final int FMT_DATE_YYYYMM = 6;
/**
* 格式:yyyy-MM-dd HH:mm:ss
*/
public static final int FMT_DATE_YYYYMMDDHHmmss = 7;
/**
* 格式:yyyyMMddHHmmss
*/
public static final int FMT_DATE_YYYYMMDDHHmmss1 = 8;
/**
* 格式:yyyy/MM/dd
*/
public static final int FMT_DATE_YYYYMMDD9 = 9;
/**
* 格式:HH:mm:ss
*/
public static final int FMT_DATE_HHMMSS = 10;
/**
* 格式:yyyy年MM月dd日
*/
public static final int FMT_DATE_YYYY_MM_DD_CN = 11;
/**
* 格式:yyyy年MM月dd日 HH时mm分ss秒
*/
public static final int FMT_DATE_YYYYMMDDHHmmss_CN = 12;
/**
* 格式:yyyy年MM月
*/
public static final int FMT_DATE_YYYY_MM_CN = 13;
/**
* 格式:yyyy-MM-dd HH:mm
*/
public static final int FMT_DATE_YYYY_MM_DDHHMM = 14;
/**
* 获得默认的 date pattern
*/
public static String getDatePattern() {
return defaultDatePattern;
}
/**
* 使用参数Format格式化Date成字符串
*/
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 根椐时间和格式获得时间
*
* @return String
*/
public static String formatDate(Date date, int nFmt) {
if (date == null) {
return "";
}
SimpleDateFormat fmtDate = null;
switch (nFmt) {
default:
case DateUtil.FMT_DATE_YYYY_MM_DD:
fmtDate = new SimpleDateFormat("yyyy-MM-dd");
break;
case DateUtil.FMT_DATE_YYYYMMDD:
fmtDate = new SimpleDateFormat("yyyyMMdd");
break;
case DateUtil.FMT_DATE_YYMMDD:
fmtDate = new SimpleDateFormat("yyMMdd");
break;
case DateUtil.FMT_DATE_YYYY:
fmtDate = new SimpleDateFormat("yyyy");
break;
case DateUtil.FMT_DATE_YYMM:
fmtDate = new SimpleDateFormat("yyMM");
break;
case DateUtil.FMT_DATE_YYYYMM:
fmtDate = new SimpleDateFormat("yyyyMM");
break;
case DateUtil.FMT_DATE_YYYYMMDDHHmmss:
fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case DateUtil.FMT_DATE_YYYYMMDDHHmmss1:
fmtDate = new SimpleDateFormat("yyyyMMddHHmmss");
break;
case DateUtil.FMT_DATE_YYYYMMDD9:
fmtDate = new SimpleDateFormat("yyyy/MM/dd");
break;
case DateUtil.FMT_DATE_HHMMSS:
fmtDate = new SimpleDateFormat("HH:mm:ss");
break;
case DateUtil.FMT_DATE_YYYY_MM_DD_CN:
fmtDate = new SimpleDateFormat("yyyy年MM月dd日");
break;
case DateUtil.FMT_DATE_YYYYMMDDHHmmss_CN:
fmtDate = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
break;
case DateUtil.FMT_DATE_YYYY_MM_CN:
fmtDate = new SimpleDateFormat("yyyy年MM月");
break;
case DateUtil.FMT_DATE_YYYY_MM_DDHHMM:
fmtDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:");
break;
}
return fmtDate.format(date);
}
/**
* 使用预设格式将字符串转为Date
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* 使用参数Format将字符串转为Date
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
logger.error(e.getMessage());
return null;
}
}
/**
* 在日期上增加数个整月
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* 获得给定日期当前月份的第一天的日期
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
/**
* 获得给定日期当前月份的最后一天的日期
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
Date resultDate = getFirstDayOfMonth(addMonth(date, 1));
Calendar calendar = Calendar.getInstance();
calendar.setTime(resultDate);
calendar.add(Calendar.DAY_OF_YEAR, -1);
return calendar.getTime();
}
/**
* 得到系统当前的日期和时间 格式为YYYY-MM-DD hh:mm:ss
* @return
*/
public static final String getSystemCurrentDateTime() {
Calendar newCalendar = Calendar.getInstance();
newCalendar.setTimeInMillis(System.currentTimeMillis());
int year = newCalendar.get(Calendar.YEAR);
int month = newCalendar.get(Calendar.MONTH) + 1;
int day = newCalendar.get(Calendar.DAY_OF_MONTH);
int hour = newCalendar.get(Calendar.HOUR_OF_DAY);
int minute = newCalendar.get(Calendar.MINUTE);
int second = newCalendar.get(Calendar.SECOND);
return doTransform(year, month, day, hour, minute, second);
}
/**
* 返回格式为YYYY-MM-DD hh:mm:ss
*
* @param year-年
* @param month-月
* @param day-日
* @param hour-小时
* @param minute-分钟
* @param second-秒
* @return
*/
private static final String doTransform(int year, int month, int day, int hour, int minute, int second) {
StringBuffer result = new StringBuffer();
result.append(String.valueOf(year)).append("-").append(month < 10 ? "0" + String.valueOf(month) : String.valueOf(month)).append("-").append(day < 10 ? "0" + String.valueOf(day) : String.valueOf(day)).append(" ").append(hour < 10 ? "0" + String.valueOf(hour) : String.valueOf(hour)).append(
":").append(minute < 10 ? "0" + String.valueOf(minute) : String.valueOf(minute)).append(":").append(second < 10 ? "0" + String.valueOf(second) : String.valueOf(second));
return result.toString();
}
/**
* 根据转换格式,把Timestamp日期转换为字符串日期
* @param Timestamp
* @param nFmt
* @return
*/
public static String timestampToString(Timestamp Timestamp, int nFmt) {
if (Timestamp == null) {
return "";
}
java.util.Date d = new java.util.Date(Timestamp.getTime());
return formatDate(d, nFmt);
}
/**
* 根椐系统时间和格式获得时间
*
* @param nFmt
* @return
*/
public static String getSystemDate(int nFmt) {
Calendar calendar = Calendar.getInstance();
return formatDate(calendar.getTime(), nFmt);
}
/**
* 使用预设Format格式化Date成字符串
*/
public static String format(Date date) {
return format(date, getDatePattern());
}
/**
* 返回预设Format的当前日期字符串
*/
public static String getToday() {
Date today = new Date();
return format(today);
}
/**
* 返回量日期相差的天数
* date1:起始时间;date2截止时间;systemDate 系统当前时间
* @throws ParseException
* */
public static String getSubDays(String stratDate) throws ParseException{
long day;
Date strDate = new SimpleDateFormat("yyyy-MM-dd").parse(stratDate);
Date sysDate = new SimpleDateFormat("yyyy-MM-dd").parse(getSystemDate(1));
day=(strDate.getTime()-sysDate.getTime())/(24*60*60*1000);
return String.valueOf(day);
}
/**
* ORA标准时间格式
*/
private static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
/**
* 带时分秒的ORA标准时间格式
*
*/
private static final SimpleDateFormat ORA_DATE_TIME_EXTENDED_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
/**
* 创建一个标准ORA时间格式的克隆
*
* @return 标准ORA时间格式的克隆
*/
@SuppressWarnings("unused")
private static synchronized DateFormat getOraDateTimeFormat() {
SimpleDateFormat theDateTimeFormat = (SimpleDateFormat) ORA_DATE_TIME_FORMAT.clone();
theDateTimeFormat.setLenient(false);
return theDateTimeFormat;
}
/**
* 创建一个带分秒的ORA时间格式的克隆
*
* @return 标准ORA时间格式的克隆
*/
private static synchronized DateFormat getOraExtendDateTimeFormat() {
SimpleDateFormat theDateTimeFormat = (SimpleDateFormat) ORA_DATE_TIME_EXTENDED_FORMAT.clone();
theDateTimeFormat.setLenient(false);
return theDateTimeFormat;
}
/**
* 返回格式为YYYY-MM-DD hh:mm:ss
*
* @param date-输入格式为ORA标准时间格式
* @return
*/
private static String doTransform(String date) {
StringBuffer buffer = new StringBuffer();
buffer.append(date.substring(0, 4));
buffer.append("-");
buffer.append(date.substring(4, 6));
buffer.append("-");
buffer.append(date.substring(6, 8));
buffer.append(" ");
buffer.append(date.substring(8, 10));
buffer.append(":");
buffer.append(date.substring(10, 12));
buffer.append(":");
buffer.append(date.substring(12, 14));
return buffer.toString();
}
/**
* 将输入格式为2004-8-13 12:31:22类型的字符串转换为标准的Date类型
*
* @param dateStr
* @return
*/
public static synchronized Date toDate(String dateStr) {
String[] list0 = dateStr.split(" ");
String date = list0[0];
String time = list0[1];
String[] list1 = date.split("-");
int year = new Integer(list1[0]).intValue();
int month = new Integer(list1[1]).intValue();
int day = new Integer(list1[2]).intValue();
String[] list2 = time.split(":");
int hour = new Integer(list2[0]).intValue();
int min = new Integer(list2[1]).intValue();
int second = new Integer(list2[2]).intValue();
Calendar cale = Calendar.getInstance();
cale.set(year, month - 1, day, hour, min, second);
return cale.getTime();
}
public static String dateToStringFirst(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
String dateToStr = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + "01" + " 00:00:00";
return dateToStr;
}
public static String dateToStringLast(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
String dateToStr = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.getActualMaximum(Calendar.DAY_OF_MONTH) + " 23:59:59";
return dateToStr;
}
/*
*获取当天是本年第几周
*/
public static int getCurrentWeek(){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(new Date());
return calendar.get(Calendar.WEEK_OF_YEAR)-2;
}
public static int getStartDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
return Integer.parseInt(df.format(date));
}
public static int getendDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
return Integer.parseInt(df.format(date));
}
//获取当前日期的字符串表达式
public static String getCurrentDate(){
Calendar calendar = Calendar.getInstance();
Date date=calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyMMdd");
String sdate=df.format(date);
return sdate;
}
//获取当前日期的字符串表达式
public static String getCurrentDateFormat(String format){
Calendar calendar = Calendar.getInstance();
Date date=calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat(format);
String sdate=df.format(date);
return sdate;
}
public static String getCurrentDate2(){
Calendar calendar = Calendar.getInstance();
Date date=calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String sdate=df.format(date);
return sdate;
}
//获取当前日期的字符串表达式
public static Date getDate(){
Calendar calendar = Calendar.getInstance();
Date date=calendar.getTime();
return date;
}
//指定日期格式与日期参数,格式化日期
public static Date formatDate(String format,Date date){
SimpleDateFormat df = new SimpleDateFormat(format);
String d=df.format(date);
try {
date = df.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//指定日期格式参数,给定字符串的日期参数,转换成该格式的日期
public static Date toDate(String format,String time){
SimpleDateFormat df = new SimpleDateFormat(format);
System.out.println(format);
Date date=null;
if(time==null||"".equals(time)){
return null;
}
try {
date = df.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//根据指定时间与时间分量、时间分量值计算日期
public static Date countDate(String unit,int value,Date date){
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
if("year".equals(unit)){
calendar.set(calendar.YEAR, calendar.get(calendar.YEAR)+value);
}else if("month".equals(unit)){
calendar.set(calendar.MONTH,calendar.get(calendar.MONTH)+value);
}else if("day".equals(unit)){
calendar.set(calendar.DAY_OF_MONTH,calendar.get(calendar.DAY_OF_MONTH)+value);
}else if("hour".equals(unit)){
calendar.set(calendar.HOUR_OF_DAY,value);
}else if("minute".equals(unit)){
calendar.set(calendar.MINUTE,calendar.get(calendar.MINUTE)+value);
}else if("second".equals(unit)){
calendar.set(calendar.SECOND,value);
}
return calendar.getTime();
}
//根据指定时间与时间分量、时间分量值计算日期
public static Date operDate(String operObj, Integer operNum, Date operDate) {
Date date = null;
try {
Calendar calendar = Calendar.getInstance();
if (operDate != null) {
calendar.setTime(operDate);
} else {
calendar.setTime(new Date());
}
if (operObj.equals("year")) {
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + operNum.intValue());
} else if (operObj.equals("month")) {
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month + operNum.intValue());
} else if (operObj.equals("day")) {
int day = calendar.get(Calendar.DAY_OF_YEAR);
calendar.set(Calendar.DAY_OF_YEAR, day + operNum.intValue());
} else if (operObj.equals("hour")) {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
calendar.set(Calendar.HOUR_OF_DAY, hour + operNum.intValue());
} else if (operObj.equals("minute")) {
int hour = calendar.get(Calendar.MINUTE);
calendar.set(Calendar.MINUTE, hour + operNum.intValue());
}
date = calendar.getTime();
} catch (Exception e) {
}
return date;
}
//根据时间分量计算以后时间
public static Date countDate(String unit,Date date){
if(unit.equals("年卡")){
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.YEAR, calendar.get(calendar.YEAR)+1);
System.out.println("执行了年卡");
return calendar.getTime();
}else if(unit.equals("季卡")){
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.MONTH, calendar.get(calendar.MONTH)+3);
System.out.println("执行了季卡");
return calendar.getTime();
}else{
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.MONTH, calendar.get(calendar.MONTH)+1);
System.out.println("执行了月卡");
return calendar.getTime();
}
}
//对日期天数的计算
public static Date countDay(int day,Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.DAY_OF_MONTH, calendar.get(calendar.DAY_OF_MONTH) + day);
return calendar.getTime();
}
//根据当前时间计算当前的月份
public static int getCurrentMoth(){
Calendar calendar =Calendar.getInstance();
return calendar.get(calendar.MONTH)+1;
}
//根据指定时间计算指定时间的月份
public static int getMonthByDate(Date date){
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
return calendar.get(calendar.MONTH)+1;
}
//根据月份返回对应的中文,比如1返回一月
public static String getChineseMonth(int num){
switch (num){
case 1:return "一月";
case 2:return "二月";
case 3:return "三月";
case 4:return "四月";
case 5:return "五月";
case 6:return "六月";
case 7:return "七月";
case 8:return "八月";
case 9:return "九月";
case 10:return "十月";
case 11:return "十一月";
default :return "十二月";
}
}
//根据参数计算日期
public static Date countDateByParam(int num){
Calendar calendar =Calendar.getInstance();
calendar.add(Calendar.MONTH,num);
//calendar.set(Calendar.MONTH.MONTH,num);
Date date=calendar.getTime();
System.out.println(toString(date,"yyyy-MM-dd HH:mm:ss"));
return calendar.getTime();
}
//计算指定日期的月份的第一天
public static Date countDateFirstDay(Date date){
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
final int firstDay=calendar.getActualMinimum(calendar.DAY_OF_MONTH);
Date firstDate=calendar.getTime();
firstDate.setDate(firstDay);
return firstDate;
}
//计算指定日期的月份的最后一天
public static Date countDateLastDay(Date date){
Calendar calendar =Calendar.getInstance();
calendar.setTime(date);
final int lastDay=calendar.getActualMaximum(calendar.DAY_OF_MONTH);
Date lastDate=calendar.getTime();
lastDate.setDate(lastDay);
return lastDate;
}
//根据月份计算当前是第几季度
public static int getCurrentQuarter(int month){
int quarter;
if(month%3==0){
quarter=month/3;
}else{
quarter=month/3+1;
}
return quarter;
}
//根据月份与消费需求类型(季2、月3、周4)计算日期范围
public static HashMap<String,Date> getStartAndEndTime(int consumeType){
int month= DateUtil.getCurrentMoth();
Calendar calendar =Calendar.getInstance();
//calendar.setTime(new Date());
HashMap<String,Date> time=new HashMap<String,Date>();
Date startTime;
Date endTime;
if(consumeType==2){
int quarter=getCurrentQuarter(month);
if(quarter==1){
startTime=calendar.getTime();
calendar.set(calendar.get(Calendar.YEAR),0,1);
calendar.set(calendar.MONTH,2);
int lastDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR),2,lastDay);
endTime=calendar.getTime();
time.put("startTime",startTime);
time.put("endTime",endTime);
}else if(quarter==2){
calendar.set(calendar.get(Calendar.YEAR),3,1);
startTime=calendar.getTime();
calendar.set(calendar.MONTH,5);
int lastDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR),5,lastDay);
endTime=calendar.getTime();
time.put("startTime", startTime);
time.put("endTime",endTime);
}else if(quarter==3){
calendar.set(calendar.get(Calendar.YEAR),6,1);
startTime=calendar.getTime();
calendar.set(calendar.MONTH,8);
int lastDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR),8,lastDay);
endTime=calendar.getTime();
time.put("startTime", startTime);
time.put("endTime",endTime);
}else if(quarter==4){
calendar.set(calendar.get(Calendar.YEAR),9,1);
startTime=calendar.getTime();
calendar.set(calendar.MONTH,11);
int lastDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR),11,lastDay);
endTime=calendar.getTime();
time.put("startTime", startTime);
time.put("endTime",endTime);
}
}else if(consumeType==3){
calendar.set(calendar.get(Calendar.YEAR),month-1,1);
startTime=calendar.getTime();
int lastDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(calendar.get(Calendar.YEAR),month-1,lastDay);
endTime=calendar.getTime();
time.put("startTime", startTime);
time.put("endTime",endTime);
}else if(consumeType==4){
int week=calendar.get(Calendar.DAY_OF_WEEK)-1;
System.out.println("week:"+week);
calendar.add(Calendar.DAY_OF_MONTH, -week+1);
startTime=calendar.getTime();
Calendar c=Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, 7-week);
endTime=c.getTime();
time.put("startTime", startTime);
time.put("endTime",endTime);
}
return time;
}
public static String toString(Date date, String formatString){
if(null == date)return null;
SimpleDateFormat df = new SimpleDateFormat(formatString);
return df.format(date);
}
}