210

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169

10 Answers10

419

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));
DeltaLima
  • 5,864
  • 1
  • 16
  • 32
60

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
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • 2
    nice, any idea what version this was introduced? save a lot of time by not creating stupid format strings... – iCodeLikeImDrunk Nov 07 '16 at 19:40
  • Yeah I totally agree. It looks like it has always been there. http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html – Chad Bingham Nov 07 '16 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. – Joshua Pinter Aug 28 '20 at 02:13
24
DateTime date = DateTime.now().withTimeAtStartOfDay();
date.toString("HH:mm:ss")
Ahmad
  • 69,608
  • 17
  • 111
  • 137
13

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.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
  • Your link to documentation is outdated. Correct URL is: http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html – Basil Bourque Apr 24 '14 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. – The Guy with The Hat Apr 24 '14 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. :)

Clad Clad
  • 2,653
  • 1
  • 20
  • 32
4

Please try to this one

public void Method(Datetime time)
{
    time.toString("yyyy-MM-dd'T'HH:mm:ss"));
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Singh
  • 47
  • 1
  • 2
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();
EdwinCab
  • 361
  • 1
  • 3
  • 17
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.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
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();
}
ctesniere
  • 131
  • 8
Eddie
  • 27
  • 3
  • 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. – Ole V.V. Nov 08 '21 at 19:46
-1

easiest way:

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