user2531590
user2531590

Reputation:

Getting dates after certain period of time in Java

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.

Upvotes: 1

Views: 1101

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 339342

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?

Upvotes: 0

kai
kai

Reputation: 6887

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

Upvotes: 2

hemu
hemu

Reputation: 3261

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

Upvotes: 1

ARIJIT
ARIJIT

Reputation: 677

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

Upvotes: 0

Duncan Jones
Duncan Jones

Reputation: 69369

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

Upvotes: 1

Related Questions