# 1. 시간 표시 변경하기
시간 표시가 너무 날(?) 것 그대로의 모습을 가지고 있어서 이번에 변경해보려고 한다.
현재 상태는 아래와 같이 되어있다.
이것을 YYYY-MM-DD HH:MM:SS 형태로 조금 더 깔끔하게 변경하려고 한다.
검색해보니까 Java 1.8 이전에는 Date 라는 클래스를 사용했는데 , 1.8이 되면서 Date 라는 클래스에서는 많은 메서드들이 @Deprecated 처리되고 LocalDateTime 이라는 클래스가 도입되었다고 해서 그것으로 검색해서 변경해보려고 한다.
우선 테스트 클래스를 작성한다.
@Test
@DisplayName("LocalDateTime 객체 원하는 형태로 변경하기")
public void testTime() {
String customizerFormat = "YYYY년-MM월-DD일 HH시:MM분:SS초";
/*
* author: LeeJongAnn
* description: Changes the class of LocalDateTime at the current time to the pattern of the cutomizer.
* */
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(customizerFormat);
String format = now.format(dateTimeFormatter);
System.out.println(format);
}
돌려보면 아래와 같이 나온다.
그러면 이제 이것을 적용해보자 아래는 boardServiceImple 클래스이다. 이곳에 changeTimeFormatNow라는 메소드를 통해서 시간을 변경해줄 수 있도록 하였다. 그리고 createBoard 메소드에서 이것을 집어넣어 주었다.
package com.spring.MovieProject.service.board;
import com.spring.MovieProject.config.DetailsUser;
import com.spring.MovieProject.entity.Board;
import com.spring.MovieProject.entity.User;
import com.spring.MovieProject.repository.BoardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
@Service
public class BoardServiceImpl implements boardService {
private BoardRepository boardRepository;
@Autowired
public BoardServiceImpl(BoardRepository boardRepository) {
this.boardRepository = boardRepository;
}
@Override
public Board createBoard(Board board, DetailsUser user) {
board.setCreationTime(changeTimeFormatNow());
Board saveBoard = boardRepository.save(board);
return saveBoard;
}
@Override
public List<Board> getBoardList() {
List<Board> boardList = (List<Board>) boardRepository.findAll();
return boardList;
}
@Override
public Board updateBoard() {
return null;
}
@Override
public void deleteBoard() {
}
@Override
public Board getBoard() {
return null;
}
/*
* author: LeeJongAnn
* description: Changes the class of LocalDateTime at the current time to the pattern of the cutomizer.
* */
public String changeTimeFormatNow() {
String customizerFormat = "yyyy년 MM월 dd일 HH시 mm분 ss초";
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(customizerFormat);
String format = now.format(dateTimeFormatter);
return format;
}
}
성공적으로 시간 형태가 변경되어 있는 것을 확인할 수 있다.
위에 있는 것보다 훨씬 깔끔한 형태이다.
앞으로 만들 내용은 아래와 같다.
1. 게시글 상세 조회 페이지
2. 사용자 리스트에서 해당 유저 편집
3. 게시글 삭제
4. 영화 관련 데이터 처리글 페이징 처리
'자바의 봄(Spring) > 프로젝트' 카테고리의 다른 글
SpringBoot-Movie-Thymeleaf-Project - 10 Thymeleaf에서 javascript 함수 사용하기 (0) | 2024.04.14 |
---|---|
SpringBoot-Movie-Thymeleaf-Project - 8 api로 받아온 데이터 페이징 처리 해주기 (0) | 2024.04.07 |
SpringBoot-Movie-Thymeleaf-Project - 7 게시글 작성하기 (0) | 2024.04.05 |
SpringBoot-Movie-Thymeleaf-Project - 6 유저 리스트 만들기 및 로그인 로그아웃시 Navigation bar 변경하기 (0) | 2024.04.05 |
SpringBoot-Movie-Thymeleaf-Project - 5 (0) | 2024.03.31 |
댓글