gpt4 book ai didi

java - 如何在 Java 中处理日期

转载 作者:行者123 更新时间:2023-11-29 06:03:49 24 4
gpt4 key购买 nike

我有一个 java 程序,我可以在 yyyy-MM-dd 中设置一个日期,我可以从中获取下一个和上一个日期。以下方法可以完成工作,

public void setDate(String date) {
StringTokenizer st = new StringTokenizer(date, "-");
year = Integer.parseInt(st.nextToken());
monthNo = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
date = year + "-" + monthNo + "-" + day;
}

public String getPreviousMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();

if (maxDate) {
calendar.set(year, monthNo, 1);
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
} else {
calendar.set(year, monthNo, day);
}

if (monthNo == 0) {
calendar.add(calendar.MONTH, -1);
} else {
calendar.add(calendar.MONTH, -1);
}
String date = (calendar.get(calendar.YEAR)) + "-" + (calendar.get(calendar.MONTH) + 1) + "-" + calendar.get(calendar.DAY_OF_MONTH);
calendar.clear();
return date;
}

public String getNextMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();

if (maxDate) {
calendar.set(year, monthNo, 1);
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
} else {
calendar.set(year, monthNo, day);
}
if (monthNo == 11) {
calendar.add(calendar.MONTH, 1);
} else {
calendar.add(calendar.MONTH, 1);
}




public String getCurrentMonth(boolean maxDate){
Calendar calendar = Calendar.getInstance();
if (maxDate) {
calendar.set(year, monthNo, 1);
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
} else {
calendar.set(year, monthNo, day);
}
String date = (calendar.get(calendar.YEAR)) + "-" + (calendar.get(calendar.MONTH) + 1) + "-" + calendar.get(calendar.DAY_OF_MONTH);
calendar.clear();
return date;
}

当我将日期设置为一月和二月时,它会给出正确的输出,但如果我选择除一月和二月以外的其他月份,我将无法获得正确的日期,以下是一些结果,

this is OK
2012-1-31 - current month
2011-12-31 - previous month
2012-2-29 - next month

2012-2-29 current month
2012-1-29 previous month ***day should be 31
2012-3-29 next month ***day should be 31

this is OK
2011-12-31 current month
2011-11-30 previous month
2012-1-31 next month

2011-11-30 current month
2011-10-30 previous month *** this should be 31
2011-12-30 next month *** this should be 31

this is OK
2011-12-31 current month
2011-11-30 previous month
2012-1-31 next month

请告诉我哪里错了...

最佳答案

为您修复 getNextMonth。 getPreviousMonth 需要做同样的事情。请参阅以下代码内嵌的注释:

  public String getNextMonth(boolean maxDate) {
Calendar calendar = Calendar.getInstance();

if (maxDate) {
calendar.set(year, monthNo, 1);
/* Move these two lines to end of method.
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);
*/
} else {
calendar.set(year, monthNo, day);
}
//Not sure why have you used if/else. Both are doing the same thing
if (monthNo == 11) {
calendar.add(calendar.MONTH, 1);
} else {
calendar.add(calendar.MONTH, 1);
}
//here i.e. After you have calculated your next month.
int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
calendar.set(year, monthNo, maxD);

}

关于java - 如何在 Java 中处理日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9205572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com