자바 사용자 정의 날짜 처리 도구 클래스

5156 단어 자바 날짜 처리
1. 주로 Calendar 클래스, Simple DateFormat 클래스 를 사용 합 니 다.
2. add 방법 은 현재 달 에 비해 set 방법 은 전체 에 대응 하 는 것 이 고 전체 달 은 달의 첫날 부터 시작 하 며 해 는 해 의 첫날 부터 시작 합 니 다.
3. 도구 류 코드 는 다음 과 같 습 니 다.
public class CalendarUtil {

    publicstatic final String FORMAT_1="yyyyMMdd";
    publicstatic final String FORMAT_2="yyyy-MM-dd";
    publicstatic final String FORMAT_3="yyyy-MM-dd hh:mm:ss";
    publicstatic final String FORMAT_4="yyyyMMdd hh:mm:ss";
   
   //      
    publicString getNow(String stringType){
       Calendarcalendar=Calendar.getInstance();
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //   /      
    publicString getLastOrNextDate(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.DAY_OF_YEAR, i);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //        
    publicString getOneDateOfWeek(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      
      if(calendar.get(Calendar.DAY_OF_WEEK)!=1){
          if (i<=6){
            calendar.set(Calendar.DAY_OF_WEEK, i+1);
          }else if(i==7) {
            calendar.add(Calendar.WEEK_OF_MONTH, 1);
            calendar.set(Calendar.DAY_OF_WEEK, 1);
          }
       }else{
          if (i<=6){
            calendar.add(Calendar.WEEK_OF_MONTH, -1);
            calendar.set(Calendar.DAY_OF_WEEK, i+1);
          }else if(i==7) {
            calendar.set(Calendar.DAY_OF_WEEK, 1);
          }
       }
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //   /      
    publicString getLastOrNextWeek(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.WEEK_OF_MONTH, i);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //   /        
    publicString getlastOrNextDateOfMonth(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.MONTH, i);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //         
    publicString getOneDateOfMonth(int i,int j,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.MONTH, i);
      calendar.set(Calendar.DAY_OF_MONTH, j);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //         
    publicString getFirstDateOfMonth(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.MONTH, i);
      calendar.set(Calendar.DAY_OF_MONTH, 1);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //          
    publicString getLastDateOfMonth(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.MONTH, i+1);
      calendar.set(Calendar.DAY_OF_MONTH, 0);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //             
    public DategetFirstDateTimeZoreOfMonth(int i,String stringType) throwsParseException{
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.MONTH, i);
      calendar.set(Calendar.DAY_OF_MONTH, 1);
       return newSimpleDateFormat(stringType).parse(newSimpleDateFormat(stringType).format(calendar.getTime()));
    }
   
   //   /      
    publicString getlastOrNextDateOfYear(int i,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.YEAR, i);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
   
   //          
    publicString getOneDateOfYear(int i,int j,String stringType){
       Calendarcalendar=Calendar.getInstance();
      calendar.add(Calendar.YEAR, i);
      calendar.set(Calendar.DAY_OF_YEAR, j);
       return newSimpleDateFormat(stringType).format(calendar.getTime());
    }
}

       :

public class MainTest {
   
    publicstatic void main(String[] args) throws ParseException {
       CalendarUtilcalendarUtil=new CalendarUtil();
      
      System.out.println(calendarUtil.getFirstDateTimeZoreOfMonth(0,CalendarUtil.FORMAT_4));
      
      System.out.println(calendarUtil.getFirstDateOfMonth(0,CalendarUtil.FORMAT_1));
      
      System.out.println(calendarUtil.getLastDateOfMonth(0,CalendarUtil.FORMAT_1));
      
      System.out.println(calendarUtil.getOneDateOfMonth(0,4,CalendarUtil.FORMAT_1));
      
      System.out.println(calendarUtil.getOneDateOfWeek(1,CalendarUtil.FORMAT_2));
      
      System.out.println(calendarUtil.getNow(CalendarUtil.FORMAT_2));
      
      System.out.println(calendarUtil.getLastOrNextWeek(1,CalendarUtil.FORMAT_2));
      
      System.out.println(calendarUtil.getlastOrNextDateOfMonth(0,CalendarUtil.FORMAT_2));
      
      System.out.println(calendarUtil.getLastOrNextDate(-1,CalendarUtil.FORMAT_2));
      
      System.out.println(calendarUtil.getlastOrNextDateOfYear(-1,CalendarUtil.FORMAT_2));
      
      System.out.println(calendarUtil.getOneDateOfYear(0,40,CalendarUtil.FORMAT_1));
    }
}

좋은 웹페이지 즐겨찾기