Parse string date time value input in Java

Java 8Object Oriented ProgrammingProgramming

In Java, you can parse string date time value input using

SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");

We have used the above class since we imported the following package −

import java.text.SimpleDateFormat;

Now, we can display the date in the same format −

Date dt = (Date) dateFormatter.parseObject("Tue, 20 Nov 2018 16:10:45 -0530");

The following is an example −

Example

 Live Demo

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
   public static void main(String[] argv) throws Exception {
      Format dateFormatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
      Date dt = (Date) dateFormatter.parseObject("Tue, 20 Nov 2018 16:10:45 -0530");
      System.out.println(dt);
   }
}

Output

Tue Nov 20 21:40:45 UTC 2018
raja
Published on 19-Dec-2018 05:21:56
Advertisements