1

When I used the following code, the Date Object was wrong.

Date date = new Date(day.getYear(), day.getMonth(), day.getDay());

Can anyone tell me how to get the Date Object From the value of year, month and day?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Johnny Tsai
  • 41
  • 1
  • 2
  • 8

5 Answers5

6

You can use the Calendar class to achieve this.

public static void main(String[] args) throws Exception {
    Date date = new Date (115, 7, 5);
    System.out.println("date     = " + date);

    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 5);
    calendar.set(Calendar.MONTH, 7);
    calendar.set(Calendar.YEAR, 2015);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    date = calendar.getTime();
    System.out.println("calendar = " + date);

    // or create directly a new clanedar instance
    // thanks Tom to mention this
    calendar = new GregorianCalendar(2015, 7, 5);
    date = calendar.getTime();
    System.out.println("calendar = " + date);
}

output

date     = Wed Aug 05 00:00:00 CEST 2015
calendar = Wed Aug 05 00:00:00 CEST 2015
calendar = Wed Aug 05 00:00:00 CEST 2015
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • If you're using a `GregorianCalendar`, then why you're not using `new GregorianCalendar(2015, 7, 5)` instead of setting each field manually? – Tom Aug 05 '15 at 08:38
  • @Tom Good question. Maybe I had something else in mind. (it's early in the morning ;-) ) – SubOptimal Aug 05 '15 at 08:44
  • Btw, `date = new Date(calendar.getTimeInMillis());` can be shortened to `date = calendar.getTime();` :) – Thomas Aug 05 '15 at 08:48
  • @Thomas It's not my day today... `inject coffee - brain 1.0 needs a reboot` ;-) – SubOptimal Aug 05 '15 at 09:07
  • `Your caffeine levels are critically low, please find a coffee maker immediately to prevent brain shutdown.` - I feel the same ;) – Thomas Aug 05 '15 at 09:11
4

Without knowing more I'd have to guess but probably you didn't read the JavaDoc on that deprecated constructor:

year the year minus 1900.
month the month between 0-11.
date the day of the month between 1-31.

As you can see, if you want to create a date for today (Aug 5th 2015) you'd need to use new Date (115, 7, 5);

If you see that documentation you are free to guess why this is deprecated and should not be used in any new code. :)

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • when I used new Date(115,7,5) get the year is 115 not 2015 – Johnny Tsai Aug 05 '15 at 08:18
  • @JohnnyTsai in that case please add more info, e.g. the exact class name (including package), Java version used, how you check the year etc. Also note the JavaDoc on `getYear()`: `the year represented by this date, minus 1900.`, i.e. you'd get 115 back. – Thomas Aug 05 '15 at 08:25
  • 2
    @JohnnyTsai Because `Date.getYear()` returns the passed year (*115*) and not the correctly adjusted year. For example try `new Date(115,7,5).toString()` and then stop using deprecated methods. Btw some more research would be great. – Tom Aug 05 '15 at 08:27
1

You can do it with a workaround if you're stuck to Java < 8, but it's very ugly:

java.util.Date date = new java.text.SimpleDateFormat("dd.MM.yyyy").parse("05.08.2015");

as @Thomas already stated, the default Constructor for date/month/year is deprecated. Probably take a look at this link if you have access to Java8.

Halko Karr-Sajtarevic
  • 2,248
  • 1
  • 16
  • 14
0

you should use getDate() instead of getDay() method to get the day because getDay() return the day of week not the day of month

Ayoub
  • 1,417
  • 14
  • 24
0
  • int getYear() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

  • int getMonth() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

  • int getDay() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK).

Hence use :

Date date = new Date(Calendar.get(Calendar.YEAR) - 1900, Calendar.get(Calendar.MONTH), Calendar.get(Calendar.DAY_OF_WEEK));

Recomendation : Use Joda-Time

rhitz
  • 1,892
  • 2
  • 21
  • 26
  • 1
    If you're setting up a `Calendar` anyways, you'd not need to use that constructor at all. Just make sure to set the time part to 0 (hour, minutes, seconds, millis) and call `getTime()` on the calendar. – Thomas Aug 05 '15 at 08:28
  • As for the recommendation: I agree with you for Java 7 and prior. Java 8 has a new date api which is similar to Joda Time. – Thomas Aug 05 '15 at 08:29
  • @Thomas , you are absolutely correct, but as the OP requirement is `Date` object, this is a solution. – rhitz Aug 05 '15 at 08:33