정말 간단한 소스이지만,

이것도 코딩하려면 일일이 다 기억하고 있지도 않을뿐더러,

일일이 코딩하는것도 어리석은 짓일지도 모른다.

 

아래 작성해놓은 소스를 마음껏 긁어다 쓰세요.^^

 

Java에서 Calendar 클래스와 SimpleDateFormat 클래스를 이용해서,

현재 날짜와 그 날짜를 기준으로 이전 날짜를 특정 포맷으로 계산하는 샘플

 

 

    

     import java.util.Calendar; 

     import java.text.SimpleDateFormat;

 

     String today= null;   // 시작일자

     String preday= null;    // 종료일자

 

     // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");  
     Calendar currentDate = Calendar.getInstance();
    
     // 현재일자
     today= dateFormat.format(currentDate.getTime());
    
     // // 현재 날짜보다 10일 이전 날짜 구하기
     currentDate.add(Calendar.DATE, -10); 

     preday= dateFormat.format(currentDate.getTime());

 

    // 결과 (예)

    // today = "20131119";

    // preday = "20131109"

 

 

Posted by 우라질레이터

urajilation@gmail.com
우라질레이터

달력

태그목록