자바(Java)

자바 현재날짜 이전날짜 문자열로 비교

xemaker 2023. 1. 5. 14:59

자바로 날짜 스트링을 받아 설정해 놓은 날짜보다 큰지 작은지 비교하는 방법이다.

예를들어 한달 무료 사용이라고 했을때 한달날짜가 지났는지 체크하려고 한다.

public class NowDate{
  public static boolean isBefore(){
    boolean rst=false;
    String nowStr="20230104";
    String dueStr="20230204";
    DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate nowDate=LocalDate.parse(nowStr, formatter);
    LocalDate dueDate=LocalDate.parse(dueStr,formatter);

    rst=nowDate.isBefore(dueDate);
    return rst;
}

    public static void main(String[] args){
     boolean b=NowDate.isBefore();
      System.out.println(b);
  }
}

이렇게 하면 되는데.. 문제는 로컬에 세팅된 시간을 가져오는 것이다. 만약 사람이 수동으로 날짜를 바꾸면 그 바뀐 이상한 날짜를 가져온다.

그래서 그냥 서버에서 날짜를 가져와서 체크하는 방법을 개발했다.

이것은 다음글에..