'How can i calculate working days off in java

Im trying to calculate the working days off with some required like:

In a month will work on 2 Saturdays . If this week work on Saturdays . The next week won't work

If this week working on Saturdays:

  • If i close work from Thursday to Tuesday next week. So it gonna be 5 days
  • Next im gonna close work from Thusday next week to Tuesday the next next week. So it gonna be 4 days

And when i report Month of leave days:

  • I have a close work from 29 this month to 4 next month. It gonna be 5-6 days depend on what month. But when i try to qerry this month it get 1 day off.
  • My solution is separate to two post. Which the first post has the endDate to the first day next month. Which another has the startDate match with the endDate of the first post. Now so we have two part of post and we can qerry each month

And here is my input data:

{
    "description":"Hello-test2",
    "userId":"2",
    "startDate":"2022-03-31 ",
    "endDate":"2022-04-02 "
}

Model

public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long postId;
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "user", referencedColumnName = "userId")
    private User user;
    private Long userId;
    private String description;
    private Boolean isEnable;
    private Long enableId;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
    private Long dayOff;
}

And here is my service

public void save(Post post) {
//        Long userId = new Long (2);
        post.setEnable(false);// is this post enabled by head department?
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(post.getStartDate());
        cal2.setTime(post.getEndDate());
        long diffInMillies = Math.abs(post.getEndDate().getTime() - post.getStartDate().getTime());//caculate date
        if(cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) //if Month of start-date  match with end-date
        {
            post.setDayOff(TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS));
            postRepository.save(post);
        } else{
           
                Post post2 = new Post(); //separate to 2 post
                post2.setEndDate(post.getEndDate());
                post2.setPostId(post.getPostId());
                post2.setDescription(post.getDescription());
//                post2.setEnableId(userId);
                post2.setEnable(false);
                post2.setStartDate(start);
                Timestamp start = getStartTimestamp(cal2.get(Calendar.MONTH)); //this function is get the first day of month
                post.setEndDate(start);

                long diffInMillies1 = Math.round(post.getEndDate().getTime() - post.getStartDate().getTime());
                long diffInMillies2 = Math.round(post2.getEndDate().getTime() - post2.getStartDate().getTime());
                post.setDayOff(TimeUnit.DAYS.convert(diffInMillies1, TimeUnit.MILLISECONDS));
                post2.setDayOff(TimeUnit.DAYS.convert(diffInMillies2, TimeUnit.MILLISECONDS));
                List<Post> posts = Arrays.asList(post, post2);
                postRepository.saveAll(posts);
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source