Java

[JAVA] GregorianCalendar

용식 2008. 3. 18. 09:47

// 2008년 3월 10일 0시 0분 0초
GregorianCalendar start = new GregorianCalendar(2008,2,10);
//2008년 3월 16일 23시 59분 59초
GregorianCalendar end = new GregorianCalendar(2008,2,16,23,59,59);
//현재시간
GregorianCalendar currentCal = new GregorianCalendar();

//2008년 3월 10일 0시 0분 1초 ~ 3월 16일 23시 59분 58초까지..
boolean between = currentCal.after(start) && currentCal.before(end);

after, before는 해당 시간을 포함하지 않는다.

parameter로 넘겨주는 값에서 월은 0이 1월이다.


또다른 사용법
날짜의 더하기 빼기..


import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public class Testm {
 public static void main(String[] args) {

  // 2008년 3월 10일 0시 0분 0초
  GregorianCalendar start = new GregorianCalendar(2008,2,10);
  String date = "";

  //2008년 3월 16일 23시 59분 59초
  GregorianCalendar end = new GregorianCalendar(2008,2,16,23,59,59);
  //현재시간
  GregorianCalendar currentCal = new GregorianCalendar();

  //2008년 3월 10일 0시 0분 1초 ~ 3월 16일 23시 59분 58초까지..
  boolean between = currentCal.after(start) && currentCal.before(end);
  start.add(start.MONTH, 2);

  date = convertDateFormat(start, "yyyyMMdd");

  System.out.println("between : " + between);
  System.out.println("start : " + date);

 }

 public static String convertDateFormat(GregorianCalendar C_Date, String C_Format){
  String returnValue = "";

  SimpleDateFormat formatter = new SimpleDateFormat(C_Format);
  returnValue = formatter.format( C_Date.getTime() );


  return returnValue;
 }
}


-----
2014년 2월 10일 추가
최근 사용해보니 Java Date 관련 API는 http://www.joda.org/joda-time/
를 사용하는게 가장 편하네요...