211

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..

2
  • 4
    tl;dr: DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss").print(DateTime.now())
    – Andrew
    Oct 26, 2017 at 22:58
  • 2
    Note that "mm" is minutes and not months in Joda date formatting, you need "MM", caps matter. Oct 16, 2020 at 21:21

10 Answers 10

420

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));
7
  • 8
    sorry, but i'm looking for jodatime's DateTimeFormatter. Dec 2, 2013 at 15:00
  • 2
    @yaojiang Joda uses the same format string syntax as SimpleDateFormat. Read the API documentation.
    – Jesper
    Dec 2, 2013 at 15:17
  • @yaojiang As Jesper mentioned, Joda uses the same formatting strings, although it has more options. I've extended the example to include Joda, as you can see it is very similar.
    – DeltaLima
    Dec 2, 2013 at 15:42
  • strangely i am using DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy"); and i still get "mm/dd/yyyy hh:mm:ss"... Dec 2, 2013 at 15:51
  • 2
    While Joda-Time uses very similar formatting codes as java.util.Date/Calendar, read the doc carefully for subtle differences. They are not identical. Apr 24, 2014 at 23:41
61

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
3
  • 2
    nice, any idea what version this was introduced? save a lot of time by not creating stupid format strings... Nov 7, 2016 at 19:40
  • Yeah I totally agree. It looks like it has always been there. joda-time.sourceforge.net/apidocs/org/joda/time/format/… Nov 7, 2016 at 22:19
  • Yes, thank you! This listing should be in their own docs but you have to do it via trial and error in your own code to see what each format looks like. This is super helpful. Aug 28, 2020 at 2:13
24
DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")
2
  • 1
    This is useful, though doesn't exactly answer the question. One-line version: DateTime.now().toString("HH:mm:ss")
    – hBrent
    Jan 26, 2018 at 1:07
  • Very Very useful!
    – papigee
    Dec 23, 2022 at 16:11
14

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.

2
  • Your link to documentation is outdated. Correct URL is: joda.org/joda-time/apidocs/org/joda/time/format/… Apr 24, 2014 at 23:44
  • 1
    @BasilBourque Thank you for pointing out that D is for day of year, but my "link to documentation" is not out of date: the documentation I linked to is out of date. I was not linking to the documentation for its own sake, I was linking to the page I got some code from—which happens to be outdated documentation. Apr 24, 2014 at 23:52
11

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. :)

2
  • yeah, i will definitely be changing the format around. this works but only for this case. Dec 2, 2013 at 15:50
  • 1
    Not really expected behaviour, afterwards noone (including you) who looks your code understand nothing what did you do here... Nov 25, 2015 at 8:36
5

Please try to this one

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

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();
1

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.

1

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();
}
1
  • No, your code is both incorrect and unnecessarily complicated. I tried it. I had expected a period of 2 years 4 months 3 days or P2Y4M3D. I got P2Y3DT4M: 2 years 3 days 4 minutes! I also expected 855 days. I only got 734.
    – Anonymous
    Nov 8, 2021 at 19:46
-1

easiest way:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.