package com.basetnt.sun.valley.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class HolidayUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* 根据判断当前时间是否是节假日还是周末还是平日 (普通为0;节假日1;周末为2)
*
* @return
*/
public static int getDateType(Date now, List<String> holidays) {
int type = 0;
String today = sdf.format(now);
if (holidays != null && holidays.size() > 0) {
for (String holiday : holidays) {
if (holiday.equals(today)) {
type = 1;
return type;
}
}
}
Calendar c = Calendar.getInstance();
c.setTime(now);
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
type = 2;
}
return type;
}
// public static void main(String[] args) throws ParseException {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// HolidayUtil h = new HolidayUtil();
// List<String> String = new ArrayList<>();
// String.add("2020-05-10");
// System.out.println(h.getDateType(sdf.parse("2020-05-10"), String));
// }
public static List<String> getDays(String startTime, String endTime) {
// 返回的日期集合
List<String> days = new ArrayList<String>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
days.add(dateFormat.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
}
java 节假日和周末和平日
最新推荐文章于 2023-06-17 11:45:29 发布