-2

Hello guys I have recently been assigned the task to gather some data from excel sheets and put them in a data base the main requirement here is that the data must be robust.The difficult part here is that many fields are dates and they don't have standard format so I can parse them with a normal date parser. For example There are dates that look like this "4-Mar 02:35" like this "5-Mar" like this

"2.2.13 6:30
" 

like this "23/2/13 17:45" and like this "2.2.13 6.30" is there any way to parse them in a manner that no additional information that does not exist is added to the end date ?

ABC
  • 4,263
  • 10
  • 45
  • 72
Nikos
  • 387
  • 2
  • 15
  • 1
    What are you trying to parse *to*? `java.util.Date` (etc) have no idea of a format... if you need to keep the original value as well, just keep it separately from the parsed value. – Jon Skeet Nov 14 '14 at 14:41
  • 1
    You can take a look at these links : 1- http://stackoverflow.com/questions/4216191/intelligent-date-time-parser-for-java 2- http://stackoverflow.com/questions/15010210/automatic-date-time-parser-without-specifying-format – zack Nov 14 '14 at 14:41

1 Answers1

1

I'd use JodaTime (http://www.joda.org/joda-time/) and it's flexibel parser.

With this, you can define several patterns for a DateTimeparser.

For example:

DateTimeFormatter dateTimeFormatter=
DateTimeFormat.forPattern("dd/MM/yy hh:mm") ;

DateTimeFormatter dateTimeFormatterWithTimeZone=
DateTimeFormat.forPattern("d.M.YY hh:mm");

DateTimeFormatter optionalTimeZoneFormatter=
new DateTimeFormatterBuilder()
.append(null, //because no printing is required
new DateTimeParser[]{dateTimeFormatter.getParser(),
dateTimeFormatterWithTimeZone.getParser()}).toFormatter();

It's explained in more details in this blogpost: http://edgblog.wordpress.com/2014/02/17/jodatime-goodies-flexible-parsing/

Best Regards

questionaire
  • 2,475
  • 2
  • 14
  • 28
  • thanks for that but can you give me an example what is the way to parse this kind of dates "4-Mar 02:35" – Nikos Nov 14 '14 at 15:25
  • 1
    You have to define it by the Pattern. [An overview of the constans can be found here](http://www.joda.org/joda-time/key_format.html). The two examples in my answer above are already example for you give dates. The first formatter will parse `"23/2/13 17:45"` the second `"2.2.13 6.30"` In order to parse a Month given by its name you have to use `MMM` as far as I remember. Also have a look at the link of the Blog. – questionaire Nov 14 '14 at 15:43