• 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

Parsing of dates in general format?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone have suggestions for parsing of dates in general/unspecified formats? In other words, I want something that can parse either "6/14/2005" or "June 14, 2005" to the same Date value. If I have a specific Date format in mind, I can use DateFormat.parse. But if I don't want to require a specific Date format, the only thing I'm aware of is Date.parse, and that is deprecated.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use regular expressions to get date from a string and set a date with the results. This gives you more flexability as to what formats you can match, and stops you running into ParseExceptions. However, if you don't restrict the formats your application can support, you will always run into problems. For example 6/7/05 - is that the 6th of July 2005 or the 7th of June 2005?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generally avoid deprecated methods, but the Date object is an exception. It's got a number of methods that I find easier to use than the corresponding Calendar methods. And it's not like the deprecation is due to actual functional problems (like Streams vs. Readers).
If the warning really bothers you, you could lift the method from the JDK source and put it into your utilities library (not an option if you want to redistribute it, of course - check the JDK source license).
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic