본문 바로가기
카테고리 없음

[Java] Java 를 이용한 주/월달력 생성로직

by Geunny 2022. 12. 6.
반응형

추후 정리

 

소스

 

DateStr

public class DateStr {
    private String dateStr;
    private final String DELIMITER = "-";
    private List<Integer> list;
    public DateStr(String dateStr) {
        if(!validation(dateStr)){
            throw new RuntimeException("DATE VALIDATION FAIL");
        }
        this.dateStr = dateStr;
        parseInt();
    }

    public boolean validation(String dateStr){
        try{
            DateTimeFormatter.ofPattern("yyyy-MM-dd").parse(dateStr);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    private void parseInt(){
        list = new ArrayList<>();
        String[] days = this.dateStr.split(DELIMITER);
        for(int i=0; i<days.length; i++){
            list.add(Integer.valueOf(days[i]));
        }
    }

    public int getYear(){
        return this.list.get(0);
    }

    public int getMonth(){
        return this.list.get(1);
    }
    public int getDay(){
        return this.list.get(2);
    }
}

Calendar

@Getter
@Builder
@NoArgsConstructor
public class Calendar {
    private LocalDate date;

    public Calendar(LocalDate date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Calendar{" +
                "date=" + date +
                '}';
    }
}

Week

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Week {
    public static final int MAX_SIZE = 7;
    private List<Calendar> weekDays = new ArrayList<>();
    
    public void addDays(Calendar calendar){
        if(weekDays.size() >= MAX_SIZE){
            throw new RuntimeException("LIST SIZE OVERFLOW");
        }
        this.weekDays.add(calendar);
    }
}

CalendarService

@Slf4j
@Service
@RequiredArgsConstructor
public class CalendarService {
    
    public List<List<Calendar>> makeMonthFromJava(String dateStr){
        LocalDate target = convertStrToLocalDate(dateStr);
        LocalDate nextMonth = target.plusMonths(1);

        LocalDate monthStartDay = LocalDate.of(target.getYear(), target.getMonth(), 1);
        LocalDate monthLastDay = LocalDate.of(nextMonth.getYear(), nextMonth.getMonth(), 1).minusDays(1);

        int gap = monthLastDay.compareTo(monthStartDay);
        int weekCnt = gap / Week.MAX_SIZE;

        if(gap % Week.MAX_SIZE !=0){
            weekCnt++;
        }
        List<List<Calendar>> month = new ArrayList<>();
        for(int i=0 ; i< weekCnt; i++){
            month.add(makeWeekFromJava(monthStartDay.plusWeeks(i).toString()));
        }
        return month;
    }

    public List<Calendar> makeWeekFromJava(String dateStr) {

        LocalDate target = convertStrToLocalDate(dateStr);

        List<Calendar> week = new ArrayList<>();
        if(target.getDayOfWeek() != DayOfWeek.SUNDAY){
            target = target.minusDays(target.getDayOfWeek().getValue());
        }

        for(int i=0 ;i<Week.MAX_SIZE; i++){
            week.add(new Calendar(target.plusDays(i)));
        }

        return week;
    }

    private LocalDate convertStrToLocalDate(String dateStr){
        DateStr str = new DateStr(dateStr);
        return LocalDate.of(str.getYear(), str.getMonth(), str.getDay());
    }

}

댓글