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

View Answers

February 9, 2018 at 8:04 PM

Hi,

To convert java.util.Date object into string SimpleDateFormat class is used, and its format() method takes data object.

The format() method performs conversion and returns string date object.

Here is the example program that I developed for you.

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 current date to String
 */
public class DateToStringConversion {

    public static void main(String[] args) {
        try {
            DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
            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);
        }

    }

}

Check Java Conversion examples.

Thanks









Related Tutorials/Questions & Answers:
How to Convert String to Date?
How to convert date to string in Java?
Advertisements
convert string to date nsdate
How to convert string to date in java in yyyy-mm-dd format?
How to convert date to string in Java in yyyy-mm-dd format
Convert String to Date
How to convert String Date to Timestamp in Java?
Convert String to Date
How to convert String into java.util.Date
J2ME Convert Date To String
Convert Date to String
Convert String into date
How to convert into to String in Java?
How to convert a String to an int?
how to convert yyyyMMdd to date in python
how to convert yyyyMMdd to date in python
difference between java5 and java6 - Java Beginners
How to convert string to byte in Java
How to convert date to time in PHP?
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 image 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
How to convert String to int in Java
How ro convert char into string??
convert date into String like 9-10-2012 into nine october two thousand twelve
Java2
How to convert date to UTC format in Java?
Convert string to Date in JSP
how to convert string to char array in Java
How to convert Arraylist into String Array Java
Java String To Date
How to convert String double quotos to String double double quotes(
How to convert Input Stream to String in java
Java convert string to date from format yyyy-mm-dd hh mm ss
Java date to String, Convert date object into a string with a pattern
about java1
Convert Date To Calendar
How to convert current date to dd mm yyyy format in Java?
convert current date into date format
How to convert a stack trace to a string?
How to convert a stack trace to a string?
How to convert current date to mm dd yyyy format in Java?
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
Java Convert date to words

Ads