Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Java by (4k points)

II want to convert a java.util.Date object to a String in Java.

The format is 2010-05-30 22:15:52

1 Answer

0 votes
by (46k points)

To convert a Date to a String try DateFormat#format method:

String pattern = "MM/dd/yyyy HH:mm:ss";

// Make an instance of SimpleDateFormat used for formatting 

// the string representation of date acc to the chosen pattern

DateFormat df = new SimpleDateFormat(pattern);

// Get the today date by Calendar object.

Date today = Calendar.getInstance().getTime();        

// Using DateFormat format method we can create a string 

// representation of a date with the defined format.

String todayAsString = df.format(today);

// Print it!

System.out.println("Today is: " + todayAsString);

click here for source. 

Related questions

0 votes
1 answer
asked Jul 10, 2019 in DevOps and Agile by chandra (29.3k points)
0 votes
1 answer
asked Jul 4, 2019 in SQL by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...