gpt4 book ai didi

输入日期为字符串的 Java 日期转换问题 Oct 2014

转载 作者:行者123 更新时间:2023-12-01 12:28:05 24 4
gpt4 key购买 nike

我需要实现输入日期(例如 2014 年 9 月、2014 年 10 月、2014 年 11 月等)是否为当前月份的逻辑。我已经编写了以下逻辑,用于使用以下方法检查所选周是否为当前周,但无法在当前月份实现

public static boolean isCurrentWeek(Date date, Date dateStart, Date dateEnd)

{

if (date != null && dateStart != null && dateEnd != null) {


if(date.equals(dateStart) || date.equals(dateEnd) ){


return true;


}else if (date.after(dateStart) && date.before(dateEnd)) {


return true;


}


else {


return false;

}

}
return false;
}

最佳答案

为此尝试 Apache Commons:

DateUtils.truncatedEquals( date, new Date(), Calendar.MONTH );

如果两个日期在同一个月,则返回true

编辑

也许是这样:

public static boolean isInCurrentMonth(String inputDateString) {
SimpleDateFormat sdf = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
try {
Date inputDate = sdf.parse(inputDateString);
return DateUtils.truncatedEquals(new Date(), inputDate, Calendar.MONTH);
} catch (ParseException e) {
// error handling
}
}

关于输入日期为字符串的 Java 日期转换问题 Oct 2014,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26180438/

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