
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to format a string to date in as dd-MM-yyyy using java?
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.
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
- Related Questions & Answers
- How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java?
- How to format JavaScript date into yyyy-mm-dd format?
- Java Program to format date in mm-dd-yyyy hh:mm:ss format
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- Accepting date strings (MM-dd-yyyy format) using Java regex?
- Convert MySQL date format from yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss ?
- Program to reformat date in YYYY-MM-DD format using Python
- MySQL date format DD/MM/YYYY select query?
- MySQL date format to convert dd.mm.yy to YYYY-MM-DD?
- How to convert Python date string mm/dd/yyyy to datetime?
- Get date format DD/MM/YYYY with MySQL Select Query?
- How to insert mm/dd/yyyy format dates in MySQL?
- Get today's date in (YYYY-MM-DD) format in MySQL?
- How to validate given date formats like MM-DD-YYYY using regex in java?
- Change date format (in DB or output) to dd/mm/yyyy in PHP MySQL?