How to convert string to date in java in yyyy-mm-dd format?

How to convert string to date in java in yyyy-mm-dd format?

Hi,

I have a date in yyyy-mm-dd format. How to convert string to date in java in yyyy-mm-dd format?

Thanks

View Answers

July 21, 2017 at 3:27 AM

Hi,

You can use the SimpleDateFormat class to format string to date.

Here is the full example code:

package net.roseindia;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * Example of formatting date in Java 
 * from yyyy-mm-dd formaat
 */
public class StringDateFormat {

    public static void main(String[] args) {

        String dt = "2017-05-08";
        String pattern = "yyyy-mm-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("Date:" + date);

    }

}

Thanks


September 13, 2020 at 6:30 AM

Hi,

Small update: Use the MM in format, MM is for month. The small (mm) is for minute and you will get wrong results. Current format to be used is:

DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

Thanks


September 13, 2020 at 6:33 AM

Hi,

Check all the date formatting options at:

Thanks


September 13, 2020 at 7:26 PM

Hi,

Here is the output of the program in Eclipse IDE:

Thanks









Related Tutorials/Questions & Answers:
How to convert date to string in Java in yyyy-mm-dd format
How to convert string to date in java in yyyy-mm-dd format?
Advertisements
Java convert string to date from format yyyy-mm-dd hh mm ss
How to format current date into yyyy-mm-dd in Java
convert yyyy-mm-dd hh mm ss to date in sql
how to format date from yyyy-mm-dd to dd-mm-yyyy
get current date in java in yyyy mm dd hh mm ss format
c# current date in yyyy-MM-dd format
php date format yyyy-mm-dd
validationrule for date as format yyyy-mm-dd in drools
yyyy-mm-dd validation in javascript
How to convert date to string in Java?
Java Date conversion - Date Calendar
How to Convert String to Date?
date format - Java Beginners
java format date
String to Date format
Simple date formatter example
How to convert String Date to Timestamp in Java?
Mysql Date Type
convert string to date nsdate
How to convert into to String in Java?
date format - JSP-Servlet
Mysql Date Expression
Date prorblem
How to convert string to byte in Java
how to convert string to double in java
how to convert string to double in java
how to convert string to double in java
how to convert string to double in java
Convert String to Date
Get current Date and Time
how to convert string to image in java
How to convert String to int in Java
How to convert String to int in Java
How to convert String to int in Java
How to convert String to int in Java
Convert String to Date
How to convert String into java.util.Date
J2ME Convert Date To String
Convert String into date
How to convert date to UTC format in Java?
Convert Date to String
Formatting and Parsing a Date Using Default Formats
Java String To Date
how to convert string to char array in Java
How to convert Arraylist into String Array Java
ask java - Date Calendar
How to convert a String to an int?
Java get yesterday's date

Ads