Home > Enterprise >  How to convert 2022-08-16T06:25:00.000 to HH:mm
How to convert 2022-08-16T06:25:00.000 to HH:mm

Time:08-02

I'm trying to convert date from API "2022-08-16T06:25:00.000" to HH:mm (6:25) but getting DateTimeParseException.
My code: ZonedDateTime.parse(it.time[0], DateTimeFormatter.ofPattern("HH:mm"))

"time": [
    "2022-08-16T06:25:00.000",
    "2022-08-16T07:40:00.000"
],

CodePudding user response:

String dateTimeStr = "2022-08-16T06:25:00.000";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm");
String time = dateTime.format(fmt);
System.out.println(time);

or, if you want to use the time as an instance of LocalTime, you can get it by dateTime.toLocalTime()

CodePudding user response:

How about different approach

String dateTimeStr = "2022-08-16T06:25:00.000";
Matcher m=Pattern.of("T(\\d{2}:\\d{2}):").matcher(dateTimeStr);
m.find();
System.out.println(m.group(1);; //should print 06:25

CodePudding user response:

You don't need to define any DateTimeFormatter in this situation.

  1. use a LocalDateTime because the input String does not hold any information about the zone
  2. don't use a DateTimeFormatter for parsing that only parses hour of day and minutes of hour, the String to be parsed just contains more information

Here's an example without any DateTimeFormatter explicitly defined (but it will use default ones for parsing, at least):

public static void main(String[] args) {
    // example input
    String fromApi = "2022-08-16T06:25:00.000";
    // parse it to a LocalDateTime because there's no zone in the String
    LocalDateTime localDateTime = LocalDateTime.parse(fromApi);
    // extract the time-of-day part
    LocalTime localTime = localDateTime.toLocalTime();
    // and print its toString() implicitly
    System.out.println(localTime);
}

Output: 06:25

The above code will produce output of the pattern HH:mm, which will have leading zeros at hours of day to always have a two-digit representation.

If you insist on single-digit hours of day, you will have to prepare a DateTimeFormatter, like in this alternative example:

public static void main(String[] args) {
    // example input
    String fromApi = "2022-08-16T06:25:00.000";
    // parse it to a LocalDateTime because there's no zone in the String
    LocalDateTime localDateTime = LocalDateTime.parse(fromApi);
    // extract the time-of-day part
    LocalTime localTime = localDateTime.toLocalTime();
    // prepare a DateTimeFormatter that formats single-digit hours of day
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("H:mm");
    // print the LocalTime formatted by that DateTimeFormatter
    System.out.println(localTime.format(dtf));
}

Output this time: 6:25

CodePudding user response:

The other answers use Java. Since you've added a [kotlin] tag, here is a Kotlin-based answer for the sake of completeness. In order to make it different to the Java answers, I'm using kotlinx.datetime, which is still at the experimental stage at version 0.4.0.

import kotlinx.datetime.LocalDateTime

fun main() {
    println(LocalDateTime.parse("2022-08-16T06:25:00.000").time)  // prints "06:25"

    // If you want "6:25" you can format it yourself:
    println(with(LocalDateTime.parse("2022-08-16T06:25:00.000")) {
        "$hour:$minute"
    })
}
  • Related