mjon_git/src/main/java/itn/com/cmm/util/DateUtils.java

207 lines
6.7 KiB
Java

package itn.com.cmm.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
/**
*
* @author : 이호영
* @fileName : DateUtil.java
* @date : 2023.04.06
* @description : Date 다루는 Util
* ===========================================================
* DATE AUTHOR NOTE
* ----------------------------------------------------------- *
* 2023.04.06 이호영 최초 생성
*
*
*
*/
public final class DateUtils {
// 날짜를 포맷하는 기본 포맷터 정의
private static final DateTimeFormatter SLUSH_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");
/**
* @methodName : getTodayYearAndMonthAndFirstDay
* @author : 이호영
* @date : 2023.04.06
* @description : 해당 년월일 구하기 : 일은 1일
* @return ex) 2023/04/01
*/
public static String getTodayYearAndMonthAndFirstDay() {
LocalDate currentDate = LocalDate.now();
return LocalDate.of(currentDate.getYear(), currentDate.getMonthValue(), 1)
.format( DateTimeFormatter.ofPattern("yyyy/MM/dd"));
}
/**
* @methodName : getTodayYearAndMonthAndLastDay
* @author : 이호영
* @date : 2023.04.06
* @description : 해당 년월일 구하기 : 일은 마지막일
* @return ex) 2023/04/30
*/
public static String getTodayYearAndMonthAndLastDay() {
LocalDate currentDate = LocalDate.now();
return LocalDate.of(currentDate.getYear(), currentDate.getMonthValue(), currentDate.lengthOfMonth())
.format( DateTimeFormatter.ofPattern("yyyy/MM/dd"));
}
/**
* @methodName : getNowYearToString
* @author : 이호영
* @date : 2023.04.07
* @description : 현재 연도
* @return : String
*/
public static String getNowYearToString() {
return Integer.toString(LocalDate.now().getYear());
}
/**
* @methodName : getNowMonthToString
* @author : 이호영
* @date : 2023.04.07
* @description : 현재 월
* @return : String
*/
public static String getNowMonthToString() {
LocalDate currentDate = LocalDate.now();
return Integer.toString(currentDate.getMonthValue());
}
/**
* @methodName : getNowDayToString
* @author : 이호영
* @date : 2023.04.07
* @description : 현재 일
* @return : String
*/
public static String getNowDayToString() {
LocalDate currentDate = LocalDate.now();
return Integer.toString(currentDate.getDayOfMonth());
}
/**
* @methodName : getNowDayToString
* @author : 이호영
* @date : 2023.08.08
* @description : yyyy-MM-dd HH:mm:ss.S 형식을 yyyy-MM-dd HH:mm 로 변환
* @return
* @throws ParseException
*/
public static String getChangFormatS(String str) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); // 원본 형식
Date date = inputFormat.parse(str); // 문자열을 날짜 객체로 변환
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 원하는 출력 형식
String formattedDate = outputFormat.format(date); // 날짜 객체를 원하는 형식의 문자열로 변환
return formattedDate; // 출력: 2023-08-07 09:36
}
// 현재 날짜를 기본 포맷으로 반환
public static String getCurrentDate() {
return getCurrentDate(SLUSH_FORMATTER);
}
// 현재 날짜를 지정된 포맷으로 반환
public static String getCurrentDate(DateTimeFormatter formatter) {
LocalDate today = LocalDate.now();
return today.format(formatter);
}
// 현재 날짜에서 특정 일수 전 날짜를 기본 포맷으로 반환
public static String getDateMonthsAgo(int months) {
return getDateMonthsAgo(months, SLUSH_FORMATTER);
}
// 현재 날짜에서 특정 일수 전 날짜를 지정된 포맷으로 반환
public static String getDateMonthsAgo(int months, DateTimeFormatter formatter) {
LocalDate today = LocalDate.now();
// System.out.println("months : "+ months);
// System.out.println("today.minusMonths(months) : "+ today.minusMonths(months).format(formatter));
LocalDate monthsAgo = today.minusMonths(months).plusDays(1);
// System.out.println("monthsAgo : " + monthsAgo);
// System.out.println("monthsAgo.format(formatter) : " + monthsAgo.format(formatter));
return monthsAgo.format(formatter);
}
/**
* @methodName : dateChkAndValueChk
* @author : 이호영
* @date : 2024.07.05
* @description : 검색 날짜 검증 및 일수 체크
* @param searchStartDate
* @param searchEndDate
* @param dateVal
* @return
*/
public static boolean dateChkAndValueChk(String searchStartDate, String searchEndDate, int months) {
boolean isValid = true;
// 날짜 검증
LocalDate startDate = null;
LocalDate endDate = null;
// 검색 시작일자와 종료일자가 있는지 체크
if (searchStartDate == null || searchStartDate.isEmpty() || searchEndDate == null || searchEndDate.isEmpty()) {
isValid = false;
}
// 날짜 형식으로 변환
if (isValid) {
try {
startDate = LocalDate.parse(searchStartDate, SLUSH_FORMATTER);
endDate = LocalDate.parse(searchEndDate, SLUSH_FORMATTER);
} catch (Exception e) {
isValid = false;
}
}
// 시작일자가 종료일자보다 이후인지 확인
if (isValid && startDate.isAfter(endDate)) {
isValid = false;
}
// 총 기간이 지정한 개월 수를 넘는지 확인
if (isValid) {
long monthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
if (monthsBetween >= months) {
isValid = false;
}
}
return isValid;
}
public static String setStrToDataFormatter(String str, String formatter) {
// 입력 문자열을 LocalDateTime으로 변환
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(str, inputFormatter);
// 원하는 출력 포맷 적용
// DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM-dd HH:mm");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(formatter);
String formattedDate = dateTime.format(outputFormatter);
return formattedDate;
}
}