Converting from String to specific HH:mm date format

Sourav Mehra :

I have a String object which stores the current system time in HH:mm format. I have to convert this to a DATE object and maintain the same HH:mm format. How can this be done.

I tried using Date parse options but everytime I get the response in complete dd-MM-yyyy HH:mm:ss format and not in the required HH:mm format needed.

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String getCurrentTime = sdf.format(Calendar.getInstance().getTime());
Date date = sdf1.parse(getCurrentTime);

Expected output should be a Date result in HH:mm format.

KayV :

Here is the simple example to solve this using java8 Date and Time API:

    DateTimeFormatter parser1 = DateTimeFormatter.ofPattern("HH:mm:ss");
    LocalDateTime ldd1 = LocalDateTime.now();
    System.out.println("DateTimeFormatter ldd1 = " + parser1.format(ldd1));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=89015&siteId=1