Java Program to parse Time using custom format


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: 27-Jun-2020

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements