アットウィキロゴ

FormatDateWithoutTimeクラス


クラス FormatDateWithoutTime


import java.util.Calendar;
import java.text.SimpleDateFormat;

public class FormatDateWithoutTime {
    private Calendar calendar;
    private int year;
    private int month;
    private int day;

    public FormatDate() {
         calendar = Calendar.getInstance();
         calendar.setLenient(false);
         this.year = calendar.get(Calendar.YEAR);
         this.month = calendar.get(Calendar.MONTH);
         this.day = calendar.get(Calendar.DAY_OF_MONTH);
         this.setCalendar();
    }

    public FormatDate(String date) throws NumberFormatException, IllegalArgumentException{
         if(date == null){
              throw new NumberFormatException();
         }
         if(date.length() != 8){
              throw new NumberFormatException();
         }
	
         Integer.parseInt(date);
         this.year = Integer.parseInt(date.substring(0,4));
         this.month = Integer.parseInt(date.substring(4,6)) - 1;
         this.day = Integer.parseInt(date.substring(6,8));
         calendar = Calendar.getInstance();
         this.setCalendar();
         calendar.setLenient(false);
         calendar.getTime();
    }

    public static boolean existDate(String date){
         boolean IsYMDDate;
	
         if(date != null && date.length() == 8){
              try{
                   Integer.parseInt(date);
                   Calendar cal = Calendar.getInstance();
                   cal.set(Integer.parseInt(date.substring(0,4)), 
                             Integer.parseInt(date.substring(4,6)) - 1,
                             Integer.parseInt(date.substring(6,8)));
                             cal.setLenient(false);
                             cal.getTime();
                             IsYMDDate = true;
                   }
                   catch(Exception e){
                        IsYMDDate = false;
                   }
         }
         else{
              IsYMDDate = false;
         }	
         return IsYMDDate;
    }

    public void nextYear(){
         try{
              this.year = this.year + 1;
              this.setCalendar();
              calendar.getTime();
         }
         catch(Exception e){
              this.nextDay();
         }
    }

    public void prevYear(){
         try{
              this.year = this.year - 1;
              this.setCalendar();
              calendar.getTime();
         }
         catch(Exception e){
              this.prevDay();
         }
    }

    public void nextMonth(){
         try{
              this.month = this.month + 1;
              if(this.month == 12){
                   this.month = 0;
                   this.nextYear();
              }
              this.setCalendar();
              calendar.getTime();
         }
         catch(Exception e){
              this.nextDay();
         }
    }

    public void prevMonth(){
         try{
              this.month = this.month - 1;
              if(this.month == -1){
                   this.month = 11;
                   this.prevYear();
              }
              this.setCalendar();
         }
         catch(Exception e){
              this.prevDay();
         }
    }

    public void nextDay(){
         try{
              this.day = this.day + 1;
              this.setCalendar();
              calendar.getTime();
         }
         catch(Exception e){
              this.day = 1;
              this.nextMonth();
         }
    }

    public void prevDay(){
         try{
              this.day = this.day - 1;
              if(this.day == 0){
                   this.day = 31;
                   this.prevMonth();
              }
              this.setCalendar();
              calendar.getTime();
         }
         catch(Exception e){
              this.prevDay();
         }
    }
	
    public String getDate() throws NumberFormatException, IllegalArgumentException{
         SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
         return dateformat.format(calendar.getTime());
    }

    public String getFormatDate(SimpleDateFormat dateformat) throws NumberFormatException, IllegalArgumentException{
         return dateformat.format(calendar.getTime());
    }

    private void setCalendar(){
         calendar.set(this.year, this.month, this.day);
    }
}




最終更新:2008年05月06日 23:38
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。