本文主要是介绍提取指定日期的前一天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
提取给定日期的前一天
/**
* 获取上一天的信息
* @param nowDate format:2001-11-11
* @retirm
*/
public static String getLastDayInfo(String nowDate) {
String yesterday = "";
int year = 0;
int month = 0;
int day = 0;
try {
System.out.println("nowDate: " + nowDate);
year = Integer.parseInt(nowDate.substring(0, nowDate.indexOf("-")));
month = Integer.parseInt(nowDate.substring(nowDate.indexOf("-") + 1,
nowDate.lastIndexOf("-")));
day = Integer.parseInt(nowDate.substring(nowDate.lastIndexOf("-") + 1));
System.out.println("nowYear = " + year);
System.out.println("nowMonth = " + month);
System.out.println("dayDay = " + day);
day = day - 1;
if (day == 0) {
month = month - 1;
if (month == 0) {
month = 12;
day = 31;
year = year - 1;
}
else {
switch (month) {
case 1:
day = 31;
break;
case 3:
day = 31;
break;
case 5:
day = 31;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 10:
day = 31;
break;
case 12:
day = 31;
break;
case 4:
day = 30;
break;
case 6:
day = 30;
break;
case 9:
day = 30;
break;
case 11:
day = 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
day = 29;
}
else {
day = 28;
}
}
}
}
String monthStr = "";
String dayStr = "";
if (month < 10) {
monthStr = "0" + String.valueOf(month);
}
else {
monthStr = String.valueOf(month);
}
if (day < 10) {
dayStr = "0" + String.valueOf(day);
}
else {
dayStr = String.valueOf(day);
}
yesterday = String.valueOf(year) + "-" + monthStr + "-" + dayStr;
}
catch (Exception e) {
System.err.println(e);
}
return yesterday;
}
* 获取上一天的信息
* @param nowDate format:2001-11-11
* @retirm
*/
public static String getLastDayInfo(String nowDate) {
String yesterday = "";
int year = 0;
int month = 0;
int day = 0;
try {
System.out.println("nowDate: " + nowDate);
year = Integer.parseInt(nowDate.substring(0, nowDate.indexOf("-")));
month = Integer.parseInt(nowDate.substring(nowDate.indexOf("-") + 1,
nowDate.lastIndexOf("-")));
day = Integer.parseInt(nowDate.substring(nowDate.lastIndexOf("-") + 1));
System.out.println("nowYear = " + year);
System.out.println("nowMonth = " + month);
System.out.println("dayDay = " + day);
day = day - 1;
if (day == 0) {
month = month - 1;
if (month == 0) {
month = 12;
day = 31;
year = year - 1;
}
else {
switch (month) {
case 1:
day = 31;
break;
case 3:
day = 31;
break;
case 5:
day = 31;
break;
case 7:
day = 31;
break;
case 8:
day = 31;
break;
case 10:
day = 31;
break;
case 12:
day = 31;
break;
case 4:
day = 30;
break;
case 6:
day = 30;
break;
case 9:
day = 30;
break;
case 11:
day = 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
day = 29;
}
else {
day = 28;
}
}
}
}
String monthStr = "";
String dayStr = "";
if (month < 10) {
monthStr = "0" + String.valueOf(month);
}
else {
monthStr = String.valueOf(month);
}
if (day < 10) {
dayStr = "0" + String.valueOf(day);
}
else {
dayStr = String.valueOf(day);
}
yesterday = String.valueOf(year) + "-" + monthStr + "-" + dayStr;
}
catch (Exception e) {
System.err.println(e);
}
return yesterday;
}
这篇关于提取指定日期的前一天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!