Java Program to parse date and time

Java 8Object Oriented ProgrammingProgramming

Create a SimpleDateFormat object −

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");

Do not forget to import the following package for SimpleDateFormat class −

import java.text.SimpleDateFormat;

Now, since we have set the format for date above, let us parse the date using parseObject() method −

Date dt = (Date) dateFormat.parseObject("2018.11.22.11.50.15");

The following is an example −

Example

 Live Demo

import java.util.Date;
import java.text.SimpleDateFormat;
public class Demo {
   public static void main(String[] argv) throws Exception {
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
      // parse
      System.out.println("Parse Date and Time...");
       Date dt = (Date) dateFormat.parseObject("2018.11.22.11.50.15");
      System.out.println(dt);
   }
}

Output

Parse Date and Time...
Thu Nov 22 11:50:15 UTC 2018
raja
Published on 19-Dec-2018 05:10:37
Advertisements