How to Format a Date String using SimpleDateFormat in Java?

JavaServer Side ProgrammingProgramming

One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat classTo parse/convert a string as a Date object −

  • Instantiate this class by passing desired format string.
  • Parse the date string using the parse() method.

Example

Live Demo

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Sample {
   public static void main(String args[]) throws ParseException {  
      String date_string = "2007-25-06";
      //Instantiating the SimpleDateFormat class
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM");      
      //Parsing the given String to Date object
      Date date = formatter.parse(date_string);      
      System.out.println("Date value: "+date);
   }
}

Output

Date value: Mon Jun 25 00:00:00 IST 2007
raja
Published on 06-Feb-2021 03:45:10
Advertisements