int day = Integer.parseInt(request.getParameter("day")); // 25
int month = Integer.parseInt(request.getParameter("month")); // 12
int year = Integer.parseInt(request.getParameter("year")); // 1988
System.out.println(year);
Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0);
b.setDob(c.getTime());
System.out.println(b.getDob());
Output is:
1988
Wed Jan 25 00:00:08 IST 1989
I am passing 25 12 1988
but I get 25 Jan 1989
. Why?
java.time
Using
java.time
framework built into Java 8If you need also information about time(hour,minute,second) use some conversion from
LocalDate
toLocalDateTime
See JavaDoc:
So, the month you set is the first month of next year.
That's my favorite way prior to Java 8:
I'd say this is a cleaner approach than:
Java's Calendar representation is not the best, they are working on it for Java 8. I would advise you to use Joda Time or another similar library.
Here is a quick example using LocalDate from the Joda Time library:
Here you can follow a quick start tutorial.
Months are zero-based in Calendar. So 12 is interpreted as december + 1 month. Use
Make your life easy when working with dates, timestamps and durations. Use HalDateTime from
http://sourceforge.net/projects/haldatetime/?source=directory
For example you can just use it to parse your input like this:
You can also specify patterns for parsing and printing.