Util工具类 获取指定时间的最近半年时间月份第一天

来源:互联网 发布:淘宝直通车在哪下载 编辑:程序博客网 时间:2024/05/22 00:30

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

阅读全文
1 0
原创粉丝点击