1 year ago
#362922
dschreid
Time between 2 dates at the beginning of each day
I have written the following code:
public static int getDaysTillActivation() {
ZoneId z = ZoneId.of("Europe/Berlin");
int day = ZonedDateTime.now(z).getDayOfMonth();
for (int i = 0; i < 31; i++) {
if (day % 2 == 0) {
return i;
}
day++;
}
return -1;
}
public static String getTimeString() {
int daysTillActivation = getDaysTillActivation();
if (daysTillActivation == 0) {
return "Active";
}
ZoneId z = ZoneId.of("Europe/Berlin");
ZonedDateTime zdtStart = ZonedDateTime.now(z);
ZonedDateTime zdtStop = ZonedDateTime.now(z).plusDays(daysTillActivation).truncatedTo(ChronoUnit.DAYS);
long millis = ChronoUnit.MILLIS.between( zdtStart , zdtStop );
return formatDuration(millis);
}
It works, for now. But before I use it in some sort of "production", I would like to hear some advises on what could be better, or what it could break...
Here's my general Idea: I want to have a feature be enabled only every 2 days, therefore I would like to display to the user how much time is left for it to be enabled
For Example:
- Mon 30.Jan - Enabled
- Thu 31.Jan - Disabled
- Wed 1.Feb - Disabled
- .... - Enabled
As you can see, I don't mind if they have multiple days off. That's why I went with simply checking if the current day is dividable by 2.
However, this code will run every second and I just want to make sure I get everything right/get the best performance out of it.
java
time
duration
days
0 Answers
Your Answer