I am carrying out an activity and I have placed a jcalendar to my form, I want that when I select a jtable, said calendar returns to place the date that I had saved. I managed to capture this value and create a date from the strings, the year and day are correct, but the month gives me the month + 1, that is, if I put the month February 02 date_birth I will be saving the month March 03 and in turn in the jcalendar will be showing that month.
int fila = this.table_candidatos.getSelectedRow();
String fecha = elect1.getCandidatos().get(fila).getFecha_nac();
String[] tiempo = fecha.split("-");
Calendar fecha_nac = Calendar.getInstance();
fecha_nac.set(Integer.parseInt(tiempo[0]), Integer.parseInt(tiempo[1])-1, Integer.parseInt(tiempo[2]));
// System.out.println(tiempo[0] + " " + tiempo[1] + " " + tiempo[2]);
// System.out.println(fecha_nac.getTime());
this.calendar_fecha.setCalendar(fecha_nac);
The problem you have is from the Class
Calendar
that I had to solve the problems withDate
and I only complicate everything more, because itCalander
has many design problems that make it unintuitive.What happens to you is that in
Calendar
January it is0
not1
, yes, I know what you are thinking, whoever thought that it was a good idea that the months should go from 0-11 but when you format the Calendaryyyy-MM-dd
if you put it1
in the output will show2
the month that is correct, the problem is only in the methodset
, so from the date you get inString fecha = elect1.getCandidatos().get(fila).getFecha_nac();
you must subtract one from the month that you pass to the methodset
.