How to parse a date from dd.mm.yyyy format, add a certain number of days to it and decompose the resulting date into a day, a month and a year?

A large number of classes of work with dates, as well as a large number of obsolete methods are misleading and incomprehensible. How can I solve my problem? The minimum API version in the project is 19.

  • joda.org/joda-time - Stranger in the Q
  • @StrangerintheQ really, to work with the date in Java, you need to use third-party libraries? - mtrfnv
  • There is a built-in SimpleDateFormat, but it will be harder to do some conversion with it - Stranger in the Q
  • For date manipulations there is such a wonderful class. developer.android.com/reference/java/util/Calendar - Eugene Krivenja 8:31 pm

1 answer 1

You can use the built-in SimpleDateFormat

 SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); Date date = formatter.parse("31.01.1983"); Date newDate = new Date(date.getTime() + TimeUnit.DAYS.toMillis(2)); String formattedDate = formatter.format(newDate); System.out.println("formattedDate = " + formattedDate); 

will lead

formattedDate = 02.02.1983