-7

The code below works well for rounding java calendar time to the nearest 15mins, however how can I change the code below to allow for rounding to the closest 5mins clock mark.

Code:

int unroundedMinutes = isha_jamaat_cal.get(Calendar.MINUTE);
int mod = unroundedMinutes % 15;
System.out.println("#######mod: " + mod);
isha_jamaat_cal.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod));
Ossama
  • 2,401
  • 7
  • 46
  • 83
  • 1
    Change `15` to `5` ... – MadProgrammer Mar 17 '18 at 22:50
  • 1
    Change 15 to 5 on the second and fourth lines, and change 8 to 3 on the fourth line perhaps? – Dawood ibn Kareem Mar 17 '18 at 22:50
  • not that easy mate, what about this?? it also needs to be changed mod < 8 ? -mod : (15-mod) – Ossama Mar 17 '18 at 22:51
  • Yes, I told you how to do that. – Dawood ibn Kareem Mar 17 '18 at 22:51
  • I should not be getting voted down @MadProgrammer – Ossama Mar 17 '18 at 22:52
  • 1
    oh yes you should. – Mike Nakis Mar 17 '18 at 22:53
  • 2
    Getting voted down is one thing people can do, and how do YOU know that specific user did so? Note: I did NOT vote down or up here, just pointing it out. Better perhaps to ask WHY people are doing so. – Mark Schultheiss Mar 17 '18 at 22:54
  • now i got -3 hhhh – Ossama Mar 17 '18 at 22:55
  • well, it is consistent with what you have been receiving on the majority of your questions on stackoverflow. You must have a special skill for posting questions that rarely receive a score above zero. – Mike Nakis Mar 17 '18 at 22:57
  • Casual suggestion, take the code you have, attempt what is suggested THEN ask/revise with "I am attempting to round time to nearest 5 minutes....(show code attempted) then What have I done wrong here?" – Mark Schultheiss Mar 17 '18 at 23:00
  • @Ossama Not the down voter - and that's not your call - Rounding time has been asked many times before so far all I see is "I have found some code can anyone modify for me" - no attempt to solve the problem yourself. Should we also discuss the choice of `Calendar` in 2018 – MadProgrammer Mar 17 '18 at 23:09
  • @Ossama The same question/answer you took the above code also demonstrated using `LocalDateTime` `LocalDateTime lastQuarter = time.truncatedTo(ChronoUnit.HOURS).plusMinutes(15 * (time.getMinute() / 15));` which would be significantly easier to modify – MadProgrammer Mar 17 '18 at 23:13
  • Thanks mate, you may need to speed feed me edits to the suggestion you just made. noobie here – Ossama Mar 17 '18 at 23:15
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 18 '18 at 01:35
  • Duplicate and similar: [this](https://stackoverflow.com/q/3553964/642706) & [this](https://stackoverflow.com/q/27082097/642706) & [this](https://stackoverflow.com/q/25552023/642706) & [this](https://stackoverflow.com/q/37613071/642706) & [this](https://stackoverflow.com/q/49020441/642706) & more. – Basil Bourque Mar 18 '18 at 01:41

1 Answers1

0

I didn't test this code but i believe it should work.

int unroundedMinutes = isha_jamaat_cal.get(Calendar.MINUTE);
int mod = unroundedMinutes % 5;
isha_jamaat_cal.add(Calendar.MINUTE, mod < 3 ? -mod : (5-mod));

The basic idea is to check whether the current time is nearest to the next or the previous 5 minutes clock mark and adjusting the added value based on this.

Amr Keleg
  • 336
  • 4
  • 11
  • 1
    Can we not use `Calendar` anymore – MadProgrammer Mar 17 '18 at 23:12
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 18 '18 at 01:42
  • it's cos he took the code from here https://stackoverflow.com/a/3553994/1280060 (written in 2010) – jdex Jul 05 '22 at 03:04