Flickering Holiday Lights Spring 2.x: Two pain points solved
Dec 05

20.weeks.ago vs. java.util.Calendar

Java, Ruby, Tech Add comments

After playing with a Rails project that needed to do a lot of date maniupulation, I was shown another of the many “small” differences that make you so glad you are in Ruby-land.

With Ruby, and the time extensions that Rails adds to the Ruby time module, you get to do things like:

20.minutes.ago
20.hours.from_now

now = Time.now
now.at_beginning_of_year
now.midnight
now.last_month
now.monday
now.next_year
now.tomorrow
now.yesterday
now.seconds_since_midnight

This is just a touch of what you can do.

The thought of doing a lot of this work in Java with the great java.util.Calendar and java.util.Date makes me cringe. I have a DateUtil that I use to do a lot of work, and there are other utils like JodaTime etc, but none compare in my opinion.

7 Responses to “20.weeks.ago vs. java.util.Calendar”

  1. Nadeem Says:

    You can do similar things with Joda in Java.

  2. Thomas Heller Says:

    Just be careful where/when you use such syntax sugar. 3.month.from_now for example equals 90 days from now not exactly 3 month. I agree that such little helpers are fun to use but when it comes to date manipulation the-hard-way (timezones, conversions, etc) you really wish for java.util.Calendar cause its actually good at it. Joda is also really good (even better actually).

  3. Guillaume Laforge Says:

    Hey, this is cool!
    That’d be a worthy addition to Groovy since we can do the same cool tricks ;-)
    You should add a JIRA ticket about that!

  4. Ron Says:

    Is there a ruby ide that can tell you all these things while you are typing?

    Now that I’ve gone eclipse, I cannot go back.

  5. Al Koscielny Says:

    See “Using the Ruby Development Tools plug-in for Eclipse” at http://www-128.ibm.com/developerworks/opensource/library/os-rubyeclipse/?ca=dgr-lnxw01Ruby4Eclipse

    A URL for the plugin is http://sourceforge.net/projects/rubyeclipse.

  6. mark Says:

    Java Calendar Component:
    http://www.yaodownload.com/software-development/components-libraries/java-calendar/

  7. jim Says:

    I am unable to find any feature that joda time has that java Date + java Calendar (+ a couple of Util classes easily obtainable) have. Personally, most of the complaints I see about java Date (and associated Calendar) are API related complaints (having to do with complexity (or perceived complexity) of the classes) and not with correctness.
    I am unable to find in the internet a ** single bug ** in the java Date + Time implementations that justifies joda.
    Now, what I AM worried about is just who are the people who implement Joda and why should I trust a rather complex date/Calendar logic to a bunch of volunteers?

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'