package com.ninemax.util.loganalysis.tool;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* 日期工具类
*
* @author Darker
*
*/
public class CalendarUtil {
/**
* 判断是星期几
*
* @param pTime
*
* @return number 返回一个数字
*/
public static int getNumberWeek(String pTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
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;
}
return dayForWeek;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
* 判断是星期几
*
* @param pTime
*
* @return String 返回一个星期单词
*
* @throws ParseException
*/
public static String getWordWeek(String pTime){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Calendar cal = Calendar.getInstance();
try {
cal.setTime(format.parse(pTime));
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0){
w = 0;
}
return weekDays[w];
}
/**
* 一个月有多少天
*
* @param year
*
* @param month
*
* @return
*/
public static int getDaysInMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
// 在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);// 7月
int maxDate = cal.getActualMaximum(Calendar.DATE);
return maxDate;
}
/**
* 组合某年某月的日期(月)
*
* @param year
*
* @param month
*
* @return
*/
public static List<String> combinateMonthTime(String year, String month) {
// 接收日期
List<String> dateList = new ArrayList<String>();
String date = "";
int days = CalendarUtil.getDaysInMonth(Integer.valueOf(year),Integer.valueOf(month));
if (Integer.valueOf(month) > 9) {
for (int j = 1; j < 10; j++) {
date = year + "-" + month + "-0" + j;
dateList.add(date);
}
for (int k = 10; k <= days; k++) {
date = year + "-" + month + "-" + k;
dateList.add(date);
}
} else {
for (int j = 1; j < 10; j++) {
date = year + "-0" + month + "-0" + j;
dateList.add(date);
}
for (int k = 10; k <= days; k++) {
date = year + "-0" + month + "-" + k;
dateList.add(date);
}
}
return dateList;
}
/**
* 组合某年的日期(年)
*
* @param year
*
* @return
*/
public static List<String> combinateYearTime(String year) {
// 接收日期参数
List<String> dateList = new ArrayList<String>();
String date = "";
for (int m = 1; m <= 12; m++) {
int days = CalendarUtil.getDaysInMonth(Integer.valueOf(year),Integer.valueOf(m));
if (m > 9) {
for (int j = 1; j < 10; j++) {
date = year + "-" + m + "-0" + j;
dateList.add(date);
}
for (int k = 10; k <= days; k++) {
date = year + "-" + m + "-" + k;
dateList.add(date);
}
} else {
for (int j = 1; j < 10; j++) {
date = year + "-0" + m + "-0" + j;
dateList.add(date);
}
for (int k = 10; k <= days; k++) {
date = year + "-0" + m + "-" + k;
dateList.add(date);
}
}
}
return dateList;
}
/**
* 收集某月的所有周末
*
* @param year
*
* @param month
*
* @return
*/
public static List<String> combinateWeekendInMonth(String year, String month) {
// 接收日期参数
List<String> dateList = new ArrayList<String>();
// 收集本月周末的日期
List<String> weekendList = new ArrayList<String>();
// 获取所有日期
dateList = CalendarUtil.combinateMonthTime(year, month);
for (int i = 0; i < dateList.size(); i++) {
if (CalendarUtil.getNumberWeek(dateList.get(i)) == 6 || CalendarUtil.getNumberWeek(dateList.get(i)) == 7) {
weekendList.add(dateList.get(i));
}
}
return weekendList;
}
/**
* 收集某月的所有工作日
*
* @param year
*
* @param month
*
* @return
*/
public static List<String> combinateWeekdayInMonth(String year, String month) {
// 接收日期参数
List<String> dateList = new ArrayList<String>();
// 收集本月周末的日期
List<String> weekdayList = new ArrayList<String>();
// 获取所有日期
dateList = CalendarUtil.combinateMonthTime(year, month);
for (int i = 0; i < dateList.size(); i++) {
if (CalendarUtil.getNumberWeek(dateList.get(i)) != 6 && CalendarUtil.getNumberWeek(dateList.get(i)) != 7) {
weekdayList.add(dateList.get(i));
}
}
return weekdayList;
}
/**
* 获取一个月周数的日期
*
* @param n 第几周
*
* @param dateList
*
* @return map
*/
public static Map<Integer,List<String>> getWeeksMapInMonth(int n,List<String> dateList){
// 接收结果集
Map<Integer,List<String>> dateMap =new HashMap<Integer,List<String>>();
// 收集周
List<String> weekList = new ArrayList<String>();
breakFor: for (int k = 0; k < dateList.size(); k++) {
String date = dateList.get(k);
if (CalendarUtil.getNumberWeek(date) == 7) {
// 剩余天数
int temp = dateList.size() - (k + (n - 1) * 7 + 1);
if (temp > 6) {
weekList.add(dateList.get(k + (n - 1) * 7));
weekList.add(dateList.get(k + 1 + (n - 1) * 7));
weekList.add(dateList.get(k + 2 + (n - 1) * 7));
weekList.add(dateList.get(k + 3 + (n - 1) * 7));
weekList.add(dateList.get(k + 4 + (n - 1) * 7));
weekList.add(dateList.get(k + 5 + (n - 1) * 7));
weekList.add(dateList.get(k + 6 + (n - 1) * 7));
dateMap.put(n, weekList);
} else {
weekList.add(dateList.get(k + (n - 1) * 7));
for (int p = 1; p <= temp; p++) {
weekList.add(dateList.get(Integer.valueOf(date.split("-")[2]) + p));
}
dateMap.put(n, weekList);
}
break breakFor;
}
}
return dateMap;
}
/**
* 获取一个月周数的日期
*
* @param n 第几周
*
* @param dateList
*
* @return list
*/
public static List<String> getWeeksListInMonth(int n ,List<String> dateList) {
// 收集周参数
List<String> weekList = new ArrayList<String>();
breakFor: for (int i = 0; i < dateList.size(); i++) {
String date = dateList.get(i);
if (CalendarUtil.getNumberWeek(date) == 7) {
// 剩余天数
int temp = dateList.size() - (i + (n - 1) * 7 + 1);
if (temp > 6) {
weekList.add(dateList.get(i + (n - 1) * 7));
weekList.add(dateList.get(i + 1 + (n - 1) * 7));
weekList.add(dateList.get(i + 2 + (n - 1) * 7));
weekList.add(dateList.get(i + 3 + (n - 1) * 7));
weekList.add(dateList.get(i + 4 + (n - 1) * 7));
weekList.add(dateList.get(i + 5 + (n - 1) * 7));
weekList.add(dateList.get(i + 6 + (n - 1) * 7));
} else {
weekList.add(dateList.get(i + (n - 1) * 7));
for (int j = 1; j <= temp; j++) {
weekList.add(dateList.get(Integer.valueOf(date.split("-")[2]) + j));
}
}
break breakFor;
}
}
return weekList;
}
/**
* 2016-06-01转换成 June 01 2016
*
* @param date
*
* @return
*/
public static String transformDate(String date) {
Date month = null;
SimpleDateFormat sdf = new SimpleDateFormat("MM");
try {
month = sdf.parse(date.split("-")[1]);
} catch (ParseException e) {
e.printStackTrace();
}
sdf = new SimpleDateFormat("MMMMM", Locale.US);
return sdf.format(month) + " "+ date.split("-")[2] + " " + date.split("-")[0];
}
}
/**
* 组合两个时间之间的时间
* @param str1
* @param str2
* @return
* @throws Exception
*/
public static List<String> getDaysList(String str1, String str2) throws Exception {
List<String> dateList = new ArrayList<String>();
long days = getDistanceDays(str1, str2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
for (int i = 0; i <= days; i++) {
c.setTime(sdf.parse(str1));
c.add(Calendar.DATE, i);
dateList.add(sdf.format(c.getTime()));
}
return dateList;
}
/**
* 计算两个时间之间相差的天数
* @param str1
* @param str2
* @return
* @throws Exception
*/
public static long getDistanceDays(String str1, String str2) throws Exception{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date one;
Date two;
long days=0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff ;
if(time1<time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
Remark:Java菜鸟交流群:企鹅(260052172)大神莫入,THS!