[Java] 자바 날짜 계산,, SimpleDateFormat

http://mainia.tistory.com/2119

GregorianCalendar 도 있음. 찾아보기

// Date 로 구하기
    SimpleDateFormat fm1 = new SimpleDateFormat("yyyy년MM월dd일");
    String date = fm1.format(new Date());
    System.out.println("현재시간 년월일 = " + date);
// 두날짜의 차이 구하기
publicvoiddoDiffOfDate(){
    String start = "2015-04-01";
    String end = "2015-05-05";
    
    try{
        SimpleDateFormat formatter = newSimpleDateFormat("yyyy-MM-dd");
        Date beginDate = formatter.parse(start);
        Date endDate = formatter.parse(end);
        
        // 시간차이를 시간,분,초를 곱한 값으로 나누면 하루 단위가 나옴
        longdiff = endDate.getTime() - beginDate.getTime();
        longdiffDays = diff / (24* 60* 60* 1000);
        System.out.println("날짜차이="+ diffDays);
        
    } catch(ParseException e) {
        e.printStackTrace();
    }
}

경과시간(초) 구하기

long time1 = System.currentTimeMillis ();
long time2 = System.currentTimeMillis ();
system.out.println ( ( time2 - time1 ) / 1000.0 );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s