관리 메뉴

드럼치는 프로그래머

[JAVA] 날짜 비교 date compare 본문

★─Programing/☆─JAVA

[JAVA] 날짜 비교 date compare

드럼치는한동이 2017. 4. 4. 11:30

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class DateCompare {
    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" );
        Date day1 = null;
        Date day2 = null;
        try {
            day1 = format.parse( "2012-12-16" );
            day2 = format.parse( "2012-12-17" );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        int compare = day1.compareTo( day2 );
        if ( compare > 0 )
        {
            System.out.println( "day1 > day2" );
        }
        else if ( compare < 0 )
        {
            System.out.println( "day1 < day2" );
        }
        else
        {
            System.out.println( "day1 = day2" );
        }
    }
}

 

[출처] http://computersj.tistory.com/entry/JAVA-%EB%82%A0%EC%A7%9C-%EB%B9%84%EA%B5%90-date-compare

Comments