Java自定义日期处理工具类

1、主要使用Calendar类、SimpleDateFormat类

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));
    }
}


你可能感兴趣的:(Java日期处理)