본문 바로가기

프로그래밍 언어 /java

날짜에 관하여...형식지정 날짜비교..

java에서 날짜 형식 타입을 지정하는 방법


날짜를 비교하거나 시간을 비교할때

SimpleDateFormat 사용해서 format형식을 먼저 정한다.

날짜

SimpleDateFormat dtformat = new SimpleDateFormat("yyyy-MM-dd");

시간

SimpleDateFormat tmFormat = new SimpleDateFormat("HH:mm");

Date dayonDe = format.parse(x); //x 비교할 날짜 시간

Date dayoffDe = format.parse(y); //y 비교할 날짜 시간

Format dtformat 넣으면 날짜비교가 가능하며

tmFormat 넣으면 시간 비교가 가능하다.


다음 compareTo함수를 이용해서

int compare=dayonDe.compareTo(dayoffDe);

두개의 날짜나 시간을 비교한다

-1 0 1 값이 나온다

값이 비교했을때 크면 1

같으면 0

작으면 -1 이다.

밑에 날짜 표현 방식이다

SimpleDateFormat 날짜 방식..