How to convert date to string in Java in yyyy-mm-dd format

How to convert date to string in Java in yyyy-mm-dd format

Hi,

I need simple Java program in which we have a date object and it should be converted to String in yyyy-mm-dd format.

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

Thanks

View Answers

February 9, 2018 at 8:37 PM

Hi,

We can use the DateFormat class in following format to construct the date formatter:

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

Then use the format function to DateFormat to convert it to yyyy-mm-dd String format.

Here is Java example for converting String Date into yyyy-mm-dd format:

package net.roseindia;

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

/**
 * @author Deepak Kumar More tutorials at http://www.roseindia.net
 */
/*
 * In this example we convert date to yyyy-mm-dd format
 */
public class DateToStringConversion {

    public static void main(String[] args) {
        try {
            DateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
            Date date = new java.util.Date();//Current date
            String sDate = fmt.format(date);
            System.out.println("Today is " + sDate);
        } catch (Exception e) {
            System.out.println("Exception :" + e);
        }

    }

}

If you run the program you will get following output:

Today is 2018-03-10

Check more tutorials at Java Conversion Examples.

Thanks


September 13, 2020 at 6:29 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
to String in yyyy-mm-dd format. How to convert date to string in Java in yyyy...How to convert date to string in Java in yyyy-mm-dd format  Hi, I... it to yyyy-mm-dd String format. Here is Java example for converting String Date
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... java.util.Date; /* * Example of formatting date in Java * from yyyy-mm-dd formaat
Advertisements
Java convert string to date from format yyyy-mm-dd hh mm ss
Java convert string to date from format yyyy-mm-dd hh mm ss  Hi, I... date into the date format. How to convert string to date from format yyyy-mm-dd... to convert string date in format yyyy-mm-dd hh mm ss to date object. We need
How to format current date into yyyy-mm-dd in Java
How to format current date into yyyy-mm-dd in Java  Hi, I have requirement to format the date into yyyy-MM-dd format for inserting it into MySQL...-dd"); formats the date into yyyy-MM-dd format. the MM is used for getting
how to format date from yyyy-mm-dd to dd-mm-yyyy
how to format date from yyyy-mm-dd to dd-mm-yyyy  <!DOCTYPE html... to calculate the nos. of days between two dates, the date format is in "yyyy-mm-dd", so what i want is in "dd-mm-yyyy" format. So, how to change it? I would be much
c# current date in yyyy-MM-dd format
format. In c# how to get current date in yyyy-mm-dd format...c# current date in yyyy-MM-dd format  Hi, How to get the current...(dateString ); Above code will give date into 2021-01-21 format (yyyy-MM-dd). I
get current date in java in yyyy mm dd hh mm ss format
get current date in java in yyyy mm dd hh mm ss format  Hi, I want to get the current date into string yyyy mm dd hh mm ss format. How to get current date in java in yyyy mm dd hh mm ss format in a Java program? Thanks  
php date format yyyy-mm-dd
php date format yyyy-mm-dd  date format yyyy-mm-dd validation
validationrule for date as format yyyy-mm-dd in drools
format yyyy-mm-dd: How i write the validation rule for date as a format yyyy-mm-dd...validationrule for date as format yyyy-mm-dd in drools  Please give...("outofServiceStartDate", "stmpOutOfServiceStartDateInvalid", "Please use the format yyyy-mm-dd
convert yyyy-mm-dd hh mm ss to date in sql
convert yyyy-mm-dd hh mm ss to date in sql  Hi, How to convert yyyy-mm-dd hh mm ss to date in sql? Thanks   Hi, You should use following code in SQL DATE_FORMAT(STR_TO_DATE(my_date, "%Y/%m/%d %H:%i:%s.%f"), "%Y-%m
yyyy-mm-dd validation in javascript
yyyy-mm-dd validation in javascript   My form return value like (2011-10-05)yyyy-mm-dd ... my End date greater than my start date.. How to do validation in javascript
String to Date format
SimpleDateFormat("yyyy-MM-dd"); String formattedDate=sdf.format(date...("yyyy-MM-dd"); String formattedDate=sdf.format(date...String to Date format  My Date format is String Eg(Jan 20 2011
java format date
java format date  Hi, How can I format the date in the following pattern? yyyy-MM-dd   import java.util.*; import java.text.*; class...-MM-dd"); String formattedDate=sdf.format(date
date format - Java Beginners
date format  How to convert yyyy-mm-dd to dd-mmm-yyyy  Hi friend, Code to convert yyyy-mm-dd to dd-mmm-yyyy : import java.util.Date...(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd
Java Date conversion - Date Calendar
Java Date conversion  How do I convert 'Mon Mar 28 00:00:00 IST 1983' which is a java.util.Date or String to yyyy/MM/dd date format in sql
How to convert date to string in Java?
How to convert date to string in Java?  Hello developers, I have java.util.Date object and it has to be converted to date format. How to convert date to string in Java? Thanks   Hi, To convert java.util.Date object
How to convert date to UTC format in Java?
How to convert date to UTC format in Java?  Hi, What is UTC Date format and How to convert date to UTC format in Java? I need simple to learn... of world. You can use Java program to convert the date in UTC String
How to Convert String to Date?
How to Convert String to Date?  Hi, I am new in Java and learning... have to convert it to Date object. Tell me How to Convert String to Date? Thanks   Hi Dude, Its very easy to convert the String into date format
Mysql Date Expression
a format  'YYYY-MM-DD' and 'HH.MM.SS'. Timestamp columns are displayed... Mysql Date Expression       The Tutorial illustrate an example from 'Mysql Date Expression
date format - JSP-Servlet
"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date dateStr...); System.out.println("yyyy-MM-dd date is ==>"+formattedDate); Date...date format  how to convert 2008-10-14 to 14-Sep-2008   Hi
How to convert current date to dd mm yyyy format in Java?
How to convert current date to dd mm yyyy format in Java?  Hi, How to convert current date to dd mm yyyy format in Java? In my program I have to display the current date in the dd-mm-yyyy format to user on the website. How
How to convert current date to mm dd yyyy format in Java?
How to convert current date to mm dd yyyy format in Java?  Hi, How to convert current date to mm dd yyyy format in Java? Share me the program... for converting current date into MM-dd-yyyy format. Here is full code: package
Simple date formatter example
the date object into the "yyyy-MM-dd HH:mm:ss" format. formatter = new... class to convert the date into the specified format. There are various constructors... SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss
Mysql Date Type
the 'emp_dob ' records in 'yyyy-mm-dd' format. select emp_dob from employee: The select query return the emp_dob  records in "yyyy-mm-dd" format...; Mysql Date Type is used to show the date in "yyyy-mm-dd" format.The
select date in desired format
select date in desired format  how to select date from the database in DD/MM/YYYY format, because if i am trying select data from the database i am getting value in yyyy/mm/dd and time format like below "2012-05-07 00:00:00.0
convert current date into date format
convert current date into date format  How to get current date and convert into date format in Java or PHP
how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object...-yyyy HH:mm:ss"); String datetext4=sdf4.format(date
how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object...-yyyy HH:mm:ss"); String datetext4=sdf4.format(date
How to convert String Date to Timestamp in Java?
String Date to Timestamp example in Java Java is programming language.... In this example program we are going to convert String representation of Date object... the pattern of the date to be parsed. In our case its: String pattern = "dd-MMM
String to date with no format
date format can be (mm-dd-yyyy) if the 99th record also was 06-06-2006 the result should be date format can be (mm-dd-yyyy;dd-mm-yyyy) can sum1 please help me...String to date with no format  Hii.. I want to convert a string
Convert String into date
convert a string into Date format. It also provide some of the methods like.... SimpleDateFormat take string as input an convert that string into Date format. Here... in java. Example:  Code to convert String to Date in java. import
convert string to date nsdate
convert string to date nsdate  Hi, How to convert c string to date NSDate?   Convert NSDate to NSString
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
How to format json String in Java
How to format json String in Java  Hi, How to format json String in Java? I have an object of HashMap how to convert it to formatted output... library. Then use following method to covert it to JSON String: String json
How to format json String in Java
How to format json String in Java  Hi, How to format json String in Java? I have an object of HashMap how to convert it to formatted output... library. Then use following method to covert it to JSON String: String json
Convert Date to GMT
format is "yyyy-MM-dd HH:mm:ss" so, it takes you in this format... Convert Date to GMT   ... a date into the GMT format. The GMT stands for Greenwich Mean Time
Convert String to Date
Convert String to Date       In this example we are going to convert string into date We..., java.text.SimpleDateFormat and java.util.Date package to convert string into date format.We
How to convert String into java.util.Date
Getting time in Milliseconds Convert Date To Calendar How to convert String Date...How to convert String into java.util.Date  Hi, How I can convert String date into java.util.Date in a Java program? Thanks   Hi
Validating date in javascript - Java Interview Questions
; In what format you are going to enter the date in Text box (i.e : dd/mm/yyyy or mm/dd/yyyy or yyyy/mm/dd...Validating date in javascript   Hi Deepak, Can u give
Date prorblem
into string filed in java code. once the date is available in filed ( of type String ) in java code, then i have to convert it into yyyy-mm-dd hh-mm-ss. then i have...) SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); DATEADDED1
Convert String to Date
Convert String to Date       In this example we are going to convert String into date... In this example we are going to convert string into date. We are creating
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
ask java - Date Calendar
(); SimpleDateFormat sdf; //sdf = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss aa"); sdf = new SimpleDateFormat("yyyy/MM/dd"); System.out.println("date " + sdf.format(date)); //string containing date in one format String strDate1
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
how to convert string to double in java
how to convert string to double in java  Hi, I have String variable with double value in it. how to convert string to double in java? Thanks  ... to convert a String into double. Here is full example code: package net.roseindia
Get current Date and Time
1,1970. String ToString() : Convert the date into String. equals(Object date... DateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"..._TO_REPLACE_1 DateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
How to convert EBCDIC format value into ASCII format value in java
How to convert EBCDIC format value into ASCII format value in java  how to convert EBCDIC data format into ASCII format data using java Use Case:I my file contains ASCII format values as well as EBCDIC format values.I need

Ads