Contents

Views 11852 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
date.set(y,m,d...);
이런 방법이 없어짐 ㅠㅠ

두 날짜의 차이를 얻고 싶으시면, 그렇게 문자열에서 부분만을 뽑아서 계산하는 것보다는 Date 클래스를 이용하는 것이 더 깔끔하고 바람직한 방법입니다. 

다음 코드를 참고하세요.
코드를 보시면 충분히 이해하실 것이라 생각됩니다. 수행하면 결과값이 5가 나옵니다. 

SimpleDateFormat는 지정한 형태로 들어온 문자열을 Date 객체로 바꿔주는 역할을 합니다.
그리고, 이렇게 바뀌어진 두 날짜의 차이를 millisecond 단위로 구하고 이것을 다시 일단위로 바꾸는 것이지요.
import java.text.SimpleDateFormat;
import java.util.Date; 

public class DiffOfDate
{
  public static void main(String[] args) throws Exception
  {
    System.out.println(diffOfDate("20031028", "20031102"));
  }

  public static long diffOfDate(String begin, String end) throws Exception
  {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

    Date beginDate = formatter.parse(begin);
    Date endDate = formatter.parse(end);

    long diff = endDate.getTime() - beginDate.getTime();
    long diffDays = diff / (24 * 60 * 60 * 1000);

    return diffDays;
  }
}


?

List of Articles
No. Category Subject Author Date Views
405 Develop [php] whois정보 조회 프로그램 hooni 2003.04.23 11733
404 Develop [php] 배열 관련 함수 설명 ㅎㅎ hooni 2003.04.23 11746
403 Develop [ios] iOS In App Purchase 코드 부분 샘플 1 hooni 2013.11.20 11760
402 Develop JSON, BSON 변환 file hooni 2013.04.23 11814
401 Develop [c++] 기초강좌 #04(클래스) hooni 2003.04.23 11832
» Develop [java] 날짜 계산 (Date, SimpleDateFormat) hooni 2013.04.23 11852
399 System/OS [linux] 텔넷, FTP 텍스트 모드에서 사용 hooni 2003.04.23 11939
398 Develop [php] 한샘 전자발주 시스템.. file hooni 2013.04.23 11981
397 System/OS [unix] SUN Solaris 싱글모드.. ㅡ,.ㅡ; hooni 2003.04.23 11991
396 System/OS [linux] 쉘 스크립트 (Shell Script) hooni 2003.04.23 12064
395 Develop [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ file hooni 2013.04.23 12066
394 System/OS [linux] /etc/fstab 설정 방법.. ㅋㅋ hooni 2013.04.23 12081
Board Pagination Prev 1 ... 60 61 62 63 64 65 66 67 68 69 ... 98 Next
/ 98