Java Program to parse Time using custom format

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 12:47:51
Advertisements