1

Basically I have a date stored as text in this format: 16/09/2014 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.

  • no, the format should be yyyy/MM/dd (with / or without or with -) ... then the comparing of strings should act in the same way as dates comparing ... now you can do this with sqlite query with WHERE: `"data > ? AND date < ?"`and WHERE ARGS `new String[] {"2014/09/16", "2014/09/23" }` of course the date ranges should be done in Java code ... another way is to store dates as EPOCH time (integer) – Selvin Sep 16 '14 at 13:49
  • Nope, I do not want to touch it with SQL statement as I am trying to do in this way: I retrieve the dates from database, then I show it depends on the frequency like for example after one day, one week and so on. –  Sep 16 '14 at 13:56

5 Answers5

2

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.setTime(formatter.parse(dateAsString));

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);

Output:

After one day: 17/09/2014
After one week: 23/09/2014
After one month: 16/10/2014
After one year: 16/09/2015
kai
  • 6,702
  • 22
  • 38
  • Why -1 after each println? –  Sep 16 '14 at 14:06
  • I was asked to surround it with a try catch. But after added this portion of codes, my date does not show up anymore. Any ideas? –  Sep 16 '14 at 14:12
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). See ThreeTen-Backport and ThreeTenABP projects for Android. – Basil Bourque Aug 21 '17 at 21:51
1

With Joda-time:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("16/09/2014", formatter);

System.out.println(date.toString(formatter));
System.out.println(date.plusDays(1).toString(formatter));
System.out.println(date.plusWeeks(1).toString(formatter));
System.out.println(date.plusMonths(1).toString(formatter));
System.out.println(date.plusYears(1).toString(formatter));

Output:

16/09/2014
17/09/2014
23/09/2014
16/10/2014
16/09/2015
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • yeah, but it told me DateTimeFormatter cant resolve to a type and there's nothing to import. Any guides? –  Sep 16 '14 at 13:53
  • 1
    You would need to download the JARs associated with Joda-time (see link in answer) and add those to your project. – Duncan Jones Sep 16 '14 at 13:54
  • So I just download the JARs and import it into my project will do? –  Sep 16 '14 at 14:18
  • Well... you need to find the correct way to add JARs to your Android project. If you use Eclipse, this question might help: [How can I use external JARs in an Android project?](http://stackoverflow.com/q/1334802) – Duncan Jones Sep 16 '14 at 14:27
  • Thanks a lot! I've found another solution from other posts! –  Sep 16 '14 at 14:48
  • Android does not support Java-8. The only chance would be to use the Threeten-backport-project. Or using Joda-Time or Time4J?! The suggested code would have to be adjusted for those external libraries. – Meno Hochschild Sep 16 '14 at 14:52
  • @Meno I'm confused - my code is for Joda not Java 8. – Duncan Jones Sep 16 '14 at 15:41
  • @Duncan Ah now I see, both libraries look sometimes very similar in code (same class name `LocalDate`) then I was myself confused, sorry. – Meno Hochschild Sep 16 '14 at 16:06
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). See ThreeTen-Backport and ThreeTenABP projects for Android. – Basil Bourque Aug 21 '17 at 21:50
1

Use Calendar api of Java/Android as follow:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Date date;

try {
    date = sdf.parse(dateStr);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, 1); //add one day to your date
    cal.add(Calendar.MONTH, 1); //add 1 month to your date 
    cal.add(Calendar.YEAR, 1); //add 1 year to current date
    System.out.println(sdf.format(cal.getTimeInMillis()));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
hemu
  • 3,199
  • 3
  • 44
  • 66
0

Here is the example:

String strDate = "16/09/2014";
int noOfDays = 1;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, noOfDays);
ARIJIT
  • 676
  • 5
  • 7
0

tl;dr

LocalDate.parse( 
    "16/09/2014" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
.plusDays( 1 )
.format( DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) )

Details

Tip: Use date-time data types for date-time values. You should be using a date-oriented type to define your column in your database to store a date value rather than as text.

Tip # 2: When you do serialize a date value to text, use the standard ISO 8601 formats. These are sensible, practical, and sort chronologically when alphabetical.

Use the java.time classes rather than the troublesome old date-time classes that are now legacy. For Android, see bullets below.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( "16/09/2014" , f ) ;

LocalDate dayAfter = ld.plusDays( 1 ) ; 
LocalDate weekAfter = ld.plusWeeks( 1 ) ;
LocalDate monthAfter = ld.plusMonths( 1 ) ;
LocalDate yearAfter = ld.plusYears( 1 ) ;

To generate a string in standard format, simply call toString.

String output = dayAfter.toString() ;  // YYYY-MM-DD standard format.

2014-09-17

For other formats, use a DateTimeFormatter as seen above.

String output = dayAfter.format( f ) ;  

17/09/2014


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154