1

I have tried the code below, but it's not working.

Please explain why it's not working.

public static void main(String[] args) {
    String ti = "11:30 PM, Sun 07 Oct 2018";
    String sformat = "h:m a, E dd M yyyy";
    String cformat = "hh:mm a";
    String d = dateFormater(ti, cformat, sformat);
    System.out.println(d);
}

public static String dateFormater(String dateFromJSON,
                                  String expectedFormat, String oldFormat) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
    Date date = null;
    String convertedDate = null;
    try {
        date = dateFormat.parse(dateFromJSON);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(expectedFormat);
        convertedDate = simpleDateFormat.format(date);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return convertedDate;
}
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
RIK
  • 123
  • 1
  • 2
  • 13
  • Please define "it's not working". Is it crashing? It does not produce the desired output? If yes, what is the expected output and the current output? – StephaneM Oct 08 '18 at 14:30
  • its output is giving an exception (java.text.ParseException: Unparseable date: "11:30 PM, Sun 07 Oct 2018"); – RIK Oct 08 '18 at 14:32
  • https://stackoverflow.com/questions/16871367/java-text-parseexception-unparseable-date – SME_Dev Oct 08 '18 at 14:33

2 Answers2

1

Your date format is incorrect. Please use the format below.

String sformat= "hh:mm a, EEE dd MMM yyyy";
Pang
  • 9,564
  • 146
  • 81
  • 122
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
  • @Rik Please accept the answer (By clicking on the tick mark near my answer) if this solves your problem. – Viswanath Lekshmanan Oct 08 '18 at 14:46
  • It's not clear that "hh" is correct here - we can't tell based on the input. It's relatively rare to have a zero-padded hour of day *and* an AM/PM indicator. – Jon Skeet Oct 08 '18 at 15:03
  • thank you,problem was solved but there wasn't any pinpointing explanation. – RIK Oct 08 '18 at 17:44
1

Your pattern needs

  • MMM because the month is in short litteral format (M only is for month number and MMMM is for long litteral format)

    String sformat = "h:m a, E dd MMM yyyy";
    

And you should consider using new time API : java.time as it's easier to use and don't need extra mandatory try/catch :

public static String dateFormater(String dateFromJSON, String expectedFormat, String oldFormat) {
    LocalDateTime date = LocalDateTime.parse(dateFromJSON, DateTimeFormatter.ofPattern(oldFormat, Locale.ENGLISH));
    String newStr = date.format(DateTimeFormatter.ofPattern(expectedFormat, Locale.ENGLISH));
    return newStr;
}

or in a single-line :

public static String dateFormaterSh(String dateFromJSON, String expectedFormat, String oldFormat) {
    return LocalDateTime.parse(dateFromJSON, DateTimeFormatter.ofPattern(oldFormat, Locale.ENGLISH))
                        .format(DateTimeFormatter.ofPattern(expectedFormat, Locale.ENGLISH));
}
azro
  • 53,056
  • 7
  • 34
  • 70