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?
Convert String to Date
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?
J2ME Convert Date To String
Convert Date to String
How to convert String into java.util.Date
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 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??
How to convert date to time in PHP?
convert date into String like 9-10-2012 into nine october two thousand twelve
Java2
Convert string to Date in JSP
How to convert date to UTC format in Java?
how to convert string to char array in Java
How to convert Arraylist into String Array Java
How to convert String double quotos to String double double quotes(
Java String To Date
Java date to String, Convert date object into a string with a pattern
about java1
How to convert Input Stream to String in java
Java convert string to date from format yyyy-mm-dd hh mm ss
Convert Date To Calendar
How to convert a stack trace to a string?
How to convert a stack trace to a string?
Java Convert date to words
Convert charArray to String
convert current date into date format
How to convert current date to dd mm yyyy format in Java?
Convert Number to String
how to get date format in string from date object containing real date in java

Ads