How to split date and time from a datetime string?

(Edit: See Answer by Ole V.V. below for a more modern (Java 8) approach for the first version)

One way to do is parse it to a date object and reformat it again:

    try {
        DateFormat f = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
        Date d = f.parse("8/29/2011 11:16:12 AM");
        DateFormat date = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat time = new SimpleDateFormat("hh:mm:ss a");
        System.out.println("Date: " + date.format(d));
        System.out.println("Time: " + time.format(d));
    } catch (ParseException e) {
        e.printStackTrace();
    }

If you just want to slice it into date-time pieces, just use split to get pieces

    String date = "8/29/2011 11:16:12 AM";
    String[] parts = date.split(" ");
    System.out.println("Date: " + parts[0]);
    System.out.println("Time: " + parts[1] + " " + parts[2]);

or

    String date = "8/29/2011 11:16:12 AM";
    System.out.println("Date: " + date.substring(0, date.indexOf(' ')));
    System.out.println("Time: " + date.substring(date.indexOf(' ') + 1));

java.time, the modern Java date and time API, and ThreeTenABP

It’s time someone provides the modern answer to this question. I do here.

I am not at complete ease with your requirements, though. For the vast majority of purposes and situations you should not keep your date nor your time of day in a string. You should use proper date-time objects. So as I see it, this is what you should want to do:

    LocalDateTime dateTime = LocalDateTime.of(2011, Month.AUGUST, 29, 11, 16, 12);
    LocalDate date = dateTime.toLocalDate();
    LocalTime time = dateTime.toLocalTime();
    System.out.println("date = " + date);
    System.out.println("time = " + time);

Output is:

date = 2011-08-29
time = 11:16:12

Assuming that 8/29/2011 11:16:12 AM has been entered as user input you need to parse it into a LocalDateTime that you can keep in your program. This goes like this:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("M/d/u h:m:s a", Locale.ENGLISH);
    String userInput = "8/29/2011 11:16:12 AM";
    LocalDateTime dateTime = LocalDateTime.parse(userInput, formatter);
    System.out.println("dateTime = " + dateTime);
dateTime = 2011-08-29T11:16:12

Going the other way, to output to the user: If your separate date and time strings are for output only, we don’t need to separate date and time before formatting into those strings. We can format the LocalDateTime directly.

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("M/d/u");
    DateTimeFormatter timeFormatter
            = DateTimeFormatter.ofPattern("h:mm:ss a", Locale.ENGLISH);
    String dateString = dateTime.format(dateFormatter);
    String timeString = dateTime.format(timeFormatter);
    System.out.println("dateString = " + dateString);
    System.out.println("timeString = " + timeString);
dateString = 8/29/2011
timeString = 11:16:12 AM

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

Why not to use DateFormat?

Check http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

String str = "8/29/2011 11:16:12 AM";

String fmt = "MM/dd/yyyy HH:mm:ss a";
DateFormat df = new SimpleDateFormat(fmt);

Date dt = df.parse(str);

DateFormat tdf = new SimpleDateFormat("HH:mm:ss a");
DateFormat dfmt  = new SimpleDateFormat("MM/dd/yyyy");


String timeOnly = tdf.format(dt);
String dateOnly = dfmt.format(dt);

It gives more work/code but it's the proper way to do it.