Contents

Views 11877 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
265 Develop [c] pcapdump 파일 분석 하는 프로그램.. ㅋㅋ file hooni 2013.04.23 6916
264 Develop [c] 간단한 순위 루틴.. (정보처리기사) hooni 2003.04.23 6907
263 Develop [js] 수명체크 프로그램 ㅋㅋ file hooni 2013.04.23 6907
262 Develop [c] 라인수 입력받아 마름모꼴 출력하기.. hooni 2003.04.23 6903
261 Develop [js] 사라지는 브라우저 hooni 2003.04.23 6882
260 Develop [ajax] 샘플 코드와 한글처리에 대한 간단한 설명 hooni 2013.04.23 6878
259 Develop [js] get방식, url이후 모두 그대로 읽어오기.. hooni 2013.04.23 6874
258 Develop [pdf] 윈도우즈 95 시스템 프로그래밍(Windows 95 system programming) file hooni 2013.04.23 6869
257 Develop [js] 숫자만 입력하게 하는 자바스크립트 hooni 2013.04.23 6865
256 Develop [c] 단기과정[01/14] 파일 입출력 file hooni 2003.04.23 6863
255 Develop [c] 문자열 str_shift 예제.. file hooni 2013.04.23 6862
254 Develop [c++] 프리렉(freelec) 예제 자료.. ㅋㄷ file hooni 2013.04.23 6861
Board Pagination Prev 1 ... 44 45 46 47 48 49 50 51 52 53 ... 71 Next
/ 71