3

In one sentence: Should I be able to parse "04 Dec 2014 pm 1:58" by PrettyTime?

Descriptive: I am in need to parse & get proper date format from some ill formatted date-time string. e.g. "04 Dec 2014 pm 1:58". When I do parse this example string, I get : "Thu Dec 04 02:59:33 ALMT 2014" which is I believe my current time stamp.

Consideration: if I had only this single ill-format, I could write my SimpleDateFormat. But there can a good varieties of formatting which are mostly going to be ill-formatted.

Can any of you kindly tell me, whether should I expect PrettyTime to parse for this type of ill-formatted string? Or could you please point to any Java library which can handle these type of ill formatted date strings in Java?

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
  • Is PrettyTime not mainly for formatting and not for parsing ? `But there can a good varieties of formatting`, so you need to cover the most important ones with SimpleDateFormat. – PeterMmm Dec 04 '14 at 08:28
  • @PeterMmm, PrettyTime is mainly for NLP. I was expecting it to be able to handle this sort of poorly formatted strings. However, lets see what our community says. If there is no direct solution, I have to write a good amount of SimpleDateFormat myself. :| – Amit K. Saha Dec 04 '14 at 09:02

2 Answers2

1

This might help you!

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy a hh:mm");
        String dateInString1 = "04 Dec 2014 pm 1:58";

        try {

            Date date = formatter.parse(dateInString1);
            System.out.println(date);
            System.out.println(formatter.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }
janasainik
  • 811
  • 5
  • 20
  • 40
  • With HH you will get 01:18, not 13:58. It's not correct! – Ruslan Ostafiichuk Dec 04 '14 at 08:31
  • Yes, it should be 'hh'. Corrected it! – janasainik Dec 04 '14 at 08:32
  • @hemanth, Thanks but as I said before, it is not the single one format. If it was the only format, I could code as you have replied. There are different formats & variations. So I was looking for any library or solution which does that part automatically. – Amit K. Saha Dec 04 '14 at 08:58
  • 2
    Seems this will not be possible(or some deep customization required!) as you should inform JVM about the format of date string that appears. – janasainik Dec 04 '14 at 09:01
  • @hemanth, agreed with you. but PrettyTime is a very good NLP. So I just hoped it may help me here :( – Amit K. Saha Dec 04 '14 at 09:17
0

Apparently, after days of various attempts, my finding is PrettyTime is not capable to do so though it handles pretty good NLP date & time related stuffs. Finally I ended up with various SimpleDateFormat . Not the best solution rather a temporary solution. If anyone stumble upon this thread, please share your experiences.

Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35