package com.ijx.datasync.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DateUtil {
private static Logger log = LoggerFactory.getLogger(DateUtil.class);
public final static String LONGEST_PATTERN="yyMMddHHmmssSSS";
/**
* 将日期格式化为给定格式的字符串
* @param date
* @param pattern
* @return
*/
public static String formatDateToString(Date date, String pattern) {
if (null == date) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* 将字符串转换为日期
* @param aMask
* @param strDate
* @return
* @throws ParseException
*/
public static final Date convertStringToDate(String pattern, String strDate){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
if(strDate != null && !"".equals(strDate)) {
return sdf.parse(strDate);
} else {
return null;
}
} catch (ParseException e) {
log.error("日期字符串解析异常",e);
return null;
}
}
/**
* 获取当前时间(精确到秒)
* @return 当前时间
*/
public static String getThisSecondTime() {
Calendar calendar = Calendar.getInstance();
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss");
return formatter.format(currentTime);
}
/**
* 获取当前时间(精确到年月日时分秒)
* @return
*/
public static String getCurrentDayTime(){
Calendar calendar = Calendar.getInstance();
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
return formatter.format(currentTime);
}
/**
* 获取当前时间是0-上午还是1-下午
* @return 当前时间
*/
public static int getCurrentMeridiem() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.AM_PM);
}
/**
* 获取当天时间
* @return
*/
public static String getCurrentDay() {
Calendar calendar = Calendar.getInstance();
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(currentTime);
}
/**
* 获取昨天时间 字符串
* @return
*/
public static String getPreDay() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
Date preTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(preTime);
}
/**
* 获取间隔为n天的时间
* @return
*/
public static String getPreNDay(int n) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, n);
Date preTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(preTime);
}
/**
* 获取当前日期的前6天
* @param aMask
* @param date
* @return
*/
public static final String getCurrentBeforeWeek() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, -6);
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(currentTime);
}
/**
* 设置开始时间为当天5点
* @return
*/
public static String getSpeedStartime() {
StringBuffer str = new StringBuffer();
Calendar calendar = Calendar.getInstance();
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
str.append(formatter.format(currentTime));
str.append(" 09:00");
return str.toString();
}
/**
* 设置开始时间为当天5点
* @return
*/
public static String getSpeedEndime() {
StringBuffer str = new StringBuffer();
Calendar calendar = Calendar.getInstance();
Date currentTime = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
str.append(formatter.format(currentTime));
str.append(" 17:00");
return str.toString();
}
/**
* 获取当前月第一天 小时分钟为00:00:00
**/
public static String getMonthFirstDay() {
StringBuffer str = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
str.append(sdf.format(lastDate.getTime()));
str.append(" 00:00:00");
return str.toString();
}
/**
* 获取当前月最后一天 小时分钟为23:59:59
**/
public static String getMonthLastDay() {
StringBuffer str = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
str.append(sdf.format(lastDate.getTime()));
str.append(" 23:59:59");
return str.toString();
}
/**
* 获取当前年第一天 小时分钟为00:00:00
**/
public static String getCurrentYearFirst() {
StringBuffer str = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
str.append(sdf.format(cd.getTime()));
str.append(" 00:00:00");
return str.toString();
}
/**
* 获取当前年最后一天 小时分钟为23:59:59
**/
public static String getCurrentYearLast() {
StringBuffer str = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
cd.add(Calendar.YEAR, 1);
cd.add(Calendar.DATE, -1);
str.append(sdf.format(cd.getTime()));
str.append(" 23:59:59");
return str.toString();
}
/**
* 根据日期获取季度
* @param cal
* @return
*/
public static int getSeason() {
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
return (month + 1 + 2) / 3;
}
/**
* 根据获取季度的第一天 小时分钟为00:00:00
* @param cal
* @return
*/
public static String getSeasonFirstDay(int i) {
StringBuffer str = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
if (i == 1) {
i = 0;
} else if (i == 2) {
i = 3;
} else if (i == 3) {
i = 6;
} else if (i == 4) {
i = 9;
}
cal.set(Calendar.MONTH, i);// 设为季度开始月
cal.set(Calendar.DATE, 1);
str.append(sdf.format(cal.getTime()));
str.append(" 00:00:00");
return str.toString();
}
/**
* 根据获取季度的最后一天
* @param cal
* @return
*/
public static String getSeasonLastDay(int i) {
StringBuffer str = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
if (i == 1) {
i = 2;
} else if (i == 2) {
i = 5;
} else if (i == 3) {
i = 8;
} else if (i == 4) {
i = 11;
}
cal.set(Calendar.MONTH, i);// 设为季度开始月
cal.set(Calendar.DATE, 1);// 设季度开始月的第一天
cal.add(Calendar.MONTH, 1);// 当前季度最后一月的下一月
cal.add(Calendar.DATE, -1);// 当前季度最后一月的最后一天
str.append(sdf.format(cal.getTime()));
str.append(" 23:59:59");
return str.toString();
}
/**
* 获取当前月
* @param cal
* @return
*/
public static String getMonth() {
SimpleDateFormat sdf = new SimpleDat