• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple solution to Java Date/Calendar problems

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know Java programmers are known for struggling with dates. Setting your personal life aside ;-) .....here's a simple solution to dealing with Calendar objects and parts thereof.

It's not uncommon for an HTML UI/presentation layer for example, to have a ONE based selection list for month and pass that to the underlying code.

At this point I think we should think in Java, as it's now in the Java realm and keep it ZERO based, but opinions vary.
Here's a simple solution, that I used when several developers had worked on the same project. It's not rocket science, it's semantics!... and meant I could peacefully co-exist with other work that had been done on the project.

Simply ensure that a variable or method has a J to indicate that it's a value that's in the Java realm, e.g.

int jMonth

So, May would be jMonth=4 or month=5. Then you quickly know if you need to do a +/-1 when using the variable.

or

String jMonthLabel(int jMonth){
where this code return Jan, Feb etc for a given Integer, expecting it to be passed as zero based.
}

As I said it's not rocket science, but it's saved me a lot of tracing back through code to find out what mood the developer was in that day! I can't change the world, but I can make it easier to live in. I'm sure others have already said this or are doing this, but posting it makes it easier to find on Google!
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you simply use the Calendar constants like Calendar.JANUARY, Calendar.FEBRUARY etc.

As for converting those constant values into Strings, that's where DateFormatSymbols comes in:
DateFormatSymbols has been designed to use the Calendar constants as array indexes. That's why, for instance, getWeekDays() returns a String[] where the first element is an empty String - because weekdays are 1-based.

For instance:
And to top it off - it's locale specific if possible as well!

And please read your private messages.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I've always found Joda Time to be a great tool to use when it comes to messing around with dates.
It always seemed to be a bit more intuative than the Java API's offering

 
reply
    Bookmark Topic Watch Topic
  • New Topic