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

Ads
 

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

Ads









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
get current date in java in yyyy mm dd hh mm ss format
how to format date from yyyy-mm-dd to dd-mm-yyyy
c# current date in yyyy-MM-dd format
validationrule for date as format yyyy-mm-dd in drools
yyyy-mm-dd validation in javascript
How to convert date to string in Java?
How to Convert String to Date?
java format date
String to Date format
How to convert String Date to Timestamp in Java?
Simple date formatter example
How to convert into to String in Java?
Mysql Date Type
convert string to date nsdate
Date prorblem
How to convert string to byte in Java
Get current Date and Time
Mysql Date Expression
how to convert string to image 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
How to convert String to int in Java
How to convert String to int in Java
How to convert String to int in Java

Ads