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

Ads
 

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

Ads









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 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  
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
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
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
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
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
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 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 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
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 to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
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
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
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
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
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
convert string to date nsdate
convert string to date nsdate  Hi, How to convert c string to date NSDate?   Convert NSDate to NSString

Ads