• 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown

Format Number as Date

Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem in SimpleDateFormatter and it will be nice if any one help me.
As we know a user can set any valid format to a SimpleDateFormatter.e.g "mm d", "MM/dd/yy" etc.
Now I want that if I have set a format to a SimpleDateFormatter (say "MM/dd:yy")and if I pass a string of numbers say "123456" it should format the string in the specified format.(ie. 12 should be taken as month 34 should be taken as day (can convert 34 to 30+4 or can throw error.))Similar to what MS Excel does when we set format of a cell of MS excel to DateFormat.
Well DateaFormatter expects the input to be formatted in the same pattern as that was set (i.e if we have set the pattern as "MM d:yy" it expects value as 11:11:11 to format else it will trhow exception)
Any suggestions, ideas is welcome.
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple ideas come to mind:
1). Have a whole bunch of SimpleDateFormats -- one for each expected input format.
Set up a series of regexes and compare the input string to them to see which SimpleDateFormat to use.
2). Write your own DateFormat that ignores all non-digit characters. It would then translate the string as described. Thus, 12/25/04 12.25.04, 12-25-04, 122504, 12-25-2004, etc., would all be translated the same way.
I wrote the following Java version of option 2 several years ago (it was based on an some Objective-C code that was used to do the same thing)
Please note that this is not the whole class, just the sample methods.

[ September 23, 2003: Message edited by: Joel McNary ]
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic