要实现该功能需要两步
第一步,配置“班”和“休”
由于每年“班”和“休”对应的日期都不一样(依据:国务院办公厅关于2020年部分节假日安排的通知),没法做到完全自动化,必须手动配置
我们将“班”和“休的日期分别配置到/data
目录下的2020_w.txt
和2020.txt
文件:
2020_w.txt(班)
1 2 3 4 5 6
| 0119 0426 0509 0628 0927 1010
|
2020.txt(休)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 0101 0124 0125 0126 0127 0128 0129 0130 0131 0201 0202 0404 0405 0406 0501 0502 0503 0504 0505 0625 0626 0627 1001 1002 1003 1004 1005 1006 1007 1008
|
如何快速查看某天是“班”还是“休”
第二步,功能代码
废话不多说,直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| import java.io.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*;
public class ChineseHolidayUtil { private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); private final static Map<Date, Integer> holidayBuffer = new HashMap<>(); private final static List<File> dataPath = new ArrayList<>(); private final static String storDataPath = "**/common/data";
static { getFilePath(new File(storDataPath)); if (dataPath.size() != 0) { String temp = ""; for (File f : dataPath) { try { BufferedReader reader = new BufferedReader(new FileReader(f)); while ((temp = reader.readLine()) != null) { if (!temp.equals("\n")) { if (f.getName().matches("[2][0][2][0-9]_w.txt")) holidayBuffer.put(holidayToDate(f, temp), 0); else if(f.getName().matches("[2][0][2][0-9].txt")) holidayBuffer.put(holidayToDate(f, temp), 1); } } reader.close(); } catch (IOException | ParseException e) { e.printStackTrace(); } } } }
private static boolean isWeekend(Date d) { Calendar cal = Calendar.getInstance(); cal.setTime(d); return (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY); }
private static void getFilePath (File file) { if(file.isDirectory()){ File f[]= file.listFiles(); if(f!=null){ for (File aF : f) { getFilePath(aF); } } } else { dataPath.add(file); } }
private static Date holidayToDate(File f, String s) throws ParseException { String fileName = f.getName(); return format.parse(fileName.split("\\_|\\.")[0] + "-" + s.substring(0,2) + "-" + s.substring(2)); }
public static Integer getHoliday(Date d) { System.out.println(d); if (holidayBuffer.containsKey(d)) { return holidayBuffer.get(d); } else { return isWeekend(d) ? 2 : 0; } } public static void main(String[] args) throws ParseException { System.out.println(getHoliday(format.parse(format.format(new Date())))); } }
|
完!