3

I'm a little bit confused about using the class GregorianCalendar and Date.
What is the difference?

For instance: I have a field in a Person object that represents the date he was born. Do I have to use Date or GregorianCalendar?

What is the best use for them?

oers
  • 18,436
  • 13
  • 66
  • 75
Mazzy
  • 13,354
  • 43
  • 126
  • 207

3 Answers3

3

If you were writing down the details on paper, would you write down the date of birth as a date, or would you write down a calendar with a circle around the date?

You would write down the date. Date is a concrete date. Calendar is a tool for working with dates.

ewan.chalmers
  • 16,145
  • 43
  • 60
  • That is not the case. Calendar was Sun's second attempt to get dates right in the Java standard libraries. Arguably, they failed. See http://stackoverflow.com/questions/1571265/why-is-the-java-date-api-java-util-date-calendar-such-a-mess – artbristol Dec 14 '11 at 09:59
1

I would use Date to represent the date of birth. Use Calendar (and child classes) to manipulate dates.

Peter Jamieson
  • 745
  • 5
  • 10
1

Both Date and Calendar classes suffer from poor APIs. You should use the Joda Time library instead.

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • Which JodaTime class is best used for a date/time of birth? – Freiheit Jan 04 '13 at 18:33
  • @Freiheit It might be best to search for / ask that question separately. It depends on the context, but `LocalDate` is probably the most appropriate. – artbristol Jan 04 '13 at 21:24