Java Program to parse date and time


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

Updated on: 27-Jun-2020

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements