1

NB: yes I realize that using simple date format may well be better than my current system, but I find this easier than using simple date format.

This code should simply return the date saved as dd/mm/yyyy, and it does so for the loan date 1/1/2013, however for the dueBack date it returns 22/4/2013, it should be returning 22/1/2013.

public String returnLoan()
{
    String dLoan = loan.get(loan.DAY_OF_MONTH) + "/" 
                     + loan.get(loan.MONTH + 1) + "/" 
                     + loan.get(loan.YEAR);
    return dLoan;
}

public String returnDueBack()
{
    String dDueBack = dueBack.get(dueBack.DAY_OF_MONTH) + "/" 
                        + dueBack.get(dueBack.MONTH + 1) + "/" 
                        + dueBack.get(dueBack.YEAR);
    return dDueBack;
}

for reference this is the constructor

public Loan()
{
    memID = 0;
    bookID = 0;
    loan = new GregorianCalendar(2013, 0, 1);
    dueBack = new GregorianCalendar(2013, 0, 22);
    returned = false;
}

Any help is greatly appreciated.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 3
    "NB: yes I realize that using simple date format may well be better than my current system, but I find this easier than using simple date format." Kind of seems like a good time to learn, rather than hacking around the problem. – Anthony Grist Nov 19 '13 at 14:37
  • 2
    In particular, your code isn't doing 0-padding - whereas if you just create a `SimpleDateFormat` with a pattern of `dd/MM/yyyy` it will work *really* simply. And you wouldn't have the current problem. How can you say that the "current system" is easier than using `SimpleDateFormat` when the current system doesn't work yet? – Jon Skeet Nov 19 '13 at 14:43

2 Answers2

9

Instead of

dueBack.get(dueBack.MONTH + 1)

use

dueBack.get(dueBack.MONTH) + 1
Henry
  • 42,982
  • 7
  • 68
  • 84
  • 1
    They need a new Calendar system which uses enums for these things, instead of constants. Less prone to errors like these – Cruncher Nov 19 '13 at 14:36
  • @Cruncher It's going to get better in Java 8 with the new [`java.time`](http://download.java.net/jdk8/docs/api/java/time/package-summary.html) package. – Jesper Nov 19 '13 at 14:39
  • @Jesper It looks good. It seems strange that it's taken this long for a decent time package. Of course Jon Skeet is going to yell at me, telling me that it's really not that easy. – Cruncher Nov 19 '13 at 14:53
  • 2
    @Cruncher The new `java.time` package is based on the popular [Joda Time](http://www.joda.org/joda-time/) library. If you can't use Java 8, then I highly recommend using Joda Time if you need to do anything with dates and times. Its API is much better than what `java.util.Date` and `java.util.Calendar` offer. – Jesper Nov 19 '13 at 15:12
0

tl;dr

LocalDate.parse(                                // Represent a date-only value, without time-of-day and without time zone.
    "22/4/2013" , 
     DateTimeFormatter.ofPattern( "d/M/uuuu" )  // Formatting pattern to match input.
)                                               // Returns a `LocalDate` object.
.plusMonths( 1 )                                // Move to the next month. Returns another `LocalDate` object.
.toString()                                     // Generate a String in standard ISO 8601 format.

2013-05-22

java.time

Much easier to use the modern java.time classes.

Define a formatting pattern to match your input.

String input = "22/4/2013" ; 
DateTimeFormatter f = DateTimeFormatter.ofPattern( "d/M/uuuu" ) ;

Parse as a LocalDate, a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.parse( input , f ) ;

Add a month.

LocalDate ldNextMonth = ld.plusMonths( 1 ) ;

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.

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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

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