2

I have a String "Saturday, Oct, 25th, 11:40"

What format does this date has? How can I parse the ordinal indicator?

Here is how i want to convert it

private String changeDateFormat(String stringDate){

        DateFormat dateFormat = new SimpleDateFormat("DD, MM, ddth, hh:mm");
        try {
            Date date = dateFormat.parse(stringDate);
            dateFormat = new SimpleDateFormat("ddMMMyyyy");
            stringDate=dateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return stringDate.toUpperCase();
    }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jenya Kyrmyza
  • 327
  • 1
  • 5
  • 16
  • 1
    Everything you need to know about `DateFormat` - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Abubakkar Oct 28 '15 at 11:36
  • how could i define the "25th"? i mean "th" thing – Jenya Kyrmyza Oct 28 '15 at 11:41
  • @JenyaKyrmyza Check out the is previous SO question - http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java – wadda_wadda Oct 28 '15 at 11:42
  • 2
    I suspect you're going to have to remove the ordinal from the date string before you attempt to parse it... – JonK Oct 28 '15 at 11:51
  • @JonK you're right. I also was thinking about it. It will always be two chars and always stay after first number in string – Jenya Kyrmyza Oct 28 '15 at 11:54
  • 2
    [This answer](http://stackoverflow.com/a/6987802/3419894) shows a regex that removes the English language ordinals from a date string, you'd have to change the pattern for i18n though. – JonK Oct 28 '15 at 11:55
  • @JonK i used this solution, thanks – Jenya Kyrmyza Oct 28 '15 at 12:04

4 Answers4

2

Hope this program solve your problem.

public class Test
{
  public static void main(String[] args)  {
    System.out.println(new Test().getCurrentDateInSpecificFormat(Calendar.getInstance()));
  }

  private String getCurrentDateInSpecificFormat(Calendar currentCalDate) {
    String dayNumberSuffix = getDayNumberSuffix(currentCalDate.get(Calendar.DAY_OF_MONTH));
    DateFormat dateFormat = new SimpleDateFormat("E, MMM, dd'"+ dayNumberSuffix +"', HH:mm");
    return dateFormat.format(currentCalDate.getTime());
  }

  private String getDayNumberSuffix(int day) {
    if (day >= 11 && day <= 13) {
      return "th";
    }
    switch (day % 10) {
    case 1:
      return "st";
    case 2:
      return "nd";
    case 3:
      return "rd";
    default:
      return "th";
    }
  }

}
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
1

The java doc has lot of information on how to parse a date from String in different formats :

SimpleDateFormat Java Doc

You can try with this, but play around this and refer java doc until you are able to solve your problem :

DateFormat dateFormat = new SimpleDateFormat("E, MMM, dd'th' HH:mm");
Date date = dateFormat.parse("Saturday, Oct, 25th, 11:40");

Try different combinations, that way you will learn more about SimpleDateFormat and how it works with different formats.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1

SimpleDateFormat doesn't seem to easily handle day numbers followed by "st" "th" etc. A simple solution would be remove that part of the original string. E.g.

int comma = original.lastIndexOf(',');
String stringDate =
        original.substring(0, comma - 2) +
        original.substring(comma + 1);

After that just use this format on stringDate:

SimpleDateFormat("EE, MM, dd, hh:mm")
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
1

One solution would be to parse the parameter in order to know which pattern to use:

private String changeDateFormat(String stringDate) {
    DateFormat dateFormat;
    if (stringDate.matches("^([a-zA-Z]+, ){2}[0-9]+st, [0-9]{2}:[0-9]+{2}")) {
        dateFormat = new SimpleDateFormat("E, MMM, dd'st', HH:mm");
    } else if (stringDate.matches("^([a-zA-Z]+, ){2}[0-9]+nd, [0-9]{2}:[0-9]+{2}")) {
        dateFormat = new SimpleDateFormat("E, MMM, dd'nd', HH:mm");
    } else if (stringDate.matches("^([a-zA-Z]+, ){2}[0-9]+rd, [0-9]{2}:[0-9]+{2}")) {
        dateFormat = new SimpleDateFormat("E, MMM, dd'rd', HH:mm");
    } else {
        dateFormat = new SimpleDateFormat("E, MMM, dd'th', HH:mm");
    }
    try {
        Date date = dateFormat.parse(stringDate);
        dateFormat = new SimpleDateFormat("ddMMMyyyy");
        stringDate = dateFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return stringDate.toUpperCase();
}

This might need some optimisation, but it gives the idea.

Loïc
  • 581
  • 3
  • 8