Saturday, March 12, 2011

Converting Date to string

Consider we create date of today:

        // Get the date today using Calendar object.
        Date today = Calendar.getInstance().getTime();    
        String dateString = date2String(today);

package org.kodejava.example.java.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public static String date2String(Date today){
        // Create an instance of SimpleDateFormat used for formatting 
        // the string representation of date (month/day/year)
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        
        // Using DateFormat format method we can create a string 
        // representation of a date with the defined format.
        String reportDate = df.format(today);
        
        // Print what date is today!
        System.out.println("Report Date: " + reportDate);
    }

No comments:

Post a Comment

Chitika