gpt4 book ai didi

java - 通过在java中传递开始和结束日期获取给定日期的周数

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:06 24 4
gpt4 key购买 nike

我正在尝试获取给定日期在开始日期和结束日期之间的周数。下面是我曾经得到的代码,但它只能工作 4 周。

我需要它来让它工作任意几周。

public static int getWeekNO( Timestamp startTime,Timestamp endTime , Timestamp dateTime) throws RuntimeException {

String formatted = null;
Timestamp currentTimestamp;
int weekNumber = 0;

logger.debug("startTime"+startTime);
logger.debug("endTime"+endTime);
logger.debug("dateTime"+dateTime);
try {
String formatDateFirst = "";
String formatDateSecond = "";
String formatDateThird = "";
String formatDateFourth = "";
String givenDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateTime);
String startDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime);
String endDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime);
logger.debug("givenDateString"+givenDateString);
logger.debug("startDateString"+startDateString);
logger.debug("endDateString"+endDateString);


SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Calendar c = Calendar.getInstance();
Date givenDate = sdf.parse(givenDateString);
Date startDate = sdf.parse(startDateString);
Date endDate = sdf.parse(endDateString);

logger.debug("givenDate"+givenDate);
logger.debug("startDate"+startDate);
logger.debug("endDate"+endDate);

c.setTime(sdf.parse(startDateString));

c.add(Calendar.DAY_OF_MONTH, 6); // number of days to add
formatDateFirst = sdf.format(c.getTime());
Date firstWeekDate = (Date) sdf.parse(formatDateFirst);
logger.debug("endDate"+firstWeekDate);

c.add(Calendar.DAY_OF_MONTH, 7); // number of days to add
formatDateSecond = sdf.format(c.getTime());
Date secondWeekDate = (Date) sdf.parse(formatDateSecond);
logger.debug("endDate"+secondWeekDate);

c.add(Calendar.DAY_OF_MONTH, 7); // number of days to add
formatDateThird = sdf.format(c.getTime());
Date thirdWeekDate = (Date) sdf.parse(formatDateThird);
logger.debug("endDate"+thirdWeekDate);

c.add(Calendar.DAY_OF_MONTH, 7); // number of days to add
formatDateFourth = sdf.format(c.getTime());
Date fourthWeekDate = (Date) sdf.parse(formatDateFourth);
logger.debug("endDate"+fourthWeekDate);


if (givenDate.compareTo(firstWeekDate) <= 0) {
weekNumber = 1;
} else if (givenDate.compareTo(secondWeekDate) <= 0
&& givenDate.compareTo(firstWeekDate) > 0) {
weekNumber = 2;
} else if (givenDate.compareTo(thirdWeekDate) <= 0
&& givenDate.compareTo(secondWeekDate) > 0) {
weekNumber = 3;
}else if (givenDate.compareTo(fourthWeekDate) <= 0
&& givenDate.compareTo(thirdWeekDate) > 0) {
weekNumber = 4;
}else{
logger.debug("Not a Valid Date");
}

} catch (Exception ex) {
throw new RuntimeException(ex);
}
return weekNumber;
}

示例:

  start Timestamp: 09/04/2016 00:00:00,End Timestamp: 28/06/2016 00:00:00. Given Timestamp : 15/05/2016 00:00:00

so here:week 1:09/04/2016 - 15/04/2016
week 2:16/04/2016 - 22/04/2016
week 3:23/04/2016 - 29/04/2016
week 4:30/04/2016 - 06/05/2016
week 5:07/05/2016 - 13/05/2016
week 6:14/05/2016 - 20/05/2016 -- my given timestamp falls here
week 7:21/05/2016 - 27/05/2016
week 8:28/05/2016 - 03/06/2016

so now my output for week no should be week:6

这里我使用的是java 1.7,所以我无法使用--import java.time.LocalDateTime;导入 java.time.temporal.ChronoUnit;

我怎样才能实现,请帮助我。

最佳答案

    public static int getWeekNO(Timestamp startTime, Timestamp endTime, Timestamp dateTime) {
if (dateTime.compareTo(startTime) < 0 || dateTime.compareTo(endTime) > 0) {
System.out.println("Not a Valid Date");
return -1;
}
return (int) ((dateTime.getTime() - startTime.getTime() - 1) / (1000L * 3600 * 24 * 7)) + 1;
}
<小时/>

编辑:

根据要求,更新了答案,

public static int getWeekNO(Timestamp startTime, Timestamp endTime, Timestamp dateTime) {
if (dateTime.compareTo(startTime) < 0 || dateTime.compareTo(endTime) > 0) {
System.out.println("Not a Valid Date");
return -1;
}
int dayDiff = (int) ((dateTime.getTime() - startTime.getTime()) / (1000L * 3600 * 24)) + 1;
int week = dayDiff / 7;
return dayDiff % 7 == 0 ? week : week + 1;
}

注意:要小心Date/Timestamp的构造函数,

  Timestamp startTime = new Timestamp(2016, 5, 30, 0, 0, 0, 0);
Timestamp dateTime = new Timestamp(2016, 6, 6, 0, 0, 0, 0);

这个构造函数月份从 0 开始。所以上面的代码是比较 2016-4-30 和 2016-5-6。只有一周时间。

关于java - 通过在java中传递开始和结束日期获取给定日期的周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37625733/

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