gpt4 book ai didi

java - 查询两个日期之间的数据

转载 作者:行者123 更新时间:2023-12-02 11:33:15 25 4
gpt4 key购买 nike

我需要从在线服务器中托管的 Mongodb 数据中获取两个日期之间的数据。我尝试了这段代码,它在我的本地主机(本地数据和实时数据)中运行良好。但是当我在线上传该应用程序时,它在 Live 中无法正常工作。

现场结果不准确。它获取指定日期之前和之后的一些记录。例如,我给出日期 01-02-2018 和 28-02-2018,结果将包含 31-01-2018 和 01-03-2018 的记录。

我认为问题是日期存储在 UTC 时区( 2018-02-15T23:33:30.000Z )中。

代码:

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");      

Date fromDate = format.parse(from + " 00:00:00");
Date toDate = format.parse(to + " 23:59:59");

Calendar cal = Calendar.getInstance();
cal.setTime(order.getOrderDate());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String strDate = sdf.format(cal.getTime());
Date orderDate = sdf.parse(strDate);

for(Order order : orders){
if(orderDate.after(fromDate) || orderDate.equals(fromDate) && orderDate.before(toDate) || orderDate.equals(toDate)){
//do something
}
}

最佳答案

java.util.Date doesn't have a timezone in it ,因此解析和格式化订单日期是没有意义的。格式化将其转换为 String ,解析将其转换回 Date ,这是没有意义的,因为订单日期已经是 Date 对象。

您必须在first格式化程序(format变量)中设置时区,然后解析fromto 日期:它们将被设置为加尔各答时区的相应日期和时间 - 在这种情况下它是有效的,因为您有字符串并希望将它们转换为日期。

然后,您使用额外的括号进行比较,以避免任何歧义(如评论中所指出的)。将 Date 设置为 Calendar 是没有意义的,只是为了稍后取回它 - Calendar 实例在您的代码中没有任何用途。

getOrderDate 的调用不应该在 for 循环内?

完整的代码如下:

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");      
// from and to dates are from Kolkata's timezone, so the formatter must know that
format.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));

// dates will be equivalent to this date and time in Kolkata, although
// the date itself doesn't store the timezone in it
Date fromDate = format.parse(from + " 00:00:00");
Date toDate = format.parse(to + " 23:59:59");

for(Order order : orders){
Date orderDate = order.getOrderDate();
// note the extra parenthesis, they make all the difference
if( (orderDate.after(fromDate) || orderDate.equals(fromDate)) &&
(orderDate.before(toDate) || orderDate.equals(toDate)) ) {
....
}
}

如果您的 Java >= 8,最好使用 java.time API:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MM-yyyy");
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime fromDate = LocalDate
// parse "from" string
.parse(from, fmt)
// start of day at Kolkata timezone
.atStartOfDay(zone);
ZonedDateTime toDate = LocalDate
// parse "to" string
.parse(to, fmt)
// get start of next day
.plusDays(1).atStartOfDay(zone);

// convert the Date to ZonedDateTime
for (Order order : orders) {
ZonedDateTime orderDate = order.getOrderDate().toInstant().atZone(zone);
if ((orderDate.isAfter(fromDate) || orderDate.isEqual(fromDate)) && (orderDate.isBefore(toDate))) {
...
}
}

这是一个不同的代码,因为这个 API 引入了新的类型和概念,但它比以前的 API 有很大的改进(DateCalendar 很困惑、有错误并且过时) .

花一些时间研究这个 API,这是完全值得的:https://docs.oracle.com/javase/tutorial/datetime/

关于java - 查询两个日期之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49131828/

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