根据具体的天数返回日期

package com.wang.struts.util;

import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;

public class DateUtil {


public String getBackDate(int backDay) {
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String date = "";
int year = Integer.parseInt((today.split("-"))[0]);
int month = Integer.parseInt((today.split("-"))[1]);
int day = Integer.parseInt((today.split("-"))[2]);
if (day - (backDay - 1) > 0) {
return date + year + "-" + month + "-" + (day - (backDay - 1));
} else {
return date
+ getBackYear(year, month)
+ "-"
+ getLastMonth(month)
+ "-"
+ (getMonthDays(year, getLastMonth(month)) - (backDay - 1 - day));
}
}


public String getForwardDate(int forwardDay) {
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
int year = Integer.parseInt((today.split("-"))[0]);
int month = Integer.parseInt((today.split("-"))[1]);
int day = Integer.parseInt((today.split("-"))[2]);
if (day + forwardDay <= 28) {
return "" + year + "-" + month + "-" + (day + forwardDay);
} else {
switch (month) {
case 2:
return "" + getForwardYear(year, month) + "-"
+ getNextMonth(month) + "-"
+ (forwardDay - (getMonthDays(year, month) - day));

case 4:
if (day + forwardDay <= 30) {
return "" + year + "-" + month + "-" + (day + forwardDay);
} else {
return "" + getForwardYear(year, month) + "-"
+ getNextMonth(month) + "-"
+ (forwardDay - (getMonthDays(year, month) - day));
}
case 6:
if (day + forwardDay <= 30) {
return "" + year + "-" + month + "-" + (day + forwardDay);
} else {
return "" + getForwardYear(year, month) + "-"
+ getNextMonth(month) + "-"
+ (forwardDay - (getMonthDays(year, month) - day));
}

case 9:
if (day + forwardDay <= 30) {
return "" + year + "-" + month + "-" + (day + forwardDay);
} else {
return "" + getForwardYear(year, month) + "-"
+ getNextMonth(month) + "-"
+ (forwardDay - (getMonthDays(year, month) - day));
}

case 11:
if (day + forwardDay <= 30) {
return "" + year + "-" + month + "-" + (day + forwardDay);
} else {
return "" + getForwardYear(year, month) + "-"
+ getNextMonth(month) + "-"
+ (forwardDay - (getMonthDays(year, month) - day));
}

default:
if (day + forwardDay <= 31) {
return "" + year + "-" + month + "-" + (day + forwardDay);
} else {
return "" + getForwardYear(year, month) + "-"
+ getNextMonth(month) + "-"
+ (forwardDay - (getMonthDays(year, month) - day));
}
}
}
}


public boolean isRn(int year) {
if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
return true;
return false;
}


public int getLastMonth(int month) {
if (month != 1)
return month - 1;
else
return 12;
}


public int getNextMonth(int month) {
if (month != 12)
return month + 1;
else
return 1;
}


public int getBackYear(int year, int month) {
if (getLastMonth(month) == 12)
return year - 1;
else
return year;
}


public int getForwardYear(int year, int month) {
if (getNextMonth(month) == 1)
return year + 1;
else
return year;
}


public int getMonthDays(int year, int month) {
int lastMonthDays = 0;
switch (month) {
case 2:
if (isRn(year))
lastMonthDays = 28;
else
lastMonthDays = 29;
break;
case 4:
lastMonthDays = 30;
break;
case 6:
lastMonthDays = 30;
break;
case 9:
lastMonthDays = 30;
break;
case 11:
lastMonthDays = 30;
break;
default:
lastMonthDays = 31;
}
return lastMonthDays;
}

// private int d = 0 ;
// public int geDay(int year,int month,int backDay){
// if (getMonthDays(year,month) - (backDay - 1) > 0) {
// d = getMonthDays(year,month) - (backDay - 1);
// return d;
// }else{
// geDay(year,month,backDay);
//
// }
// return d;
// }

public static void main(String[] args) {
DateUtil du = new DateUtil();
// for (int m = 1; m <= 12; m++) {
// System.out.println("1900年" + m + "月的天数" + du.getMonthDays(1900, m));
// }
int sign = 1;
Scanner sc = new Scanner(System.in);
while (sign != 0) {
System.out.print("输入一个天数:");
sign = sc.nextInt();
System.out.println(du.getForwardDate(sign));
}
System.out.println("程序退出");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值