I Was So Lost I Was So Lost - 1 month ago 13
Android Question

Getting dates after certain period of time in Java

Basically I have a date stored as text in this format:

in SQLite Browser. I wonder is there any way to get the date after one day, one week, one month and one year of each records in the database using Java.

I retrieved and display the date retrieved from database in a listview:

viewHolder.txt_ddate.setText("Next Payment On: "
+ _recurlist.get(position).getRecurringStartDate().trim());

So I was thinking to use Java technique to get the dates I mentioned above. I have researched on this and found Documentation but I not sure how to implement it into my problem.

Any guides? Thanks in advance.

kai kai
Answer Source

Use a Calendar object like in your example, which provides the add method.

String dateAsString = "16/09/2014";
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();

c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println("After one day: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.DAY_OF_MONTH, -1);

c.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("After one week: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.WEEK_OF_YEAR, -1);

c.add(Calendar.MONTH, 1);
System.out.println("After one month: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.MONTH, -1);

c.add(Calendar.YEAR, 1);
System.out.println("After one year: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.YEAR, -1);


After one day: 17/09/2014
After one week: 23/09/2014
After one month: 16/10/2014
After one year: 16/09/2015