Parse Time Using Custom Format in Java



To get the time format, use the DateFormat class and create a new object.

DateFormat dateFormatter = new SimpleDateFormat("hh.mm.ss a");

Now, parse the time.

dateFormatter.parse("12.55.20 PM");

Example

 Live Demo

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
   public static void main(String[] argv) throws Exception {
      DateFormat dateFormatter = new SimpleDateFormat("hh.mm.ss a");
      System.out.println("Parse Time...");
      Date dt = (Date) dateFormatter.parse("12.55.20 PM");
      System.out.println(dt);
   }
}

Output

Parse Time...
Thu Jan 01 12:55:20 UTC 1970
Updated on: 2020-06-27T09:30:10+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements