Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Convert Date to String

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Date to String

Advertisement
In this section, you will learn to convert a date into string type. The java.util package provides the functionality to convert it.

Convert Date to String

     

In this section, you will learn to convert a date into string type. The java.util package provides the functionality to convert it.

Description of program:

This program helps you to convert the date into a string type. Firstly, you need to pass a date  in the parse() method which is  invoked through the DateFormat object . The format() method converts the date into a string. This method is defined as final in the DateFormat class. This is used to format a Date into a date/time string. The parameter passed is as date/time and it returns a string.

 Here is the code of program:

import java.util.*;
import java.text.*;
public class DateToString {
 public static void main(String[] args) {
 try {
 DateFormat formatter ; 
 Date date ;  
  formatter = new SimpleDateFormat("dd-MMM-yy");
  date = (Date)formatter.parse("11-June-07");  
  String s = formatter.format(date);
  System.out.println("Today is " + s);
  catch (ParseException e)
  {System.out.println("Exception :"+e);  }  
 
 }
}  

Output of this program:

C:\date>javac DateToString.java
C:\date>java DateToString
Today is 11-Jun-07

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 16, 2007

Related Tutorials

Discuss: Convert Date to String   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
D
April 13, 2011
and why not use toString???

so why not use the Date.toString method and write 1 and a half lines of code instead of 5???
Aditi Misra
November 25, 2011
Extracting Date From System Date

We can have the System dte by Calling the zero argument constructor of java.util.*. How to extract individual elements from that?
bourri
July 29, 2012
convert String to Date

I'm triying to convert String to Date with the same form in Mysql date but I dont know how??
Eugene van der Merwe
August 13, 2012
incomplete

For this to be a truly good example, you would include code to convert today's date. Your example converts some arbitrary date, not today as indicated in the output.
harish
November 16, 2012
how to include date into sql database using jdbc

how to include date into sql database using jdbc
V.MADHUSUDANA REDDY
April 21, 2014
numeric date convert into string format

this is nice. then u ll try totally numeric date converted into stirng date format.
DMCA.com