You could use java.time
and it is supported in lower Android APIs, because there's API desugaring in Android now.
There's a zone-aware class (java.time.ZonedDateTime
) and an offset-aware one (java.time.OffsetDateTime
), but your example String
just contains an offset from GMT / UTC. That's why I would use an OffsetDateTime
that parses the exact moment in time and then adds a day.
Here's a simple example that defines a formatter which parses the given String
and uses it for the output:
public static void main(String[] args) {
// example String
String date = "Fri Dec 18 23:00:00 GMT+02:00 2020";
// create a formatter that is able to parse and output the String
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu",
Locale.ENGLISH);
// parse the String using the formatter defined above
OffsetDateTime odt = OffsetDateTime.parse(date, dtf);
System.out.println("OffsetDateTime parsed is\t" + odt.format(dtf));
// add a day to the date part
OffsetDateTime dayLater = odt.plusDays(1);
System.out.println("Adding a day results in\t\t" + dayLater.format(dtf));
}
This outputs
OffsetDateTime parsed is Fri Dec 18 23:00:00 GMT+02:00 2020
Adding a day results in Sat Dec 19 23:00:00 GMT+02:00 2020
If you are interested in outputting dates only (no time part or offset), there's another handy thing in those classes, that is easy extraction of date- or time-part. You can do the following with an OffsetDateTime
, for example:
// extract the part that only holds information about day of month, month of year and year
LocalDate dateOnly = odt.toLocalDate();
// print the default format (ISO standard)
System.out.println(dateOnly);
// or define and use a totally custom format
System.out.println(dateOnly.format(
DateTimeFormatter.ofPattern("EEEE, 'the' dd. 'of' MMMM uuuu",
Locale.ENGLISH)
)
);
That would output
2020-12-18
Friday, the 18. of December 2020
In case you are dealing with a DatePicker datePicker
, you can receive selected values by getYear()
, getMonth()
and getDayOfMonth()
, then create a
LocalDate localDate = LocalDate.of(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
and then simply add a day by localDate.plusDays(1);