header

Torsten Curdt’s weblog

Java Dates and other crap

Is it just me who thinks that the java time/date/calendar handling just sucks? Here are some points that come to my mind straight away…

  • The distinction between date and time is blurry. Basically because there is no Time object that just takes the hour, second and millisecond values.
  • Why are the explicit constructors of the Date object deprecated. Well, probably because java aims to support different Calendars. Fine! But where is the connection between them. Shouldn’t the Calendar then act as Date-Factory?
  • Let’s say you want to increase you position in the timeline by one day. What you want to do is to add a day to the date which reflects a certain point inside the Calendar. But with the current API you add it to the calendar. Just think about this sentence and you see how awkward this is. Using the Calendar object as the main computation object just feels wrong.
  • …I could go on and on

Does anyone know a good alternative? Google revealed the joda time API which on the first glance looks much better. Any other suggestions?

  • Tom Jones
    I think this is the right way to handle the problem until the Java date implementation is fixed, if ever. If you roll your own abstraction you can collect all of the nasty date code into a small implementation and use a nicer, more application specific version in your own code.

    http://checkedexception.blogsp...
  • bah
    Date time sucks. Computers sucks.
  • hoc
    4 years and nothing has changed much.
    I found this site by typing "java calendar is s**t" into google for some stress relief.
  • Dude, you are not the only one. Java date time really sucks.
  • Sam
    Yep Java dates are so gay. Why the hell would months start at zero but not days? .Net dates are a lot nicer to use. Operator overloading makes things easy. The TimeSpan type is also useful and simple. 'Date - Date' gives a timespan. date + timespan gives a new date! Who'd a thunk it could be so simple...
  • r080
    > Richard C

    exactly, starting months from zero? wtf? I spent hours finding why the hell is actual date one month in the past...

    I'm here because I needed to find someone who complains about it just to calm myself down. Thanks! :)
  • Richard C
    The one-size-fits all approach to date/time completely sucks. 99% of the time you want to do something really simple and its incredibly hard (and probably executes slow too). Also, the javadoc in this area is very poor. Also, the default week-numbering configuration has changed in Java5, just to really piss you off! And which bright spark decided months should start at zero! To be consistent shouldn't the day of the month start at zero too?
  • Mini
    How can i convert a date from one timezone to another ?
  • Mini
    Can anybody tell me how to convert a time between different timezones in java?
    Say in india time is 10:35 Am , now i want to find equivalent time in America or Australia , How can i achieve that through java ?
  • "hi can someone help to devise a function that will find the date three weeks from the current system date"



    Using Chronos (http://www.chronos-st.org): 'YearMonthDay today + 3 weeks.'



    "I just want a simple way to get the current year, as an int."



    Using Chronos: 'YearMonthDay today year' (or else 'GregorianCalendar clock thisYear'; 'HebrewCalendar clock thisYear'; etc.)



    And to convert from one time zone to another:



    (Timepoint nowIn: 'Asia/Jerusalem') in: 'Pacific/Auckland'



    And to convert between calendars:



    (Timepoint nowIn: 'Asia/Jerusalem' as: #Hebrew) as: #Persian
  • Nedlud
    I just want a simple way to get the current year, as an int.



    I see the Joda thing does this. Why does everything have to so convoluted in Java?
  • > hi can someone help to devise a function that will find the date three weeks from the current system date



    Using Joda-Time:



    DateTime now= new DateTime();

    DateTime threeWeeksHence = now.plus(Period.weeks(3));
  • kofi
    hi can someone help to devise a function that will find the date three weeks from the current system date
  • Still I think the whole concept is really blurry in terms of time vs date information. Even if you see a Date as the immutable brother of the Calendar. This API has too many short comings to be called straight forward. Maybe time for some good thinking...
  • Berin Loritsch
    If you think of it as the similarity between String and StringBuilder, then the Date/Calendar relationship becomes a bit more clear. The problem arizes in trying to treat the Date object like a mutable object. BTW, if you want to combine the time from one Date object and the Date from another, it would be done like this:



    Date combined = new Date( date.getTime() + time.getTime() );



    This is also how you can make the conversion from one time zone to the next. I developed my own Date object that would make translations for you. Essentially, it assumed that any times provided to the constructor were the local timezone (Timezone.getTimeZone()), although you could provide the timezone. It stored everything in UTC, and translated back when necessary.



    It works pretty good for that purpose. However, I have never understood why the Calendar does not change any information when you set the TimeZone differently than what was already there.
  • Gert Thiel
    Joda Time - Java date and time API

    http://joda-time.sourceforge.net/



    DateTime dt = new DateTime();

    int year = dt.getYear();

    String monthText = dt.monthOfYear().getAsText(Locale.ENGLISH);

    String monthInFrench = dt.monthOfYear().getAsText(Locale.FRENCH);

    String dateAsISO8601Format = dt.toString();



    (Have not use it but wanted to offer a helpful link).
  • Anton Tagunov
    Sure, I feel the same. There are two things we do in practice:



    1) we use Calendar instances for dates (in fact if you look into JDK code you'll see that Date uses a static instance of Calendar internally if any of its get* methods is called, and access to this instance is synchronized, phhh!)



    2) when I need strictly hour and minute I use my own HourMinute class, with my own formatters, so much simpler




    Should we probably set up our brains and come up with an alternative API for date handling? Not easy, but challenging ;-)
  • Nope, it's definitely not just you. It's nearly impossible to get your head around all of the Java date stuff correctly. Particularly annoying is how to sort out conversions between timezones. The thing that bit me the other day as well is you can perform arithmetic on the seconds values returned by Date. So if you have two dates, one which is the day and one which is the time, you can't just add the two getSeconds() values together. Sigh....
  • Yes, date time sucks. If there is one thing worse, it is XML Schema date and time constructs. Oh, and the mapping of XSD to java times.



    The only representation that works consistently well is time_t in UTC; java.util.Date. but conversion to real time is troublesome, especially once you add in timezones and summer times. Life would be easier if they didn't exist.
blog comments powered by Disqus