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

Ads









Related Tutorials/Questions & Answers:
How to convert string to date in java in yyyy-mm-dd format?
Java convert string to date from format yyyy-mm-dd hh mm ss
Advertisements
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
php date format yyyy-mm-dd
validationrule for date as format yyyy-mm-dd in drools
yyyy-mm-dd validation in javascript
Java Date conversion - Date Calendar
How to Convert String to Date?
date format - Java Beginners
How to convert into to String in Java?
java format date
Mysql Date Type
How to convert String Date to Timestamp in Java?
String to Date format
How to convert string to byte in Java
Simple date formatter example
convert string to date nsdate
how to convert string to image in java
Mysql Date Expression
how to convert string to double in java
date format - JSP-Servlet
How to convert date to UTC format in Java?
How to convert String into java.util.Date
Date prorblem
How to convert Arraylist into String Array Java
Get current Date and Time
Java String To Date
Convert String to Date
Formatting and Parsing a Date Using Default Formats

Ads