1

I just want to (filter/sort/remove) the weekends(Saturday and Sunday) in the list and store them in a new list in java. i). The class Name is Holiday, it holds only one variable named holiday date.

ii). And I have a table named Holiday, in this table I have data stored (Holidays list for 2022), totally I have 17 records in a table.

iii). That table data record can be fetched as a list.

iv). Have to find, if the employee takes a leave, that comes in holiday.

v). Now, compare an employee holiday with the Holidays list for 2022(17 records)

vi)If employee leave is the same as the date of the holiday list means I want to remove it and filter other normal working days & Store the dates in new array list.

vii) I just check the what diff between the two date formats.

viii) I am new to Java 8 concepts so. now only I start to learn java 8 that's why face this problem I just copy the code from this link and alternate it stackoverflow.com/questions/58134699/…

      String from="28/04/2022",;
        String to="28/07/2022",;
       
        Date d_from = new SimpleDateFormat("yyyy-MM-dd").parse(from);
        Date d_to = new SimpleDateFormat("yyyy-MM-dd").parse(to);
       
        //System.out.println(d_from.getTime());
       
        long t1=d_from.getTime();
        long t2=d_to.getTime();
       
        SimpleDateFormat f=new SimpleDateFormat("dd/MM/yyyy");
        
        List<Hoilday> Hoilday_list = new ArrayList<>();
        Map<String, Object> paramMap = new HashMap<>();
        
        StringBuilder sb = new StringBuilder();
        String sql = "";
        sb.append("select holidayDate as hoildayDate from hoildays ");
        sql = sb.toString();
        Hoilday_list = namedParameter.query(sql, paramMap, new BeanPropertyRowMapper<>(Hoilday.class));
       
        if(t1<t2)
        {
            //1 = 1000
            for(long i=t1;i<=t2;i+=86400000)
            {
              
     
               String allDates  = f.format(i);
             List<String> list = new ArrayList<String>();
            list.add(allDates);
       
            
          
            for(Hoilday Hoildays: Hoilday_list) {
                
                
                List<Hoilday> result = Hoilday_list.stream()
                        .filter(app1 -> list.stream()
                                .noneMatch(app2 -> Hoildays.getHoildayDate().equals(list)))
                        .collect(Collectors.toList());
            log.info(result);

In Advance Thank you for solving the question.

  • What is `Holiday`, why do you need this class to begin with? – Alexander Ivanchenko Oct 21 '22 at 08:19
  • 5
    Why are you mixing legacy `Date` API with the new Java-time API, meaning why do you need both `LocalDate` and legacy `Date`? – Alexander Ivanchenko Oct 21 '22 at 08:21
  • 2
    `.noneMatch(app2 -> Hoildays.getHoildayDate().equals(list))` - condition inside `noneMatch()` is wrong for several reasons. The most obvious: it doesn't check stream elements, which makes the stream poinless. – Alexander Ivanchenko Oct 21 '22 at 08:25
  • Your current question is unclear. Please, improve it, use the *edit* button or this [link](https://stackoverflow.com/posts/74150169/edit). – Alexander Ivanchenko Oct 21 '22 at 08:33
  • 2
    I strongly recommend you don’t use `SimpleDateFormat` nor `Date` (any of the `Date` classes). They are troublesome, poorly designed and long outdated, nothing you want to struggle with. Use `LocalDate`. See also [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). – Ole V.V. Oct 21 '22 at 10:30
  • 1
    I’d probably convert the list of `Holiday` to a list of `LocalDate` using a not too complicated stream operation. And then call `removeAll()` on a copy of the list of the employee’s leave dates (assuming they are `LocalDate` too). – Ole V.V. Oct 21 '22 at 10:39

1 Answers1

0

tl;dr

Addressing a simplified version of your Question's problem, narrowing down a list of holidays to those landing on weekdays.

new TreeSet< LocalDate >()
…
.stream()
.filter( localDate -> ! Set.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ).contains( localDate )
.toList() 

Details

The class Name is Holiday, it holds only one variable named holiday date.

So drop the class. If its only feature is a member field holding a date value, just use java.time.LocalDate.

Avoid legacy date-time classes. Use only java.time classes. Never use SimpleDateFormat nor either of the Date classes.

I imagine it would be helpful to use the holiday dates in their chronological order. So we use NavigableSet interface, with implementation TreeSet. A Set eliminates duplicates.

final NavigableSet< LocalDate > holidays = new TreeSet<>() ;
…
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
holidays.add( ld ) ;

I will drastically simplify your Question to ignore the employee aspect, and instead narrow down this set of holidays to those dates not landing on a Saturday or Sunday. You can apply the same principles in similar code to address your employee problem.

Use the java.time.DayOfWeek enum for day of week. Use an EnumSet for efficiency in defining an weekend.

final Set< DayOfWeek > weekend = EnumSet.of( SATURDAY , SUNDAY ) ;

Filter your set of holidays.

List< LocalDate > weekdayHolidays = 
    holidays
    .stream()
    .filter( holiday -> ! weekend.contains( holiday ) )
    .toList() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154