gpt4 book ai didi

java - 如何在指定的日期范围内找到给定的日期

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

这是我的代码

boolean isWithinRange(String d)
{
boolean withinDate = false;
try
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(d);
withinDate = !(date.before(startDate) || date.after(endDate));
}
catch (ParseException parseException)
{
}
return withinDate;
}

输入

2015-11-26
2015-11-26 - 复制

两者都返回 true 但我需要的是“2015-11-26”应该是 true 而“2015-11-26 - Copy”应该是 false。

最佳答案

这是因为SimpleDateFormat愉快地解析“2015-11-26”并忽略“- Copy”部分。

javadoc状态:

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

要检测整个字符串是否已被使用,请改用 parse(String source, ParsePosition pos)方法。 ParsePosition 告诉您解析停止的位置。只需将其与原始日期字符串的长度进行比较即可。

关于java - 如何在指定的日期范围内找到给定的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216949/

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