4.1一般的な日付操作

11249 ワード

プログラマーはよくjavaとC#を比較して、javaの文法がどんなに簡潔ではないか、C#がどんなに簡潔かを言って、その中でよくjavaの日付操作を言っています.確かに原生の日付操作は面倒で、Calenderに基づいて加減乗除しなければならない.メリットは非常に柔軟で、どうすればいいか、自分の要求に合ったDateUtilを自分で作ればいいということだ.
以下は、多くの一般的な日付操作方法を含む実際の開発で使用されているDateUtilです.
public class DateUtil {
    private static final String defaultFormatStr = "yyyy-MM-dd";//           

    /**
     *       
     *
     * @param date
     * @param formatStr       
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String dateToString(Date date, String formatStr) {
        DateFormat df = new SimpleDateFormat(formatStr);
        return df.format(date);
    }

    /**
     *           
     *
     * @param dateStr           
     * @param formatStr               yyyy-MM-dd
     * @return Date         
     * @author    
     * @date 2017-05-23
     */
    public static Date stringToDate(String dateStr, String formatStr) {
        DateFormat sdf = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     *       ,   yyyy-MM-dd HH:mm:ss
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getSystemTime() {
        String strTime = "";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        strTime = df.format(new Date());
        return strTime;
    }

    /**
     *       ,   yyyy-MM-dd
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getSystemDate() {
        String strDate = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        strDate = df.format(new Date());
        return strDate;
    }


    /**
     *       
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getShortSystemTime() {
        String strTime = "";
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        strTime = df.format(new Date());
        return strTime;
    }


    /**
     *        ,yyyyMMdd
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getShortSystemDate() {
        String strTime = "";
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        strTime = df.format(new Date());
        return strTime;
    }

    /**
     *       
     *
     * @param date          
     * @param dayNum          
     * @param formatStr       
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getOperaDate(String date, int dayNum, String formatStr) {
        Date dt = null;
        SimpleDateFormat df = new SimpleDateFormat(formatStr);
        try {
            dt = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.add(5, dayNum);
        return df.format(gc.getTime());
    }

    /**
     *       ,      
     *
     * @param date       
     * @param dayNum       
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getOperaDate(String date, int dayNum) {
        return getOperaDate(date, dayNum, defaultFormatStr);
    }

    /**
     *       
     *
     * @param date           
     * @param monthNum     
     * @param formatStr       
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getOperaMonth(String date, int monthNum, String formatStr) {
        Date dt = null;
        SimpleDateFormat df = new SimpleDateFormat(formatStr);
        try {
            dt = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.add(2, monthNum);
        return df.format(gc.getTime());
    }

    /**
     *       
     *
     * @param date
     * @param monthNum
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getOperaMonth(String date, int monthNum) {
        return getOperaMonth(date, monthNum, defaultFormatStr);
    }

    /**
     *           
     *
     * @param date1       1
     * @param date2       2
     * @param formatStr       
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static int getDateDifference(String date1, String date2, String formatStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        ParsePosition pos = new ParsePosition(0);
        ParsePosition pos1 = new ParsePosition(0);
        Date dt1 = formatter.parse(date1, pos);
        Date dt2 = formatter.parse(date2, pos1);
        int l = (int) (dt2.getTime() - dt1.getTime()) / (3600 * 24 * 1000);
        return l;
    }

    /**
     *           ,       
     *
     * @param date1   1
     * @param date2   2
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static int getDateDifference(String date1, String date2) {
        return getDateDifference(date1, date2, defaultFormatStr);
    }

    /**
     *           ,       
     *
     * @param date1   1
     * @param date2   2
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static int getHourDifference(Date date1, Date date2) {
        int l = (int) (date1.getTime() - date2.getTime()) / (3600 * 1000);
        return l;
    }

    /**
     *           
     *
     * @param date1       1
     * @param date2       2
     * @param formatStr       
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static int getMonthDifference(String date1, String date2, String formatStr) {
        int result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     *           
     *
     * @param date1   1
     * @param date2   2
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static int getMonthDifference(String date1, String date2) {
        int result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(defaultFormatStr);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result;
    }

    /**
     *         
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getLastDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));//  
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));//  ,  Calendar     0  ,    1
        cal.set(Calendar.DATE, 1);//  ,    
        cal.add(Calendar.MONTH, 1);//     ,        
        cal.add(Calendar.DATE, -1);//              
        return new SimpleDateFormat(defaultFormatStr).format(cal.getTime());
    }

    /**
     *        
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));//  
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));//  ,  Calendar     0  ,    1
        cal.set(Calendar.DATE, 1);//  ,    
        String df = new SimpleDateFormat(defaultFormatStr).format(cal.getTime());
        return df;//        
    }

    /**
     *          
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getFirstDayOfLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));//  
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));//  ,  Calendar     0  ,    1
        cal.set(Calendar.DATE, 1);//  ,    
        cal.add(Calendar.MONTH, -1);//     ,        
        String df = new SimpleDateFormat(defaultFormatStr).format(cal.getTime());
        return df;//        
    }

    /**
     *           
     *
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getLastDayOfNextMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));//  
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));//  ,  Calendar     0  ,    1
        cal.set(Calendar.DATE, 1);//  ,    
        cal.add(Calendar.MONTH, 2);//     ,         
        cal.add(Calendar.DATE, -1);//                
        String df = new SimpleDateFormat(defaultFormatStr).format(cal.getTime());
        return df;//        
    }


    /**
     *         
     *
     * @param date     
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static String getLastDayOfMonth(String date) {
        Date dt = null;
        SimpleDateFormat df = new SimpleDateFormat(defaultFormatStr);
        try {
            dt = df.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));//  
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));//  ,  Calendar     0  ,    1
        cal.set(Calendar.DATE, 1);//  ,    
        cal.add(Calendar.MONTH, 1);//     ,        
        cal.add(Calendar.DATE, -1);//              
        return df.format(cal.getTime());//        
    }

    /**
     *               (           )
     *
     * @param starDate
     * @param endDate
     * @return
     * @author    
     * @date 2017-05-23
     */
    public static List getDayList(String starDate, String endDate) {
        SimpleDateFormat format = new SimpleDateFormat(defaultFormatStr);
        List dayList = new ArrayList();
        if (starDate.equals(endDate)) {
            dayList.add(starDate);
        } else if (starDate.compareTo(endDate) < 0) {
            while (starDate.compareTo(endDate) <= 0) {
                dayList.add(starDate);
                long l = stringToDate(starDate, "yyyy-MM-dd").getTime();
                starDate = format.format(l + 3600 * 24 * 1000);
            }
        } else {
            dayList.add(endDate);
        }
        return dayList;
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.getMonthDifference("2017-05-21", "2017-03-11"));
    }

}

全面的ですから、持ってきて直接使ってもいいです.もちろんjodaなどのログを操作する他のコンポーネントもあります.
ソースのダウンロード
この例の詳細ソース