ypc的gravatar头像
ypc 2012-10-12 08:33:03

java 获得一月前的日期

日期获得 一个月前的

package com.zuidaima.test;
import java.text.SimpleDateFormat;
import java.util.Date;




public class ttt {
    /**
     * @Title: lastWeek
     * @return
     * @Description: 获得一月前的日期
     */

    public static void main(String[] args) {
        System.out.println(lastMonth());
    }
    public static String lastMonth() {
        Date date = new Date();
        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(date)) - 1;
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
        
        if (month == 0) {
            year -= 1;
            month = 12;
        } else if (day > 28) {
            if (month == 2) {
                if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                    day = 29;
                } else
                    day = 28;
            } else if ((month == 4 || month == 6 || month == 9 || month == 11)
                    && day == 31) {
                day = 30;
            }
        }
        String y = year + "";
        String m = "";
        String d = "";
        if (month < 10)
            m = "0" + month;
        else
            m = month + "";
        if (day < 10)
            d = "0" + day;
        else
            d = day + "";
        return y + "-" + m + "-" + d;
    }
}

java 获得一月前的日期


最代码官方编辑于2013-12-20 18:15:20


打赏

文件名:ttt.java,文件大小:1K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友