Yao Chen Yao Chen - 2 years ago 150
Java Question

How to format Joda-Time DateTime to only mm/dd/yyyy?

I have a string "

11/15/2013 08:00:00
", I want to format it to "
", what is the correct

I've tried many and googled and still unable to find the correct pattern.

edit: I am looking for Joda-Time
, not Java's SimpleDateFormat..

Answer Source

Create a DateTimeFormatter using DateTimeFormat.forPattern(String)

Using Joda time you would do it like this:

String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date

Also, if you wanted to use standard Java, you would do it like this:

// Format for input
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Parsing the date
Date date = dateParser.parse(dateTime);
// Format for output
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
// Printing the date
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download