나에게 부족한것을 배우기 위해 열심히 스프링 관련된 강의를 듣는 중이었는데, 코드 연습으로는 실전만한게 없다고 생각해 세미 프로젝트를 진행할 예정이다.
현재 요구사항 명세서까지는 작성했는데 아직 테이블 명세서를 작성하지 못했다.
그치만... 내가 할려고 하는 것이 예약 사이트인데 달력을 어떻게 만들지 감이 안잡혀서
자바 달력 강의를 듣고 지금까지 배운 내용들을 응용해서 오늘 급하게 만들었다...
기분이 좋아서 글을 써 봅니다...
앞으로 이 자바 모델을 어떻게 더 발전해 나갈지 계속해서 글을 써 보겟다
package com.semiproject.semiproject.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Controller
public class Index {
// 이 달력은 이중리스트로 해결을 해야할것 같다
@GetMapping({"/",""})
public String index(Model model){
List<List<Resolve>> dates = createDate();
log.info("날짜 데이터 1 = {}", dates.get(0).toString());
log.info("날짜 데이터 2 = {}", dates.get(1).toString());
log.info("날짜 데이터 3 = {}", dates.get(2).toString());
log.info("날짜 데이터 4 = {}", dates.get(3).toString());
log.info("날짜 데이터 5 = {}", dates.get(4).toString());
model.addAttribute("dates",dates);
return "index";
}
// 첫번째 주의 List는 그달 1일의 요일 - 일요일의 숫자만큼의 날짜가 더해져야한다
private List<List<Resolve>> createDate() {
List<Resolve> date = new ArrayList<>();
List<List<Resolve>> free = new ArrayList<>();
LocalDate today = LocalDate.now();
LocalDate first = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate last = today.with(TemporalAdjusters.lastDayOfMonth());
log.info("달력의 날 = {}", last.getDayOfMonth());
// 첫번째주의 List 값만들기
List<Resolve> firstWeek = new ArrayList<>();
List<Resolve> secondWeek = new ArrayList<>();
List<Resolve> thirdWeek = new ArrayList<>();
List<Resolve> fourthWeek = new ArrayList<>();
List<Resolve> fifthWeek = new ArrayList<>();
Resolve init = new Resolve(LocalDate.of(2023,01,01));
for(int i=0;i<last.getDayOfMonth();i++){
if(i<7)
firstWeek.add(new Resolve(first.plusDays(i)));
if(i>6 && i<14)
secondWeek.add(new Resolve(first.plusDays(i)));
if(i>=14 && i<21)
thirdWeek.add(new Resolve(first.plusDays(i)));
if(i>=21 && i<28)
fourthWeek.add(new Resolve(first.plusDays(i)));
if(i>=28 && i< last.getDayOfMonth())
fifthWeek.add(new Resolve(first.plusDays(i)));
}
List<List<Resolve>> result = List.of(firstWeek,secondWeek,
thirdWeek,fourthWeek,fifthWeek);
return result;
}
static class Resolve{
LocalDate date ;
public Resolve(LocalDate date) {
this.date = date;
}
public int getDate() {
return date.getDayOfMonth();
}
public int getMonth(){
return date.getMonthValue();
}
public int getYear(){
return date.getYear();
}
@Override
public String toString() {
return "Resolve{" +
"date=" + date +
'}';
}
}
}
헤헤... 가져다 쓰고 싶은 사람은 없겠지만 지적, 훈수 감사히 받을게용~
부족한 점, 개선 할 점 알려주시면 적극 반영하겠습니다.