Util工具類 獲取指定時間的最近半年時間月份第一天

String[] yeer = Util.getHalfYear(new Date());
for (int i = 0; i < yeer.length; i++) {
    System.out.println(yeer[i]);
}

結果:2017-5-01   2017-6-01   2017-7-01   2017-8-01   2017-9-01   2017-10-01


/**
 * 獲取指定時間的最近半年時間月份第一天
 * <p>
 * 2017年8月8日 17:19:10
 * xj
 *
 * @param date 指定日期
 * @return String[]
 */
public static String[] getHalfYear(Date date) {
    String[] last6Months = new String[6];
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    //要先+1,才能把本月的算進去
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    for (int i = 0; i < 6; i++) {
        //逐次往前推1個月
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        last6Months[5 - i] = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-01";
    }

    return last6Months;
}

更多工具類方法:http://blog.csdn.net/qq_34117825/article/details/78392976

發佈了106 篇原創文章 · 獲贊 255 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章