Johnny Tsai
Johnny Tsai

Reputation: 41

How to get a Date object from year,month and day?

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

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

When I passed the values 2015, 8 and 5, I got

Sun Sep 05 00:00:00 CST 3915

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

Day Class is defined by myself.

Upvotes: 1

Views: 12664

Answers (6)

Basil Bourque
Basil Bourque

Reputation: 338614

tl;dr

java.util.Date.from                         // Avoid `java.util.Date` if at all possible. But if you must, you can convert from modern `java.time.Instant`. 
(
    LocalDate                               // Represent a date-only value.
        .of ( 2025 , Month.JANUARY , 23 )   // Or use month number, 1-12 for January-December. 
        .atStartOfDay ( ZoneOffset.UTC )    // Returns a `ZonedDateTime` object. 
        .toInstant()                        // Returns a `Instant` object extracted from the `ZonedDateTime` object. 
)

java.time

In modern Java 8+, use only java.time. Never use the terribly-flawed legacy classes such as Date & Calendar.

To represent a date only value, use LocalDate.

LocalDate ld = LocalDate.of( year , month , day ) ;

Note that, unlike the legacy classes, the java.time classes use sane numbering. That includes 1-12 for months January-December.

Or use a named month in the Month enum.

LocalDate ld = LocalDate.of( 2025 , Month.JANUARY , 23 ) ;

If you want to get the first moment of the day on that date as seen in UTC (an offset of zero hours-minutes-seconds), use Instant class.

Instant instant = 
    LocalDate
        .of ( 2025 , Month.JANUARY , 23 ) 
        .atStartOfDay ( ZoneOffset.UTC ) 
        .toInstant() ;

See that code run at Ideone.com where we call Instant#toString.

2025-01-23T00:00:00Z

You should avoid java.util.Date if at all possible. But if you must, you can convert.

java.util.Date d = Date.from( instant ) ;

Upvotes: 1

SubOptimal
SubOptimal

Reputation: 22973

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

Upvotes: 7

Thomas
Thomas

Reputation: 88707

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. :)

Upvotes: 4

Halko Karr-Sajtarevic
Halko Karr-Sajtarevic

Reputation: 2268

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.

Upvotes: 1

rhitz
rhitz

Reputation: 1892

  • 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

Upvotes: 0

Ayoub
Ayoub

Reputation: 1435

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

Upvotes: 0

Related Questions