How to format a string to date in as dd-MM-yyyy using java?

Java 8Object Oriented ProgrammingProgramming

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).

Using the methods of this class you can parse String to Date or, format Date to String.

Parsing String to Date

You can parse a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To parse a String to Date object −

  • Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.

//Instantiating the SimpleDateFormat class
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
  • Parse/convert the required String to Date object using the parse() method by passing it as a parameter.

Date date = formatter.parse(dob);
System.out.println("Date object value: "+date);

Example

Following Java program accepts name date of birth from the user in String format, converts/parses the obtained date of birth String to Date object and, calculates the current age and displays the result.

 Live Demo

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Scanner;
public class CalculatingAge {
   public static Date StringToDate(String dob) throws ParseException{
      //Instantiating the SimpleDateFormat class
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      //Parsing the given String to Date object
      Date date = formatter.parse(dob);
      System.out.println("Date object value: "+date);
      return date;
   }
   public static void main(String args[]) throws ParseException {
      //Reading name and date of birth from the user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your date of birth (dd-MM-yyyy): ");
      String dob = sc.next();
      //Converting String to Date
      Date date = CalculatingAge.StringToDate(dob);
      //Converting obtained Date object to LocalDate object
      Instant instant = date.toInstant();
      ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());
      LocalDate givenDate = zone.toLocalDate();
      //Calculating the difference between given date to current date.
      Period period = Period.between(givenDate, LocalDate.now());
      System.out.print("Hello "+name+" your current age is: ");
      System.out.print(period.getYears()+" years "+period.getMonths()+" and "+period.getDays()+" days");
   }
}

Output

Enter your name:
Krishna
Enter your date of birth (dd-MM-yyyy):
26-09-1989
Date object value: Tue Sep 26 00:00:00 IST 1989
Hello Krishna your current age is: 29 years 8 and 5 days
raja
Published on 04-Jul-2019 17:53:57
Advertisements