125
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?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
JAVAGeek
  • 2,674
  • 8
  • 32
  • 52
  • 2
    What is b in the code? – MysteryGuy May 16 '17 at 16:32
  • For new readers to the question I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). See [the answer by Przemek](https://stackoverflow.com/a/33892250/5772882). – Ole V.V. Jun 23 '21 at 05:47

6 Answers6

137

Months are zero-based in Calendar. So 12 is interpreted as december + 1 month. Use

c.set(year, month - 1, day, 0, 0);  
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 6
    IBM API designers, JavaScript API designers. Other than that, probably nobody. Note that Calendar is now obsoleted by th the Java 8 java.time API, which does the right thing. – JB Nizet Sep 27 '17 at 06:55
88

That's my favorite way prior to Java 8:

Date date = new GregorianCalendar(year, month - 1, day).getTime();

I'd say this is a bit cleaner than:

calendar.set(year, month - 1, day, 0, 0);
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
  • 6
    Month could be specified using `Calendar` constants eg. `Calendar.FEBRUARY`. – Geekarist Jan 08 '18 at 06:25
  • 1
    Beware, the months used to be numbered from zero in Java 7. Use constants to avoid trouble. – jediz Jun 06 '18 at 09:31
  • 1
    A point to make - the two render different results when calling `getTime()`. Setting info in the constructor gives time at the start of day where as the latter approach uses the current time. See the [docs](https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#GregorianCalendar()). So the first is probably a better approach, assuming what people are actually trying to do. – funct7 Oct 01 '20 at 22:30
  • 1
    This is returning a Date, which by definition is in UTC. But with your JVM *not* also set to UTC you won't be getting what you expect. The returned Date will have what the UTC time was at the beginning of the day of the date you entered (assuming constructor form). I entered my birthday and got the date before, b/c the TZ of my computer is + hours from UTC. – Chris Murphy Jul 22 '21 at 16:55
42

java.time

Using java.time framework built into Java 8

int year = 2015;
int month = 12;
int day = 22;
LocalDate.of(year, month, day); //2015-12-22
LocalDate.parse("2015-12-22"); //2015-12-22
//with custom formatter 
DateTimeFormatter.ofPattern formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate.parse("22-12-2015", formatter); //2015-12-22

If you need also information about time(hour,minute,second) use some conversion from LocalDate to LocalDateTime

LocalDate.parse("2015-12-22").atStartOfDay() //2015-12-22T00:00
Przemek
  • 7,111
  • 3
  • 43
  • 52
  • Good Answer, but specify a time zone if known. A `LocalDate` has no time zone and so does not represent an exact moment on the timeline. If your context indicates a time zone, apply it to get a `ZonedDateTime` object: `LocalDate.parse("2015-12-22").atStartOfDay( ZoneId.of( "America/Montreal" ) )` – Basil Bourque Aug 30 '16 at 18:43
11

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:

LocalDate localDate = new LocalDate(year, month, day);
Date date = localDate.toDate();

Here you can follow a quick start tutorial.

James
  • 3
  • 2
Fdiazreal
  • 785
  • 1
  • 8
  • 13
6

See JavaDoc:

month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.

So, the month you set is the first month of next year.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
zw324
  • 26,764
  • 16
  • 85
  • 118
0

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:

HalDateTime mydate = HalDateTime.valueOf( "25.12.1988" );
System.out.println( mydate );   // will print in ISO format: 1988-12-25

You can also specify patterns for parsing and printing.

Jemolah
  • 1,962
  • 3
  • 24
  • 41
  • 3
    It looks like you’re promoting your own library class? That’s probably permitted, but it would look nicer if you mention if you’re a main developer of `HalDateTime`. – Ole V.V. Apr 16 '17 at 00:00
  • I did not downvote. However life never is easy with dates. Your library wont change that fact. – kiltek May 27 '19 at 10:32
  • Why downvote? My library dates from 2013. All functionality is available in java.time as specified in JSR 310 which I personally find is one of the best designed APIs. I fully recommend using that one. – Jemolah May 30 '19 at 07:48