0

I made a function that was supposed to add one day to the date string parsed, it worked like a charm except it's decrementing the month by one value per function call. I don't know why it's decrementing that way, I just wanted to increment the day by one.

Here's the fx :

private void setTarikh() throws ParseException {
    FragmentJadual parentFragment = (FragmentJadual) getParentFragment();
    int lastTarikh = parentFragment.hariArray.size() - 1;
    String tarikhNewData = parentFragment.hariArray.get(lastTarikh).getTarikhHari();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd / MM / yyyy");
    Date d = dateFormat.parse(tarikhNewData);
    Calendar calender = Calendar.getInstance();
    calender.setTime(d);
    calender.add(Calendar.DATE, 1);
    SimpleDateFormat sdf1 = new SimpleDateFormat("dd / MM / yyyy");
    String output = sdf1.format(calender.getTime());
    mNewDay = calender.get(Calendar.DAY_OF_MONTH);
    mNewMonth = calender.get(Calendar.MONTH);
    mNewYear = calender.get(Calendar.YEAR);
    mTarikhTv.setText("" + mNewDay + " / " + mNewMonth + " / " + mNewYear);
}

I really tried looking at other functions that had problems but I can't even find a hint. Please help, at least to confirm that this function itself is true. I hate bugs but love them at the same time..!

  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Nov 06 '19 at 04:53

2 Answers2

2

tl;dr

LocalDate
.parse(
    "23/01/2020" ,
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
.plusDays( 1 ) 
.toString()

2020-01-24

Avoid legacy date-time classes

As others said, your likely problem is the crazy counting used by the Calendar class, running 0-11 for months January to December. One of many reasons to never use Calendar class.

The terrible date-time classes bundled with the earliest versions of Java were supplanted years ago by the industry-leading java.time classes.

java.time

To parse a string in an format of day-month-year separate by the / character, define a formatting pattern with the DateTimeFormatter class, and parse as a LocalDate.

String input = "23/01/2020" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;

ld.toString(): 2020-01-23

Add your day increment.

LocalDate dayAfter = ld.plusDays( 1 ) ;

dayAfter.toString(): 2020-01-24

This has already been discussed many times on Stack Overflow. So search to learn more.


Table of all date-time types in Java, both modern and legacy


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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android.

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

Instead of

mTarikhTv.setText("" + mNewDay + " / " + mNewMonth + " / " + mNewYear);

Use

mTarikhTv.setText(output);

Besides this, it's not a problem or bug, it's a normal behaviour. If you try to look inside Calendar class you will find Month start here from 0-11 meaning 0-JANUARY and 11-DECEMBER.

System.out.println(Calendar.MARCH);
Output: 2 // Instead of 3
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46