From 308da4113d395005204741494b71d85bcf252271 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 24 May 2019 13:26:02 +0400 Subject: [PATCH] fix week number calculation --- .../ru/ulstu/utils/timetable/TimetableService.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/utils/timetable/TimetableService.java b/src/main/java/ru/ulstu/utils/timetable/TimetableService.java index aa49fed..d65e276 100644 --- a/src/main/java/ru/ulstu/utils/timetable/TimetableService.java +++ b/src/main/java/ru/ulstu/utils/timetable/TimetableService.java @@ -3,6 +3,7 @@ package ru.ulstu.utils.timetable; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; +import ru.ulstu.core.util.DateUtils; import ru.ulstu.utils.timetable.errors.TimetableClientException; import ru.ulstu.utils.timetable.model.Lesson; import ru.ulstu.utils.timetable.model.TimetableResponse; @@ -50,9 +51,15 @@ public class TimetableService { } private int getCurrentWeek() { - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date()); - return (cal.get(Calendar.WEEK_OF_YEAR) + 1) % 2; + Date currentDate = Calendar.getInstance().getTime(); + currentDate = DateUtils.clearTime(currentDate); + + Calendar firstJan = Calendar.getInstance(); + firstJan.set(Calendar.MONTH, 0); + firstJan.set(Calendar.DAY_OF_MONTH, 1); + + return (int) Math.round(Math.ceil((((currentDate.getTime() - firstJan.getTime().getTime()) / 86400000) + + DateUtils.addDays(firstJan.getTime(), 1).getTime() / 7) % 2)); } private TimetableResponse getTimetableForUser(String userFIO) throws RestClientException {