【工具类】关于日期相关类(当前日期加减一、格式化日期)

1.格式化 当前日期 2013-01-02

 

    public String FormatCurrentDate(int year, int month, int day) {
        String format = null;
        if (month < 10) {
            if (day < 10) {
                format = year + "-0" + month + "-0" + day;
            } else {
                format = year + "-0" + month + "-" + day;
            }
        } else {
            if (day < 10) {
                format = year + "-" + month + "-0" + day;
            } else {
                format = year + "-" + month + "-" + day;
            }
        }
        return format;
    }
View Code

 

 

 

2.当前日期加一

 

    public String CurrentDateNext(String currentdate) {
        String next = null;
        int year = 0;
        int month = 0;
        int day = 0;
        Calendar tempCal = Calendar.getInstance();

        try {
            tempCal.setTime(todayFrofat.parse(currentdate));
            year = tempCal.get(Calendar.YEAR);
            month = tempCal.get(Calendar.MONTH) + 1;
            day = tempCal.get(Calendar.DAY_OF_MONTH);
            if (month == 1 || month == 3 || month == 5 || month == 7
                    || month == 8 || month == 10) {
                if (day < 31) {
                    day = day + 1;
                } else if (day == 31) {
                    day = 1;
                    month = month + 1;
                }
            } else if (month == 2) {
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    if (day < 29) {
                        day = day + 1;
                    } else if (day == 29) {
                        month = month + 1;
                        day = 1;
                    }
                } else {
                    if (day < 28) {
                        day = day + 1;
                    } else if (day == 28) {
                        month = month + 1;
                        day = 1;
                    }
                }
            } else if (month == 12) {
                if (day < 31) {
                    day = day + 1;
                } else if (day == 31) {
                    month = 1;
                    day = 1;
                    year = year + 1;
                }
            }else if (month == 4 || month == 6 || month == 9 || month == 11
                    ) {
                if (day < 30) {
                    day = day + 1;
                } else if (day == 30) {
                    day = 1;
                    month = month + 1;
                }
            } 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        next = FormatCurrentDate(year, month, day);
        return next;
    }
View Code

 

 

 

3.当前日期减一

 

    public String CurrentDatePre(String currentdate) {
        String pre = null;
        int year = 0;
        int month = 0;
        int day = 0;
        Calendar tempCal = Calendar.getInstance();
        try {
            tempCal.setTime(todayFrofat.parse(currentdate));
            year = tempCal.get(Calendar.YEAR);
            month = tempCal.get(Calendar.MONTH) + 1;
            day = tempCal.get(Calendar.DAY_OF_MONTH);
            if (day > 1) {
                day = day - 1;
            } else if (day == 1) {
                month = month - 1;
                if (month == 1 || month == 3 || month == 5 || month == 7
                        || month == 8 || month == 10 || month == 12) {
                    day = 31;
                } else if (month == 4 || month == 6 || month == 9
                        || month == 11) {
                    day = 30;
                } else if (month == 2) {
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                        day = 29;
                    } else {
                        day = 28;
                    }
                } else if (month < 1) {
                    day = 31;
                    month = 12;
                    year = year - 1;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        pre = FormatCurrentDate(year, month, day);

        return pre;
    }
View Code

 

 

 

 

转载于:https://www.cnblogs.com/androidsj/archive/2013/06/05/3119184.html

你可能感兴趣的:(【工具类】关于日期相关类(当前日期加减一、格式化日期))