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
September 13, 2020 at 7:33 PM
HI,
If you want to learn Java date conversion in detail then check following tutorials:
Thanks
Ads
Related Tutorials/Questions & Answers:
Advertisements
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
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
 
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
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
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
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
How to convert String Date to Timestamp in Java?
of the
date to be parsed. In our case its:
String pattern = "
dd-MMM-
yyyy";
3... is the full code of the program to
convert Date String format to
Timestamp...
String Date to Timestamp example in
Java
Java is programming language
Simple date formatter example
formatter = new SimpleDateFormat("
yyyy-
MM-
dd HH:
mm:ss");
String formattedDate... into the "
yyyy-
MM-
dd
HH:
mm:ss"
format.
formatter = new SimpleDateFormat... we have formatted the
date into
"
yyyy-
MM-dd"
format.
Here
Mysql Date Type
' records in '
yyyy-
mm-
dd'
format.
select emp_dob from employee: The select query return the emp_dob
records in "
yyyy-
mm-dd"
format.
The
Date Type...;
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
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
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
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
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