Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

FAQ - Everything you ever wanted to know about dates

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it'd be nice to have a few pages on working with dates in Java in our wiki. So, I'm bringing attention to the topic with this post.
What I'd like to do here is gather ideas for what issues frequently arise when working with dates - things like parsing dates, formatting dates, comparing dates, and manipulating dates. After we've accumulated some ideas and links to questions and/or answers, if someone doesn't beat me to the punch, I'll start putting together the wiki pages.
(I think JDBC date issues would fit better in the JDBC FAQ section than here.)
Update: The topics so far
  • parsing a string to a date
  • formatting a date for display
  • comparing dates
  • date arithmetic
  • determining holidays (perhaps weekend days, day of week)
  • working with fields of Date/Calendar classes - especially that the first month is numbered 0
  • timezones
  • daylight savings time


  • (Again, I'd prefer to discuss JDBC/database issues with dates elsewhere.)
    Now, let's begin work on these at http://faq.javaranch.com/view?JavaDatesFaq
    [ April 30, 2004: Message edited by: Dirk Schreckmann ]
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Cool idea. Dates are a mess in Java, so any help would be appreciated.
    Some requirements I've had to meet in the past:
    * Convert from string to date & back. Eg write 12/31/1999 to plain text file, read it back into a date.
    * Compare the time portions of two dates ignoring the date portion. Ok, that's a bit too bizzare.
    * Add or subtract n days, months, hours, etc.
    * I have a current requirement to add n non-holiday business days. Do you know an existing class that works with a holiday schedule and skips weekends?
     
    Ranch Hand
    Posts: 1923
    Scala Postgres Database Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Difference between and pitfall: sql.Date and util.Date.
    Why is my month 0? (or 1 less than expected).
    What's the first day of week?
    There was a longer calculate-easter thread - in "Programming Diversions"?
     
    Ranch Hand
    Posts: 884
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How about including java.sql.Timestamp as well?
    Comparison of Timestamp is upto the time component & it takes a while to debug why the comparison between 2 Timestamp failed when their date component is entirely identical, only to realise that their time component is different.
     
    Ranch Hand
    Posts: 3271
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One place I always get hung up with regards to Dates are timezones. Quite often, I have dates coming from multiple timezones and I need to convert times from one timezone to another. Not only is that task difficult, but some places observe daylight savings time while others don't.
    I've found little in Java that confuses me as much as Dates do.
     
    Ranch Hand
    Posts: 815
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How to get one.
    [ April 30, 2004: Message edited by: Joseph George ]
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How to get one.
    A good place to start might be Solveig's Dating Design Patterns site.
    [ April 30, 2004: Message edited by: Dirk Schreckmann ]
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think we've put together a nice challenging list (which I distilled in the first post), so far.
    Let's start hacking away at creating some of the best explanations we can put together at http://faq.javaranch.com/view?JavaDatesFaq
     
    WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic