1

I'm spending some time with Java again after a long break on the .NET side. I came across this code:

Date date = new Date(Date.UTC(y - 1900, m - 1, d, h, M, s));

Unfortunately Date.UTC has been deprecated for a while. So, what is an equivalent replacement that won't cause compiler warnings?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 1
    The Date constructor you are using is also deprecated, not just the Date.UTC. – Martin Apr 28 '11 at 17:03
  • @Strawberry, really? It's not in JDK 1.6. To be clear, it's `Date(long)`. – Drew Noakes Apr 28 '11 at 17:41
  • http://download.oracle.com/javase/6/docs/api/java/sql/Date.html#Date(long) – Drew Noakes Apr 28 '11 at 17:41
  • Whoops sorry I thought it was the java.util.Date and in fact java.util.Date(long) isn't actually deprecated... Two mistakes for the price of one :) – Martin Apr 28 '11 at 17:43
  • @Strawberry, actually it is a java.util.Date, I just pasted the wrong link. But it's not deprecated either: http://download.oracle.com/javase/6/docs/api/java/util/Date.html#Date(long) – Drew Noakes Apr 28 '11 at 18:01

3 Answers3

4

Try this

GregorianCalendar cal = new GregorianCalendar();
cal.set(year, month, day,
        hour, minute, second);
Date date = cal.getTime();

You GregorianCalendar also supports setting the TimeZone if needed.

Jason
  • 126
  • 4
  • I ran this in a unit test and it doesn't give the same `Date` as the one in the question. – Drew Noakes Apr 28 '11 at 18:13
  • The Date (getTime() in milliseconds) is not as the same as above? Or the actual Date (Month/Day/Year HH:MM:SS)? Make sure that the TimeZone is set correctly. – Jason Apr 28 '11 at 21:33
2

Use Calendar Specifically use set() method, Also there is very good API joda time

Update

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(y, m, d, h, M, s);
Date date = cal.getTime();
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Yeah use Calendar.getInstance() with TimeZone of UTC then build use the set method as above. – Dave G Apr 28 '11 at 17:02
  • I am making a library for others to consume so I don't want to force a dependency on them. `set` returns `void`, so what do I do after that? – Drew Noakes Apr 28 '11 at 17:04
  • 1
    It will set the specified time into calendar object, if you want to retrieve `Date` then invoke `getTime()` on calendar instance – jmj Apr 28 '11 at 17:05
  • What a weird API. I can see why people like joda so much :) – Drew Noakes Apr 28 '11 at 17:09
  • Do I need to subtract 1900 still? I'll accept any answer that shows equivalent code in full, like I asked in the question. – Drew Noakes Apr 28 '11 at 17:10
  • @Drew Why weird :) ? It seems you are trying to write same API :) – jmj Apr 28 '11 at 17:11
  • Well "To get the Date, call getTime" is a little counter-intuitive for a start. Also, what is the magic string that gives you a UTC calendar? – Drew Noakes Apr 28 '11 at 17:14
  • I ran this in a unit test and it doesn't give the same `Date` as the one in the question. – Drew Noakes Apr 28 '11 at 18:12
  • It will give the date representation on your system's timezone – jmj Apr 28 '11 at 18:13
1

Use Joda-time. It's just awesome and a huge leap from the standard Java Date/Time libraries:

new DateTime(year, monthOfYear, dayOfMonth, hourOfDay, 
             minuteOfHour, secondOfMinute, millisOfSecond, 
             DateTimeZone.UTC);

But if you don't like having all those params which are easily confused you can also use take builder-style approach:

new DateTime()
    .withYear(2011)
    .withMonthOfYear(6)
    .withDayOfMonth(12)
    // etc...
    .withZone(DateTimeZone.UTC);

Each call to withXxxx() returns a copy so DateTime remains immutable.

alpian
  • 4,668
  • 1
  • 18
  • 19
  • Cheers, but I'm producing a library for others to consume and don't want to introduce a new dependency. – Drew Noakes Apr 28 '11 at 18:13
  • @Drew: fair enough - but you could use Maven assembly to bundle everything together into your library for distribution. – alpian Apr 28 '11 at 18:17