Skip to content

Commit

Permalink
[#3] refactor : DateTimeUtils 추가 및 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
princenim committed Mar 25, 2024
1 parent 1b3e781 commit 8b79df3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kboticketing.kboticketing.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author hazel
*/
public class DateTimeUtils {

public static String getCurrentTime() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return now.format(formatter);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kboticketing.kboticketing.utils.exception;

import com.kboticketing.kboticketing.utils.DateTimeUtils;
import com.kboticketing.kboticketing.utils.response.ErrorResponse;
import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -43,13 +44,10 @@ public ResponseEntity<ErrorResponse> handleValidationException(
FieldError firstError = bindingResult.getFieldError();
String defaultMessage = firstError.getDefaultMessage();

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

return ResponseEntity.status(HttpStatus.BAD_REQUEST).
body(ErrorResponse.builder()
.message(defaultMessage)
.timestamp(now.format(formatter))
.timestamp(DateTimeUtils.getCurrentTime())
.build());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kboticketing.kboticketing.utils.response;

import com.kboticketing.kboticketing.utils.DateTimeUtils;
import com.kboticketing.kboticketing.utils.exception.ErrorCode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -19,13 +20,10 @@ public class ErrorResponse {

public static ResponseEntity<ErrorResponse> toResponseEntity(ErrorCode errorCode) {

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

return ResponseEntity.status(errorCode.getHttpStatus())
.body(ErrorResponse.builder()
.message(errorCode.getMessage())
.timestamp(now.format(formatter))
.timestamp(DateTimeUtils.getCurrentTime())
.build());
}
}

0 comments on commit 8b79df3

Please sign in to comment.