For a booking monitoring system I'm using a simple date class(not showing the full class):
package DataBase;
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "Date [day=" + day + ", month=" + month + ", year=" + year + "]";
}
}
I use this class because it simplifies a lot of my code when I need to process the bookings and put them in JTables.
I obtain the date from a JDatePicker which then put's it in an SQL database together with more info about the Booking. Then when the application is restarted the rows from the table are used to visualize the JTables.
My problem however is that I get strange behavior when converting the dates from the Java.util.Date
which I get from the database to my own date class above. I use this piece of code to convert them:
//Java Util Date, dateString is a string formatted like this: dd-MM-yyyy
Date date = new Date();
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
date = format.parse(dateString);
//my own date
DataBase.Date dateTemp = new DataBase.Date(date.getDay(),date.getMonth() ,date.getYear());
When I print dateString
& dateTemp.toString()
, I obtain the following output:
19-03-2017
Date [day=0, month=2, year=117]
The date above is correct but it's values for day, month and year on the second line don't match up.
another example:
21-03-2017
Date [day=2, month=2, year=117]
there seems to be a pattern to it but I can't seem to find what I'm doing wrong here!