Utility class

时间:2016-07-20 10:51:08

标签: java spring

在我的项目中,我需要一年内所有假期的集合。 我的第一种方法是使用静态类,使用如下方法:

        Calendar now = new GregorianCalendar();
        giorniFestivi = new HashSet<GregorianCalendar>();

        GregorianCalendar newYearDay = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.JANUARY, 1);
        GregorianCalendar epiphany = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.JANUARY, 6);
        GregorianCalendar easterMonday = CommonUtils.findEasterDay(now.get(GregorianCalendar.YEAR));
        GregorianCalendar liberationDay = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.APRIL, 25);
        GregorianCalendar workerDay = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.MAY, 1);
        GregorianCalendar republicDay = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.JUNE, 2);
        GregorianCalendar ferragosto = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.AUGUST, 15);
        GregorianCalendar allSaints = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.NOVEMBER, 1);
        GregorianCalendar immaculate = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.DECEMBER, 8);
        GregorianCalendar christmasDay = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.DECEMBER, 25);
        GregorianCalendar saintStephano = new GregorianCalendar(now.get(Calendar.YEAR), Calendar.DECEMBER, 26);

        easterMonday.add(GregorianCalendar.DATE, 1);                                                                                                                                                    
        easterMonday.set(GregorianCalendar.HOUR, 0);                                                                                                                                                    
        easterMonday.set(GregorianCalendar.MINUTE, 0);                                                                                                                                                  
        easterMonday.set(GregorianCalendar.SECOND, 0);                                                                                                                                                  
        easterMonday.set(GregorianCalendar.MILLISECOND, 0);

但每次调用此方法时,我都会避免创建所有这些对象。所以我已经将我的代码与“Singleton方法”集成在一起,如果它不是null,则直接返回giorniFestivi(它是我班级中的静态属性)。

这种方法是正确的还是可以改进的(我还在考虑让这个类成为一个Spring Bean,并在需要的时候从上下文中获取它)?

事先提前

0 个答案:

没有答案