JAVA日期工具类
public class DateUtil {
private static final ThreadLocal< SimpleDateFormat> threadLocal = new ThreadLocal < SimpleDateFormat> ( ) ;
private static final Object object = new Object ( ) ;
public static String getDate ( ) {
SimpleDateFormat ft= new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date dd = new Date ( ) ;
return ft. format ( dd) ;
}
public static String getCurrentTime ( ) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
return format. format ( new Date ( ) ) ;
}
public static String getCurrentTime ( String dateType) {
SimpleDateFormat df = new SimpleDateFormat ( dateType) ;
return df. format ( new Date ( ) ) ;
}
public static String getDayForWeek ( ) {
Date date= new Date ( ) ;
SimpleDateFormat dateFm = new SimpleDateFormat ( "EEEE" ) ;
return dateFm. format ( date) ;
}
public static Date convertStringToDate ( String dateString) throws ParseException{
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
return format. parse ( dateString) ;
}
public static Date stringToDate ( String date, String pattern) {
Date myDate = null;
if ( date != null) {
try {
myDate = getDateFormat ( pattern) . parse ( date) ;
} catch ( Exception e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
}
return myDate;
}
public static String dateToString ( Date date) {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
String str = format. format ( date) ;
return str;
}
private static SimpleDateFormat getDateFormat ( String pattern) throws RuntimeException {
SimpleDateFormat dateFormat = threadLocal. get ( ) ;
if ( dateFormat == null) {
synchronized ( object) {
if ( dateFormat == null) {
dateFormat = new SimpleDateFormat ( pattern) ;
dateFormat. setLenient ( false ) ;
threadLocal. set ( dateFormat) ;
}
}
}
dateFormat. applyPattern ( pattern) ;
return dateFormat;
}
public static int getDayOfYear ( ) {
Calendar cal = Calendar. getInstance ( ) ;
cal. setTime ( new Date ( ) ) ;
return cal. get ( Calendar. DAY_OF_YEAR) ;
}
public static int getWeekOfYear ( ) {
Calendar cal = Calendar. getInstance ( ) ;
cal. setTime ( new Date ( ) ) ;
return cal. get ( Calendar. WEEK_OF_YEAR) ;
}
public static int getYear ( String date, String pattern) {
return getYear ( stringToDate ( date, pattern) ) ;
}
public static int getYear ( Date date) {
return getInteger ( date, Calendar. YEAR) ;
}
public static int getMonth ( String date, String pattern) {
return getMonth ( stringToDate ( date, pattern) ) ;
}
public static int getMonth ( Date date) {
return getInteger ( date, Calendar. MONTH) + 1 ;
}
public static int getDay ( String date, String pattern) {
return getDay ( stringToDate ( date, pattern) ) ;
}
public static int getDay ( Date date) {
return getInteger ( date, Calendar. DATE) ;
}
public static String dayForWeek ( String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Calendar c = Calendar. getInstance ( ) ;
c. setTime ( format. parse ( pTime) ) ;
int dayForWeek = 0 ;
if ( c. get ( Calendar. DAY_OF_WEEK) == 1 ) {
dayForWeek = 7 ;
} else {
dayForWeek = c. get ( Calendar. DAY_OF_WEEK) - 1 ;
}
if ( dayForWeek== 1 ) {
return "周一" ;
} else if ( dayForWeek== 2 ) {
return "周二" ;
} else if ( dayForWeek== 3 ) {
return "周三" ;
} else if ( dayForWeek== 4 ) {
return "周四" ;
} else if ( dayForWeek== 5 ) {
return "周五" ;
} else if ( dayForWeek== 6 ) {
return "周六" ;
} else if ( dayForWeek== 7 ) {
return "周日" ;
}
return "" ;
}
public static int daysBetween ( Date date, Date otherDate) throws ParseException
{
SimpleDateFormat sdf= new SimpleDateFormat ( "yyyy-MM-dd" ) ;
date= sdf. parse ( sdf. format ( date) ) ;
otherDate= sdf. parse ( sdf. format ( otherDate) ) ;
Calendar cal = Calendar. getInstance ( ) ;
cal. setTime ( date) ;
long time1 = cal. getTimeInMillis ( ) ;
cal. setTime ( otherDate) ;
long time2 = cal. getTimeInMillis ( ) ;
long between_days= ( time2- time1) / ( 1000 * 3600 * 24 ) ;
return Integer. parseInt ( String. valueOf ( between_days) ) ;
}
public static long getQuot ( String time1, String time2) {
long quot = 0 ;
SimpleDateFormat ft = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
Date date1 = ft. parse ( time1 ) ;
Date date2 = ft. parse ( time2 ) ;
quot = date1. getTime ( ) - date2. getTime ( ) ;
quot = quot / 1000 / 60 / 60 / 24 ;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
return quot;
}
public static int daysBetween ( String date, String otherDate) throws ParseException{
String eL = "^(\\d{4})-([0-1]\\d)-([0-3]\\d)\\s([0-5]\\d):([0-5]\\d):([0-5]\\d)$" ;
Pattern pattern = Pattern. compile ( eL) ;
Matcher mDate1 = pattern. matcher ( date) ;
boolean dateFlag1 = mDate1. matches ( ) ;
Matcher mOtherDate1 = pattern. matcher ( otherDate) ;
boolean otherDateFlag1 = mOtherDate1. matches ( ) ;
String eLL = "(19|20)[0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ;
Pattern pattern1 = Pattern. compile ( eLL) ;
Matcher mDate2 = pattern1. matcher ( date) ;
boolean dateFlag2 = mDate2. matches ( ) ;
Matcher mOtherDate2 = pattern1. matcher ( otherDate) ;
boolean otherDateFlag2 = mOtherDate2. matches ( ) ;
Calendar cal = Calendar. getInstance ( ) ;
if ( dateFlag2 && otherDateFlag2) {
cal. setTime ( stringToDate ( date+ " " + "00:00:00" , "yyyy-MM-dd HH:mm:ss" ) ) ;
long time1 = cal. getTimeInMillis ( ) ;
cal. setTime ( stringToDate ( otherDate+ " " + "00:00:00" , "yyyy-MM-dd HH:mm:ss" ) ) ;
long time2 = cal. getTimeInMillis ( ) ;
long between_days= ( time2- time1) / ( 1000 * 3600 * 24 ) ;
return Integer. parseInt ( String. valueOf ( between_days) ) ;
} else if ( dateFlag1 && otherDateFlag1) {
cal. setTime ( stringToDate ( date, "yyyy-MM-dd HH:mm:ss" ) ) ;
long time1 = cal. getTimeInMillis ( ) ;
cal. setTime ( stringToDate ( otherDate, "yyyy-MM-dd HH:mm:ss" ) ) ;
long time2 = cal. getTimeInMillis ( ) ;
long between_days= ( time2- time1) / ( 1000 * 3600 * 24 ) ;
return Integer. parseInt ( String. valueOf ( between_days) ) ;
} else {
Log4jUtil. error ( "日期格式不匹配" ) ;
return - 1 ;
}
}
private static Date addInteger ( Date date, int dateType, int amount) {
Date myDate = null;
if ( date != null) {
Calendar calendar = Calendar. getInstance ( ) ;
calendar. setTime ( date) ;
calendar. add ( dateType, amount) ;
myDate = calendar. getTime ( ) ;
}
return myDate;
}
private static int getInteger ( Date date, int dateType) {
int num = 0 ;
Calendar calendar = Calendar. getInstance ( ) ;
if ( date != null) {
calendar. setTime ( date) ;
num = calendar. get ( dateType) ;
}
return num;
}
public static Date addYear ( Date date, int yearAmount) {
return addInteger ( date, Calendar. YEAR, yearAmount) ;
}
public static Date addMonth ( Date date, int monthAmount) {
return addInteger ( date, Calendar. MONTH, monthAmount) ;
}
public static Date addDay ( Date date, int dayAmount) {
return addInteger ( date, Calendar. DATE, dayAmount) ;
}
public static Date addHour ( Date date, int hourAmount) {
return addInteger ( date, Calendar. HOUR_OF_DAY, hourAmount) ;
}
public static Date addMinute ( Date date, int minuteAmount) {
return addInteger ( date, Calendar. MINUTE, minuteAmount) ;
}
public static Date addSecond ( Date date, int secondAmount) {
return addInteger ( date, Calendar. SECOND, secondAmount) ;
}
public static String oracleDateToString ( String oracleDate) {
SimpleDateFormat sdf = new SimpleDateFormat ( "EEE MMM dd HH:mm:ss Z yyyy" , Locale. UK) ;
try {
Date date = sdf. parse ( oracleDate) ;
String dateString = dateToString ( date) ;
return dateString;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
return "" ;
}
public static String daysBetweenNowTime ( String dateTime) throws ParseException {
Date target_date = convertStringToDate ( dateTime) ;
Date current_date = new Date ( ) ;
String format = "yyyy-MM-dd hh:mm:ss" ;
List yearMonthDayList = new ArrayList ( ) ;
Calendar calendarTemp = Calendar. getInstance ( ) ;
calendarTemp. setTime ( target_date) ;
while ( calendarTemp. getTime ( ) . getTime ( ) <= current_date. getTime ( ) ) {
yearMonthDayList. add ( new SimpleDateFormat ( format)
. format ( calendarTemp. getTime ( ) ) ) ;
calendarTemp. add ( Calendar. DAY_OF_YEAR, 1 ) ;
}
Collections. sort ( yearMonthDayList) ;
int num = 0 ;
int size = yearMonthDayList. size ( ) ;
int week = 0 ;
for ( int i = 0 ; i < size; i++ ) {
String day= ( String) yearMonthDayList. get ( i) ;
try {
calendarTemp. setTime ( new SimpleDateFormat ( format) . parse ( day) ) ;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
int j = calendarTemp. get ( Calendar. DAY_OF_WEEK) ;
int value= j- 1 ;
week = value;
if ( week== 6 || week== 0 ) {
num++ ;
}
}
long between_Sec = current_date. getTime ( ) - target_date. getTime ( ) - num* 86400000 ;
long days = between_Sec / ( 1000 * 60 * 60 * 24 ) ;
long hours = ( between_Sec % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ;
long minutes = ( between_Sec % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ;
StringBuffer time_buffer = new StringBuffer ( ) ;
if ( days> 1 ) {
time_buffer. append ( days) . append ( "天" ) ;
}
if ( hours> 1 && days< 7 ) {
time_buffer. append ( hours) . append ( "小时" ) ;
}
if ( minutes> 1 && hours< 12 ) {
time_buffer. append ( minutes) . append ( "分" ) ;
}
return time_buffer. toString ( ) ;
}
public static String daysBetweenNowTime ( String startTime, String endTime) throws ParseException {
Date target_date = convertStringToDate ( startTime) ;
Date current_date = convertStringToDate ( endTime) ;
String format = "yyyy-MM-dd hh:mm:ss" ;
List yearMonthDayList = new ArrayList ( ) ;
Calendar calendarTemp = Calendar. getInstance ( ) ;
calendarTemp. setTime ( target_date) ;
while ( calendarTemp. getTime ( ) . getTime ( ) <= current_date. getTime ( ) ) {
yearMonthDayList. add ( new SimpleDateFormat ( format)
. format ( calendarTemp. getTime ( ) ) ) ;
calendarTemp. add ( Calendar. DAY_OF_YEAR, 1 ) ;
}
Collections. sort ( yearMonthDayList) ;
int num = 0 ;
int size = yearMonthDayList. size ( ) ;
int week = 0 ;
for ( int i = 0 ; i < size; i++ ) {
String day= ( String) yearMonthDayList. get ( i) ;
try {
calendarTemp. setTime ( new SimpleDateFormat ( format) . parse ( day) ) ;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
int j = calendarTemp. get ( Calendar. DAY_OF_WEEK) ;
int value= j- 1 ;
week = value;
if ( week== 6 || week== 0 ) {
num++ ;
}
}
long between_Sec = current_date. getTime ( ) - target_date. getTime ( ) - num* 86400000 ;
long days = between_Sec / ( 1000 * 60 * 60 * 24 ) ;
long hours = ( between_Sec % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ;
long minutes = ( between_Sec % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ;
StringBuffer time_buffer = new StringBuffer ( ) ;
if ( days> 1 ) {
time_buffer. append ( days) . append ( "天" ) ;
}
if ( hours> 1 && days< 7 ) {
time_buffer. append ( hours) . append ( "小时" ) ;
}
if ( minutes> 1 && hours< 12 ) {
time_buffer. append ( minutes) . append ( "分" ) ;
}
return time_buffer. toString ( ) ;
}
public static String betweenNowTime ( String dateTime, String patern) throws ParseException {
Date target_date = convertStringToDate ( dateTime) ;
int nowWeek= 0 ;
int addDay= 0 ;
nowWeek= getDayAndWeek ( target_date) ;
if ( nowWeek== 6 ) {
addDay= 2 ;
target_date = addDayTime ( target_date, addDay) ;
} else if ( nowWeek== 0 ) {
addDay= 1 ;
target_date = addDayTime ( target_date, addDay) ;
}
Date current_date = new Date ( ) ;
String format = "yyyy-MM-dd hh:mm:ss" ;
List yearMonthDayList = new ArrayList ( ) ;
Calendar calendarTemp = Calendar. getInstance ( ) ;
calendarTemp. setTime ( target_date) ;
while ( calendarTemp. getTime ( ) . getTime ( ) <= current_date. getTime ( ) ) {
yearMonthDayList. add ( new SimpleDateFormat ( format)
. format ( calendarTemp. getTime ( ) ) ) ;
calendarTemp. add ( Calendar. DAY_OF_YEAR, 1 ) ;
}
Collections. sort ( yearMonthDayList) ;
int num = 0 ;
int size = yearMonthDayList. size ( ) ;
int week = 0 ;
for ( int i = 0 ; i < size; i++ ) {
String day= ( String) yearMonthDayList. get ( i) ;
try {
calendarTemp. setTime ( new SimpleDateFormat ( format) . parse ( day) ) ;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
int j = calendarTemp. get ( Calendar. DAY_OF_WEEK) ;
int value= j- 1 ;
week = value;
if ( week== 6 || week== 0 ) {
num++ ;
}
}
long between_Sec = current_date. getTime ( ) - target_date. getTime ( ) - num* 86400000 ;
long days = between_Sec / ( 1000 * 60 * 60 * 24 ) ;
long hours = ( between_Sec % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ;
long minutes = ( between_Sec % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ;
String day= "" ;
String hour= "" ;
String minute= "" ;
String returns= "" ;
if ( days> 0 ) {
day= String. valueOf ( days) + "天" ;
}
if ( hours> 0 ) {
hour= String. valueOf ( hours) + "小时" ;
}
if ( minutes> 0 ) {
minute= String. valueOf ( minutes) + "分钟" ;
}
if ( patern. equals ( "dd" ) ) {
returns = day;
} else if ( patern. equals ( "hh" ) ) {
returns = day+ hour;
} else if ( patern. equals ( "mi" ) ) {
returns = day+ hour+ minute;
}
if ( StringUtil. isEmpty ( returns) ) {
returns = "0" ;
}
return returns;
}
private static int getDayAndWeek ( Date date) throws ParseException{
int week = 0 ;
Calendar calendarTemp = Calendar. getInstance ( ) ;
calendarTemp. setTime ( date) ;
int j = calendarTemp. get ( Calendar. DAY_OF_WEEK) ;
int value= j- 1 ;
week = value;
return week;
}
private static Date addDayTime ( Date date, int addDay) {
Date myDate = null;
if ( date != null) {
Calendar calendar = Calendar. getInstance ( ) ;
calendar. setTime ( date) ;
calendar. add ( Calendar. DAY_OF_MONTH, addDay) ;
myDate = calendar. getTime ( ) ;
myDate. setHours ( 0 ) ;
myDate. setMinutes ( 0 ) ;
myDate. setSeconds ( 0 ) ;
}
return myDate;
}
public static String betweenNowTime ( String startTime, String endTime, String patern) throws ParseException {
Date target_date = convertStringToDate ( startTime) ;
Date current_date = convertStringToDate ( endTime) ;
String format = "yyyy-MM-dd hh:mm:ss" ;
List yearMonthDayList = new ArrayList ( ) ;
Calendar calendarTemp = Calendar. getInstance ( ) ;
calendarTemp. setTime ( target_date) ;
while ( calendarTemp. getTime ( ) . getTime ( ) <= current_date. getTime ( ) ) {
yearMonthDayList. add ( new SimpleDateFormat ( format)
. format ( calendarTemp. getTime ( ) ) ) ;
calendarTemp. add ( Calendar. DAY_OF_YEAR, 1 ) ;
}
Collections. sort ( yearMonthDayList) ;
int num = 0 ;
int size = yearMonthDayList. size ( ) ;
int week = 0 ;
for ( int i = 0 ; i < size; i++ ) {
String day= ( String) yearMonthDayList. get ( i) ;
try {
calendarTemp. setTime ( new SimpleDateFormat ( format) . parse ( day) ) ;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
int j = calendarTemp. get ( Calendar. DAY_OF_WEEK) ;
int value= j- 1 ;
week = value;
if ( week== 6 || week== 0 ) {
num++ ;
}
}
long between_Sec = current_date. getTime ( ) - target_date. getTime ( ) - num* 86400000 ;
long days = between_Sec / ( 1000 * 60 * 60 * 24 ) ;
long hours = ( between_Sec % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ;
long minutes = ( between_Sec % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ;
if ( patern. equals ( "dd" ) ) {
return String. valueOf ( days) ;
} else if ( patern. equals ( "mi" ) ) {
return String. valueOf ( minutes) ;
} else {
return String. valueOf ( hours) ;
}
}
public static String timeBetweenTwoTime ( String startTime, String endTime) throws ParseException {
Date target_date = convertStringToDate ( startTime) ;
Date current_date = convertStringToDate ( endTime) ;
String format = "yyyy-MM-dd hh:mm:ss" ;
List yearMonthDayList = new ArrayList ( ) ;
Calendar calendarTemp = Calendar. getInstance ( ) ;
calendarTemp. setTime ( target_date) ;
while ( calendarTemp. getTime ( ) . getTime ( ) <= current_date. getTime ( ) ) {
yearMonthDayList. add ( new SimpleDateFormat ( format)
. format ( calendarTemp. getTime ( ) ) ) ;
calendarTemp. add ( Calendar. DAY_OF_YEAR, 1 ) ;
}
Collections. sort ( yearMonthDayList) ;
int num = 0 ;
int size = yearMonthDayList. size ( ) ;
int week = 0 ;
for ( int i = 0 ; i < size; i++ ) {
String day= ( String) yearMonthDayList. get ( i) ;
try {
calendarTemp. setTime ( new SimpleDateFormat ( format) . parse ( day) ) ;
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
int j = calendarTemp. get ( Calendar. DAY_OF_WEEK) ;
int value= j- 1 ;
week = value;
if ( week== 6 || week== 0 ) {
num++ ;
}
}
long between_Sec = current_date. getTime ( ) - target_date. getTime ( ) - num* 86400000 ;
long days = between_Sec / ( 1000 * 60 * 60 * 24 ) ;
long hours = ( between_Sec % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 ) ;
long minutes = ( between_Sec % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ;
long sencond = ( between_Sec % ( 1000 * 60 ) ) / ( 1000 ) ;
StringBuffer time_buffer = new StringBuffer ( ) ;
if ( hours> 1 ) {
time_buffer. append ( days* 24 + hours) . append ( "小时" ) ;
}
if ( minutes> 1 && hours< 12 ) {
time_buffer. append ( minutes) . append ( "分" ) ;
}
if ( sencond>= 0 && minutes< 60 ) {
time_buffer. append ( sencond) . append ( "秒" ) ;
}
return time_buffer. toString ( ) ;
}
public static int minsBetween ( String date, String otherDate) throws ParseException{
String eL = "^(\\d{4})-([0-1]\\d)-([0-3]\\d)\\s([0-5]\\d):([0-5]\\d):([0-5]\\d)$" ;
Pattern pattern = Pattern. compile ( eL) ;
Matcher mDate1 = pattern. matcher ( date) ;
boolean dateFlag1 = mDate1. matches ( ) ;
Matcher mOtherDate1 = pattern. matcher ( otherDate) ;
boolean otherDateFlag1 = mOtherDate1. matches ( ) ;
String eLL = "(19|20)[0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ;
Pattern pattern1 = Pattern. compile ( eLL) ;
Matcher mDate2 = pattern1. matcher ( date) ;
boolean dateFlag2 = mDate2. matches ( ) ;
Matcher mOtherDate2 = pattern1. matcher ( otherDate) ;
boolean otherDateFlag2 = mOtherDate2. matches ( ) ;
Calendar cal = Calendar. getInstance ( ) ;
if ( dateFlag2 && otherDateFlag2) {
cal. setTime ( stringToDate ( date+ " " + "00:00:00" , "yyyy-MM-dd HH:mm:ss" ) ) ;
long time1 = cal. getTimeInMillis ( ) ;
cal. setTime ( stringToDate ( otherDate+ " " + "00:00:00" , "yyyy-MM-dd HH:mm:ss" ) ) ;
long time2 = cal. getTimeInMillis ( ) ;
long between_days= ( time2- time1) / ( 1000 * 60 ) ;
return Integer. parseInt ( String. valueOf ( between_days) ) ;
} else if ( dateFlag1 && otherDateFlag1) {
cal. setTime ( stringToDate ( date, "yyyy-MM-dd HH:mm:ss" ) ) ;
long time1 = cal. getTimeInMillis ( ) ;
cal. setTime ( stringToDate ( otherDate, "yyyy-MM-dd HH:mm:ss" ) ) ;
long time2 = cal. getTimeInMillis ( ) ;
long between_days= ( time2- time1) / ( 1000 * 60 ) ;
return Integer. parseInt ( String. valueOf ( between_days) ) ;
} else {
Log4jUtil. error ( "日期格式不匹配" ) ;
return - 1 ;
}
}
public static List< String> getAllDates ( String start, String end) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
List< String> list = new ArrayList < String> ( ) ;
try {
Date date_start = sdf. parse ( start) ;
Date date_end = sdf. parse ( end) ;
Date date = date_start;
Calendar cd = Calendar. getInstance ( ) ;
while ( date. getTime ( ) <= date_end. getTime ( ) ) {
list. add ( sdf. format ( date) ) ;
cd. setTime ( date) ;
cd. add ( Calendar. DATE, 1 ) ;
date= cd. getTime ( ) ;
}
} catch ( ParseException e) {
Log4jUtil. error ( e. getMessage ( ) , e) ;
}
return list;
}
public static List< String> getDates ( String startTime, String endTime) {
List< String> result = null;
try {
SimpleDateFormat dateFormat= new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date dateStart = dateFormat. parse ( startTime) ;
Calendar calendarStart = Calendar. getInstance ( ) ;
calendarStart. setTime ( dateStart) ;
Date dateEnd = dateFormat. parse ( endTime) ;
Calendar calendarEnd = Calendar. getInstance ( ) ;
calendarEnd. setTime ( dateEnd) ;
result = new ArrayList < String> ( ) ;
result. add ( startTime) ;
calendarStart. add ( Calendar. DAY_OF_YEAR, 1 ) ;
while ( calendarStart. before ( calendarEnd) ) {
result. add ( dateFormat. format ( calendarStart. getTime ( ) ) ) ;
calendarStart. add ( Calendar. DAY_OF_YEAR, 1 ) ;
}
result. add ( endTime) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return result;
}
public static boolean checkIsHoliday ( String dateTime) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date date = dateFormat. parse ( dateTime) ;
Calendar cal = Calendar. getInstance ( ) ;
cal. setTime ( date) ;
if ( cal. get ( Calendar. DAY_OF_WEEK) == Calendar. SATURDAY||
cal. get ( Calendar. DAY_OF_WEEK) == Calendar. SUNDAY) {
return true ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return false ;
}
}