Format a datetime string to time only

30,718

Solution 1

You need to do the following.

try {
   Date date = null;
   date = df.parse("2011-01-24 02:45:00");
   SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
   String shortTimeStr = sdf.format(date);
   System.out.println(shortTimeStr);
} catch (ParseException e) {
   // To change body of catch statement use File | Settings | File Templates.
   e.printStackTrace(); 
}

As I mentioned in comment, you should store the time as milliseconds instead of string. Otherwise, I do not see any other way than creating a middleman object Date.

Solution 2

SimpleDateFormat.format expects Date or Calendar as argument while you're passing a String. Convert the String into Date first.

String dateStr = cursor.getString(cursor.getColumnIndex("start_time"));
SimpleDateFormat toFullDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fullDate = toFullDate.parse(dateStr);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String shortTimeStr = sdf.format(fullDate); 

And yeah, I'd rather stored date as long in the database, so getting a Date object then would be more trivial:

Long dateMilliseconds = cursor.getLong(cursor.getColumnIndex("start_time"));
Date fullDate = new Date(dateMilliseconds);
Share:
30,718
Squonk
Author by

Squonk

Learned the fundamentals of BASIC programming in 1978 from a 4 page magazine pull-out. Since then I've coded in hex (6502, 8080, Z80, x86), Fortran, Pascal, C/C++, VB6, C# and I'm now hooked in to Java programming for Android devices. Over the years I've done most things from electronics design/development to furniture removals, factory work, fast food delivery and network systems administration. One day I'll work out what I want to do when I grow up. :-)

Updated on September 06, 2020

Comments

  • Squonk
    Squonk over 3 years

    I'm retrieving a datetime from a SQLite DB which comes out in the format...

    2011-01-24 02:45:00
    

    In C# I can simply use DateTime.Parse("2011-01-24 02:45:00").ToString("HH:mm") in order to get the string 02:45

    Is their a way I can I do this in Android/Java? My code in my Android app looks like this...

    // dateStr below is logging as correctly being yyyy-MM-dd HH:mm:ss format
    String dateStr = cursor.getString(cursor.getColumnIndex("start_time"));
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    String shortTimeStr = sdf.format(dateStr); // <-- throws IllegalArgumentException
    

    EDIT: Thanks to both doc_180 and Konstantin Burov - the examples given have helped me sort out the problem. Part of the issue was that I was importing java.sql.Date instead of java.util.Date. I've changed things around and it's working perfectly for me now.

    • uncaught_exceptions
      uncaught_exceptions over 13 years
      SimpleDateFormat needs a date arguement, not string. You should store your date as milliseconds and create a date object from that. Date d = new Date(dateAsMillisec) and then pass that to simpledateformat.
    • Squonk
      Squonk over 13 years
      @doc_180: OK sorry, you're quite correct - probably need to rethink my question. My problem is my DB on the Android side is a 'mirror' of a PC-based app's SQLite DB which uses that long/reverse datetime format and I'd like to keep the formats the same. I tried using Date.parse(String) then sdf.format(...) but that also threw an exception.
    • uncaught_exceptions
      uncaught_exceptions over 13 years
      Did you try my code. It works and I have checked.
  • Squonk
    Squonk over 13 years
    Thank you, see my EDIT at the end of my question. I think storing date as long (milliseconds) might be a way to go in the future but at the moment, I'm mirroring a PC SQLite DB created by someone else so I'm sticking with the same formats between PC/Android.
  • Squonk
    Squonk over 13 years
    Thank you, see my EDIT at the end of my question. I think storing date as long (milliseconds) might be a way to go in the future but at the moment, I'm mirroring a PC SQLite DB created by someone else so I'm sticking with the same formats between PC/Android.
  • Rick
    Rick about 10 years
    What is the df stand for?
  • turbandroid
    turbandroid over 8 years
    df stands for DateFormat. it seems that df has reference of SimpleDateFormat to parse date.