時間管理のためのツール(日付シフト、日付形式、曜日の取得など)

1.指定された日付形式(文字列形式)を返します

 public static String getDate(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdFormat = new SimpleDateFormat(format);

        return sdFormat.format(date);
    }

1.1、指定された日付を返します(日付形式)

 public static Date getDate(String date, String format) {
        try {
            return new SimpleDateFormat(format).parse(date);
        } catch (Exception e) {
            //e.printStackTrace();
            return null;
        }
    }

2.日付シフト

 public static Date moveDate(Date date, int moveDistance) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, moveDistance);
        return calendar.getTime();
    }

3.年次シフト

 public static Date moveYear(Date date, int moveYear) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, moveYear);
        return calendar.getTime();
    }

4.月ごとにシフト

 public static Date moveMonth(Date date, int moveDistance) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, moveDistance);
        return calendar.getTime();
    }

5.分単位でシフト

public static Date moveMinute(Date date, int moveDistance) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, moveDistance);
        return calendar.getTime();
    }

6.秒単位でパン

public static Date moveSecond(Date date, int moveDistance) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, moveDistance);
        return calendar.getTime();
    }

7.何曜日に戻るか

 public static String getWeekDay(Date date) {
        SimpleDateFormat sdFormat = new SimpleDateFormat("EEEE");
        return sdFormat.format(date);
    }
 public static String getWeekDay(String date, String format) {
        //这里的getDate在上面有写
        return getWeekDay(getDate(date, format));
    }

8.現在の日がである月の最初の日をクエリします

public static Date getMonthFirstDate(Date date) {
        return getDate((new SimpleDateFormat("yyyy-MM").format(date) + "-01"),
                "yyyy-MM-dd");
    }

 

おすすめ

転載: blog.csdn.net/qq_36138652/article/details/108241417