View Javadoc
1   package fr.paris.lutece.plugins.aot.modules.declarationimmat.utils;
2   
3   import java.text.ParseException;
4   import java.text.SimpleDateFormat;
5   import java.util.ArrayList;
6   import java.util.Calendar;
7   import java.util.Date;
8   import java.util.List;
9   
10  /**
11   * DateUtils to find if a date is during the french holidays or a sunday.
12   *
13   */
14  public class DateUtils
15  {
16  
17      /**
18       * Private Constrctor.
19       */
20      private DateUtils( )
21      {
22          super( );
23      }
24  
25      /**
26       * this method test if a date is during holidays using the wikipedia page for calculatint paques.
27       * 
28       * @param dateToTest
29       *            the date in a calendar
30       * @return true or false
31       */
32      public static boolean isDateDuringHoliday( Calendar dateToTest )
33      {
34  
35          final List<Calendar> holidays = new ArrayList<>( );
36  
37          final Calendar calendar = ( Calendar ) dateToTest.clone( );
38          // Add to the List each static day
39          calendar.set( calendar.get( Calendar.YEAR ), Calendar.JANUARY, 1 );
40          holidays.add( ( Calendar ) calendar.clone( ) );
41          calendar.set( calendar.get( Calendar.YEAR ), Calendar.MAY, 1 );
42          holidays.add( ( Calendar ) calendar.clone( ) );
43          calendar.set( calendar.get( Calendar.YEAR ), Calendar.MAY, 8 );
44          holidays.add( ( Calendar ) calendar.clone( ) );
45          calendar.set( calendar.get( Calendar.YEAR ), Calendar.JULY, 14 );
46          holidays.add( ( Calendar ) calendar.clone( ) );
47          calendar.set( calendar.get( Calendar.YEAR ), Calendar.AUGUST, 15 );
48          holidays.add( ( Calendar ) calendar.clone( ) );
49          calendar.set( calendar.get( Calendar.YEAR ), Calendar.NOVEMBER, 1 );
50          holidays.add( ( Calendar ) calendar.clone( ) );
51          calendar.set( calendar.get( Calendar.YEAR ), Calendar.NOVEMBER, 11 );
52          holidays.add( ( Calendar ) calendar.clone( ) );
53          calendar.set( calendar.get( Calendar.YEAR ), Calendar.DECEMBER, 25 );
54          holidays.add( ( Calendar ) calendar.clone( ) );
55  
56          // Adding the day that are dynamic
57          final int G = dateToTest.get( Calendar.YEAR ) % 19;
58          final int C = ( int ) Math.floor( dateToTest.get( Calendar.YEAR ) / 100d );
59          final int H = ( int ) ( ( C - Math.floor( C / 4d ) - Math.floor( ( 8 * C + 13 ) / 25d ) + 19 * G + 15 ) % 30 );
60          final int I = ( int ) ( H - Math.floor( H / 28d ) * ( 1 - Math.floor( H / 28d ) * Math.floor( 29d / ( H + 1 ) ) * Math.floor( ( 21d - G ) / 11d ) ) );
61          final int J = ( int ) ( ( dateToTest.get( Calendar.YEAR ) * 1 + Math.floor( dateToTest.get( Calendar.YEAR ) / 4d ) + I + 2 - C + Math.floor( C / 4d ) ) % 7 );
62          final int L = I - J;
63          // Month of Paques
64          final int monthOfPaques = ( int ) ( 3 + Math.floor( ( L + 40 ) / 44d ) );
65          // day of Paques (sunday)
66          final int dayOfPaques = ( int ) ( L + 28 - 31 * Math.floor( monthOfPaques / 4d ) );
67          calendar.set( dateToTest.get( Calendar.YEAR ), monthOfPaques - 1, dayOfPaques + 1 );
68          final Calendar mondayOfPaques = ( Calendar ) calendar.clone( );
69          holidays.add( mondayOfPaques );
70  
71          // Ascension
72          calendar.set( dateToTest.get( Calendar.YEAR ), monthOfPaques - 1, dayOfPaques + 39 );
73          final Calendar dayOfAscension = ( Calendar ) calendar.clone( );
74          holidays.add( dayOfAscension );
75  
76          // Pentecote
77          calendar.set( dateToTest.get( Calendar.YEAR ), monthOfPaques - 1, dayOfPaques + 50 );
78          final Calendar mondayOfPentecote = ( Calendar ) calendar.clone( );
79          holidays.add( mondayOfPentecote );
80  
81          return holidays.contains( dateToTest );
82      }
83  
84      /**
85       * this method test if a date is a sunday and/or if it's an holiday.
86       * 
87       * @param paramDate
88       *            the date to test
89       * @return true or false
90       */
91      public static boolean isDateDuringHolidayOrSunday( Date paramDate )
92      {
93          Calendar dateToTest = Calendar.getInstance( );
94          dateToTest.setTime( paramDate );
95  
96          return isDateDuringHoliday( dateToTest ) || dateToTest.get( Calendar.DAY_OF_WEEK ) == Calendar.SUNDAY;
97      }
98  
99      public static boolean isValidDate( String pDate, String pattern )
100     {
101         boolean result = Boolean.FALSE;
102         SimpleDateFormat dateFormat = new SimpleDateFormat( pattern );
103         dateFormat.setLenient( false );
104         try
105         {
106             dateFormat.parse( pDate.trim( ) );
107             result = Boolean.TRUE;
108         }
109         catch ( ParseException pe )
110         {
111             result = Boolean.FALSE;
112         }
113         return result;
114     }
115 
116     public static boolean isValidDate( String pDate )
117     {
118         List<String> list = java.util.Arrays.asList( "dd/MM/yyyy", "dd-MM-yyyy", "yyyy-MM-dd", "yyyy/MM/dd", "dd/MM/yyyy HH:mm:ss", "dd-MM-yyyy HH:mm:ss", "yyyy-MM-dd HH:mm:ss",
119                 "dd-MM-yyyy HH:mm:ss" );
120 
121         return list.stream( ).anyMatch( pattern -> isValidDate( pDate, pattern ) );
122 
123     }
124 
125 }