I tried a lot with different methods to find out exact solution, but I only get time difference, if I know future date but I want Next Sunday time from current time.
-
1What have you tried? What did your search and research bring up? There are plenty of like questions and answers here already, and we can focus our help to you much better if you specify what they give you and what you are still missing. – Ole V.V. Jul 21 '17 at 10:37
2 Answers
You can try this,
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int saturdayInMonth = calendar.get(Calendar.DAY_OF_MONTH) + (Calendar.SATURDAY - calendar.get(Calendar.DAY_OF_WEEK));
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
saturdayInMonth, 23, 59, 59); // This time will be sunday night 23 hour 59 min and 59 seconds
Date sunday = new Date(calendar.getTimeInMillis() + 1000); //this is 1 second after that seconds that is sunday 00:00.

- 11,122
- 3
- 31
- 41
-
1It’s a little bit tricky, though I believe it works. I would prefer the clarity you can get with [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) as in [Hugo’s answer](https://stackoverflow.com/a/45220666/5772882). – Ole V.V. Jul 21 '17 at 10:34
In Android, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes, together with ThreeTenABP (more on how to use it here). All the classes are in the org.threeten.bp
package.
To get a difference between 2 dates, you could use a org.threeten.bp.ZonedDateTime
, because this class takes care of Daylight Saving Time changes (and any other offset changes that might happen) and gives the correct/accurate result (if you calculate it without using a timezone, DST changes won't be considered in the calculation).
I also use org.threeten.bp.temporal.TemporalAdjusters
class, which has a built-in method to find the next specified day-of-week (by using the constants in the org.threeten.bp.DayOfWeek
class).
To get the difference, you can use org.threeten.bp.temporal.ChronoUnit.MILLIS
to get the difference in milliseconds (and then you use this value to display in whatever format you want). Or you can use another constants available in org.threeten.bp.temporal.ChronoUnit
class (such as MINUTES
or HOURS
, which will give the difference in minutes or hours - check the javadoc to see all units available).
Another way to get the difference is using a org.threeten.bp.Duration
which will contain the number of seconds and nanoseconds between the 2 dates.
// change this to the timezone you need
ZoneId zone = ZoneId.of("Asia/Kolkata");
// get current date in the specified timezone
ZonedDateTime now = ZonedDateTime.now(zone);
// find next Sunday
ZonedDateTime nextSunday = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
// get the difference in milliseconds
long diffMillis = ChronoUnit.MILLIS.between(now, nextSunday);
// get the difference as a Duration
Duration duration = Duration.between(now, nextSunday);
Note that I used the timezone Asia/Kolkata
. The API uses IANA timezones names (always in the format Region/City
, like America/Sao_Paulo
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like IST
or PST
) because they are ambiguous and not standard.
You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds()
.
The code above will get nextSunday
with the same time (hour/minute/second/nanosecond) as now
- except when there's a DST change (in this case, the time is adjusted accordingly).
But if you want to get the remaining time from now until the beginning of the next Sunday, then you have to adjust it to the start of the day before calculating the difference:
// adjust it to the start of the day
nextSunday = nextSunday.toLocalDate().atStartOfDay(zone);
Note that the start of a day is not always midnight - due to DST changes, a day can start at 01:00 AM for example (clocks might be set to 1 hour forward at midnight, making the first hour of the day to be 1 AM). Using atStartOfDay(zone)
guarantees that you don't have to worry about that, as the API handles it for you.
If the current date is already a Sunday, what's the result you want?
The code above will get the next Sunday, even if the current date is a Sunday. If you don't want that, you can use TemporalAdjusters.nextOrSame
, which returns the same date if it's already a Sunday.
To display the Duration
value in time-units (like hours, minutes and seconds), you can do the following:
StringBuilder sb = new StringBuilder();
long seconds = duration.getSeconds();
long hours = seconds / 3600;
append(sb, hours, "hour");
seconds -= (hours * 3600);
long minutes = seconds / 60;
append(sb, minutes, "minute");
seconds -= (minutes * 60);
append(sb, seconds, "second");
append(sb, duration.getNano(), "nanosecond");
System.out.println(sb.toString());
// auxiliary method
public void append(StringBuilder sb, long value, String text) {
if (value > 0) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(value).append(" ");
sb.append(text);
if (value > 1) {
sb.append("s"); // append "s" for plural
}
}
}
The result (in my current time) is:
47 hours 44 minutes 43 seconds 148000000 nanoseconds
If you want the milliseconds instead of nanoseconds, you could replace append(sb, duration.getNano(), "nanosecond")
with:
// get milliseconds from getNano() value
append(sb, duration.getNano() / 1000000, "millisecond");