Convert Date to String

Ads
 

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.

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.

Ads

Ads