001 |
public class DateUtil
{ |
002 |
003 |
//泽勒一致性公式计算星期几 |
004 |
public static String
getWeekOfDate( int year, int month, int day)
{ |
005 |
int q
= day; |
006 |
int m; |
007 |
String
weekday[] = new String[]
{ "Saturday" , "Sunday" , "Monday" , |
008 |
"Tuesday" , "Wednessday" , "Thursday" , "Friday" }; |
009 |
switch (month)
{ |
010 |
case 1 : |
011 |
m
= 13 ; |
012 |
year
= year - 1 ; |
013 |
break ; |
014 |
case 2 : |
015 |
m
= 14 ; |
016 |
year
= year - 1 ; |
017 |
break ; |
018 |
default : |
019 |
m
= month; |
020 |
break ; |
021 |
} |
022 |
int j
= year / 100 ; |
023 |
int k
= year % 100 ; |
024 |
int h
= (q + 26 *
(m + 1 )
/ 10 +
k + k / 4 +
j / 4 + 5 *
j) % 7 ; |
025 |
return weekday[h]; |
026 |
} |
027 |
028 |
//计算两个时间段之间的星期数 |
029 |
public static Object
countTwoDayWeek(String startDate, String endDate) { |
030 |
if (StringUtils.isNotEmpty(startDate) |
031 |
&&
StringUtils.isNotEmpty(endDate)) { |
032 |
long between_days=getDaysBetweenDates(startDate,
endDate); |
033 |
Double
days = Double.parseDouble(String.valueOf(between_days)); |
034 |
if ((days
/ 7 )
> 0 &&
(days / 7 )
<= 1 )
{ |
035 |
//
不满一周的按一周算 |
036 |
return 1 ; |
037 |
} else if (days
/ 7 > 1 )
{ |
038 |
int day
= days.intValue(); |
039 |
if (day
% 7 > 0 )
{ |
040 |
return day
/ 7 + 1 ; |
041 |
} else { |
042 |
return day
/ 7 ; |
043 |
} |
044 |
} else if ((days
/ 7 )
== 0 )
{ |
045 |
return 0 ; |
046 |
} else { |
047 |
//
负数返还null |
048 |
return null ; |
049 |
} |
050 |
} |
051 |
return null ; |
052 |
} |
053 |
054 |
//计算两个时间段的天数 |
055 |
public static long getDaysBetweenDates(String
startDate, String endDate) { |
056 |
if (StringUtils.isNotEmpty(startDate) |
057 |
&&
StringUtils.isNotEmpty(endDate)) { |
058 |
DateFormat
ft = new SimpleDateFormat( "yyyy-MM-dd
HH:mm:ss" ); |
059 |
Date
start = null ; |
060 |
Date
end = null ; |
061 |
try { |
062 |
start
= ft.parse(startDate); |
063 |
end
= ft.parse(endDate); |
064 |
} catch (ParseException
e) { |
065 |
//
TODO Auto-generated catch block |
066 |
e.printStackTrace(); |
067 |
} |
068 |
Calendar
cal = Calendar.getInstance(); |
069 |
cal.setTime(start); |
070 |
long time1
= cal.getTimeInMillis(); |
071 |
cal.setTime(end); |
072 |
long time2
= cal.getTimeInMillis(); |
073 |
long between_days
= (time2 - time1) / ( 1000 * 3600 * 24 ); |
074 |
return between_days; |
075 |
} |
076 |
return 0 ; |
077 |
} |
078 |
079 |
//计算两个时间段之间的月数 |
080 |
public static int getMonthSpace(String
startDate, String endDate) { |
081 |
DateFormat
ft = new SimpleDateFormat( "yyyy-MM-dd
HH:mm:ss" ); |
082 |
Calendar
c1 = Calendar.getInstance(); |
083 |
Calendar
c2 = Calendar.getInstance(); |
084 |
try { |
085 |
c1.setTime(ft.parse(startDate)); |
086 |
c2.setTime(ft.parse(endDate)); |
087 |
} catch (ParseException
e) { |
088 |
e.printStackTrace(); |
089 |
} |
090 |
if (c1.getTimeInMillis()
< c2.getTimeInMillis()) |
091 |
return 0 ; |
092 |
int year1
= c1.get(Calendar.YEAR); |
093 |
int year2
= c2.get(Calendar.YEAR); |
094 |
int month1
= c1.get(Calendar.MONTH); |
095 |
int month2
= c2.get(Calendar.MONTH); |
096 |
int day1
= c1.get(Calendar.DAY_OF_MONTH); |
097 |
int day2
= c2.get(Calendar.DAY_OF_MONTH); |
098 |
//
获取年的差值 假设 d1 = 2015-8-16 d2 = 2011-9-30 |
099 |
int yearInterval
= year1 - year2; |
100 |
//
如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数 |
101 |
if (month1
< month2 || month1 == month2 && day1 < day2) |
102 |
yearInterval--; |
103 |
//
获取月数差值 |
104 |
int monthInterval
= (month1 + 12 )
- month2; |
105 |
if (day1
< day2) |
106 |
monthInterval--; |
107 |
monthInterval
%= 12 ; |
108 |
return yearInterval
* 12 +
monthInterval; |
109 |
} |
110 |
111 |
public static void main(String[]
args) { |
112 |
System.out.println(DateUtil.getWeekOfDate( 2017 , 9 , 30 )); |
113 |
System.out.println(DateUtil.getDaysBetweenDates( "2016-09-30
06:30:15" , |
114 |
"2018-09-30
20:30:30" )); |
115 |
System.out.println(DateUtil.getMonthSpace( "2016-09-30
06:30:15" , |
116 |
"2015-08-31
05:30:30" )); |
117 |
System.out.println(DateUtil.countTwoDayWeek( "2017-09-30
06:30:15" , |
118 |
"2017-10-15
05:30:30" )); |
119 |
} |
120 |
} |