Home > Blockchain >  Caused by: java.time.format.DateTimeParseException: Text '11 2 AM' could not be parsed at
Caused by: java.time.format.DateTimeParseException: Text '11 2 AM' could not be parsed at

Time:07-13

I want parse the time in hh:mm:a format. but when I passed the string in hh:m:a format it gives error java.time.format.DateTimeParseException: at index 3. for example:- when I passed the string like 11:15 AM everything is fine. but when I passed the String in 11:2 AM. It says Text '11 2 AM' could not be parsed at index 3.

Here is my java code:-

String time = result.get(0);
time = time.replace("a.m", "AM");
time = time.replace("p.m", "PM");
time = time.substring(0, time.length() - 1);

mTimebtn.setText(time);

if (time.contains("AM")) {
    DateTimeFormatter dtfParse1 = DateTimeFormatter.ofPattern("hh m a", Locale.ENGLISH);
    // a second one to be used in order to format the desired result
    DateTimeFormatter dtfFormat1 =  DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
    LocalDate localDate1 = LocalDate.parse(time, 
    mTimebtn.setText(localDate1.format(dtfFormat1));

    finaltime = localDate1.format(dtfFormat1);
}

CodePudding user response:

If you want to parse some time of day, you will be needing a java.time.LocalTime, not a LocalDate.

In contrast to the old and outdated java.util.Date, a LocalDate represents a date consisting of day of month, month of year and year.

You are trying to parse a time of day, the pattern might even be working for it but you cannot create a LocalDate from information about hour of day, minute of hour and AM/PM. That won't be working…

The following code might be working for you:

public static void main(String[] args) {
    // your example input String
    String someTime = "11 2 AM";
    // one dtf for parsing input
    DateTimeFormatter timeParser = DateTimeFormatter
                                    .ofPattern("K m a", Locale.ENGLISH);
    // another one for formatting as desired
    DateTimeFormatter timeFormatter = DateTimeFormatter
                                        .ofPattern("KK:mm a", Locale.ENGLISH);
    // parse using the parser
    LocalTime localTime = LocalTime.parse(someTime, timeParser);
    // print using the formatter
    System.out.println(localTime.format(timeFormatter));
}

Output:

11:02 AM

CodePudding user response:

try {
     String time = "11 2 AM";
     String parsePatter = time.contains(":") ? "hh:mm a" : "hh m a";
     SimpleDateFormat parser = new SimpleDateFormat(parsePatter);
     Date date = parser.parse(time);

     // Result
     SimpleDateFormat resultParse = new SimpleDateFormat("hh:mm a");
     String result = resultParse.format(date);
     System.out.println(result); // 11:02 AM
    } catch (Exception ex) {
       ex.printStackTrace();
    }

CodePudding user response:

To Convert in hh:mm:a format any of the form will work for 11:22 AM or 11:2 AM

 val time = "11:22 AM"
        //  or
        val time2 = "11:2 AM"
        val parser = SimpleDateFormat("HH:mm a")
        val date = parser.parse(time2) as Date
        var format=parser.format(date)

CodePudding user response:

You passed 11:2 AM but the format is hh m A. The format should be hh:m A but if you wanted to use format(hh m A) you should pass 11 2 AM. And LocalDate you should pass the date too. Check my answer below:

String time = "2022-07-13 11:2 AM";
time = time.replace("a.m", "AM");
time = time.replace("p.m", "PM");
System.out.println("Init:"   time);
if (time.contains("AM")) {
   DateTimeFormatter dtfParse1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:m a",
                    Locale.ENGLISH);
            // a second one to be used in order to format the desired result
            DateTimeFormatter dtfFormat1 = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
            LocalDateTime localDate1 = LocalDateTime.parse(time, dtfParse1);
            System.out.println(localDate1);
            //mDatebtn.setText(day   "-"   month   "-"   year);
            System.out.println(localDate1.format(dtfFormat1));
            //finaltime = localDate1.format(dtfFormat1);
        }
  • Related