Home Java Java-conversion Convert Date to String



Convert Date to String
Posted on: June 16, 2007 at 12:00 AM
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.

Related Tags for Convert Date to String:
cstringstlormdateformobjectconvertparsehelpmethodformattypeintthisinvokeforprogramtoramepefirstdateformatinrmpassasmntparpstrjthroughmehrintoobjprowhichseeatkisirarstrrtrtsssriringthstjepronogro


More Tutorials from this section

Ask Questions?    Discuss: Convert Date to String   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.