Java – Convert Date to Formatted String

Long time ago, i have a post about date validation in Java using SimpleDateFormat.
Java – Date validation using SimpleDateFormat

The SimpleDateFormat can format a Date object into string. See the following example.

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

public class DateToString {
	
  public static void main(String[] args) {
    // Set the format string
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    
    // Get the today date
    Date today = Calendar.getInstance().getTime();
    
    // Convert the date to string
    String strDate = sdf.format(today);

    // Print the date
    System.out.println("Report Date: " + strDate);
  }
}

 

Done =)

Reference: Kodejava – How do I convert Date to String?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.