How to convert date to string in Java?

Ads
 

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

Ads









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
How to convert String into java.util.Date
How to convert String Date to Timestamp in Java?
How to convert into to String in Java?
J2ME Convert Date To String
How to convert a String to an int?
Convert String into date
How to convert string to byte in Java
how to convert string to image 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 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?
How to convert date to UTC format in Java?
convert date into String like 9-10-2012 into nine october two thousand twelve
How to convert Arraylist into String Array Java
Java String To Date
How to convert Input Stream to String in java
How to convert current date to dd mm yyyy format in Java?
convert current date into date format
How to convert current date to mm dd yyyy format in Java?

Ads