Home > Java > javaTutorial > body text

How to parse a string in the format dd/MM/yyyy into a date in dd/MM/yyyy in Java?

WBOY
Release: 2023-08-29 19:13:06
forward
1523 people have browsed it

How to parse a string in the format dd/MM/yyyy into a date in dd/MM/yyyy in Java?

The java.text package provides a class called SimpleDateFormat for formatting and parsing dates in the desired way (natively).

One of the constructors of this class accepts a string value representing the desired date format and a constructor SimpleDateFormat object.

The format() method of this class accepts a java.util.Date object and returns the date/time character in the format represented by the current object string.

So, parse the date string into another date format -

  • Get the input date string.

  • Convert it to a java.util.Date object.

    >
  • Instantiate the SimpleDateFormat class by passing the desired (new) format as a string to its constructor.

  • Call the format() method by passing the Date object obtained above as a parameter.

Example

Live demonstration

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class FormattingDate {
   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 = FormattingDate.StringToDate(dob);
      System.out.println("Select format: ");
      System.out.println("a: MM-dd-yyyy || b: dd-MM-yyyy || c: yyyy-MM-dd ");
      char ch = sc.next().toCharArray()[0];;
      switch (ch) {
         case 'a':
            System.out.println("Date in the format: MM-dd-yyyy");
            System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(date));
            break;
         case 'b':
            System.out.println("Date in in the format: dd-MM-yyyy");
            System.out.println(new SimpleDateFormat("dd-MM-yyyy").format(date));
            break;
         case 'c':
            System.out.println("Date in the format: yyyy-MM-dd");
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            break;
         default:
            System.out.println("Model not found");
            break;
      }
   }
}
Copy after login

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
Select format:
a: MM-dd-yyyy || b: dd-MM-yyyy || c: yyyy-MM-dd
a
Date in the format: MM-dd-yyyy
09-26-1989
Copy after login

The above is the detailed content of How to parse a string in the format dd/MM/yyyy into a date in dd/MM/yyyy in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!