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

JavaJodatimeDate Format

Java Problem Overview


I have a string "11/15/2013 08:00:00", I want to format it to "11/15/2013", what is the correct DateTimeFormatter pattern?

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

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

Java Solutions


Solution 1 - Java

Note that in JAVA SE 8 a new java.time (JSR-310) package was introduced. This replaces Joda time, Joda users are advised to migrate. For the JAVA SE ≥ 8 way of formatting date and time, see below.

Joda time

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
System.out.println(dtfOut.print(jodatime));

Standard Java ≥ 8

Java 8 introduced a new Date and Time library, making it easier to deal with dates and times. If you want to use standard Java version 8 or beyond, you would use a DateTimeFormatter. Since you don't have a time zone in your String, a java.time.LocalDateTime or a LocalDate, otherwise the time zoned varieties ZonedDateTime and ZonedDate could be used.

// Format for input
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
LocalDate date = LocalDate.parse(dateTime, inputFormat);
// Format for output
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
// Printing the date
System.out.println(date.format(outputFormat));

Standard Java < 8

Before Java 8, you would use the a SimpleDateFormat and java.util.Date

String dateTime = "11/15/2013 08:00:00";
// Format for input
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Parsing the date
Date date7 = dateParser.parse(dateTime);
// Format for output
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
// Printing the date
System.out.println(dateFormatter.format(date7));

Solution 2 - Java

I am adding this here even though the other answers are completely acceptable. JodaTime has parsers pre built in DateTimeFormat:

dateTime.toString(DateTimeFormat.longDate());

This is most of the options printed out with their format:

shortDate:         11/3/16
shortDateTime:     11/3/16 4:25 AM
mediumDate:        Nov 3, 2016
mediumDateTime:    Nov 3, 2016 4:25:35 AM
longDate:          November 3, 2016
longDateTime:      November 3, 2016 4:25:35 AM MDT
fullDate:          Thursday, November 3, 2016
fullDateTime:      Thursday, November 3, 2016 4:25:35 AM Mountain Daylight Time

Solution 3 - Java

DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")

Solution 4 - Java

I think this will work, if you are using JodaTime:

String strDateTime = "11/15/2013 08:00:00";
DateTime dateTime = DateTime.parse(strDateTime);
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/YYYY");
String strDateOnly = fmt.print(dateTime);

I got part of this from here.

Solution 5 - Java

I have a very dumb but working option. if you have the String fullDate = "11/15/2013 08:00:00";

   String finalDate = fullDate.split(" ")[0];

That should work easy and fast. :)

Solution 6 - Java

Please try to this one

public void Method(Datetime time)
{
    time.toString("yyyy-MM-dd'T'HH:mm:ss"));
}

Solution 7 - Java

UPDATED:

You can: create a constant:

private static final DateTimeFormatter DATE_FORMATTER_YYYY_MM_DD =
          DateTimeFormat.forPattern("yyyy-MM-dd"); // or whatever pattern that you need.

This DateTimeFormat is importing from: (be careful with that)

> import org.joda.time.format.DateTimeFormat; > import org.joda.time.format.DateTimeFormatter;

Parse the Date with:

DateTime.parse(dateTimeScheduled.toString(), DATE_FORMATTER_YYYY_MM_DD);

Before:
DateTime.parse("201711201515",DateTimeFormat.forPattern("yyyyMMddHHmm")).toString("yyyyMMdd");

if want datetime:

DateTime.parse("201711201515", DateTimeFormat.forPattern("yyyyMMddHHmm")).withTimeAtStartOfDay();

Solution 8 - Java

This works

String x = "22/06/2012";
String y = "25/10/2014";

String datestart = x;
String datestop = y;
	
//DateTimeFormatter format = DateTimeFormat.forPattern("dd/mm/yyyy");
SimpleDateFormat  format = new SimpleDateFormat("dd/mm/yyyy");
	
Date d1 = null;
Date d2 = null;

try {
	d1 =  format.parse(datestart);
	d2 = format.parse(datestop);
		
	DateTime dt1 = new DateTime(d1);
	DateTime dt2 = new DateTime(d2);
		
	//Period
	period = new Period (dt1,dt2);
		
	//calculate days
	int days = Days.daysBetween(dt1, dt2).getDays();
		
		
} catch (ParseException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

Solution 9 - Java

Another way of doing that is:

String date = dateAndTime.substring(0, dateAndTime.indexOf(" "));

I'm not exactly certain, but I think this might be faster/use less memory than using the .split() method.

Solution 10 - Java

easiest way:

DateTime date = new DateTime();
System.out.println(date.toString(DateTimeFormat.forPattern("yyyy-mm-dd")));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniCodeLikeImDrunkView Question on Stackoverflow
Solution 1 - JavaDeltaLimaView Answer on Stackoverflow
Solution 2 - JavaChad BinghamView Answer on Stackoverflow
Solution 3 - Javasebastian krügerView Answer on Stackoverflow
Solution 4 - JavaThe Guy with The HatView Answer on Stackoverflow
Solution 5 - JavaClad CladView Answer on Stackoverflow
Solution 6 - JavaSinghView Answer on Stackoverflow
Solution 7 - JavaJacky.chengView Answer on Stackoverflow
Solution 8 - JavaEddieView Answer on Stackoverflow
Solution 9 - JavaThe Guy with The HatView Answer on Stackoverflow
Solution 10 - JavaabdelatifView Answer on Stackoverflow