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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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