52

How can I format the "2010-07-14 09:00:02" date string to depict just "9:00"?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JJunior
  • 2,831
  • 16
  • 56
  • 66

7 Answers7

118

Use DateTimeFormatter to convert between a date string and a real LocalDateTime object. with a LocalDateTime as starting point, you can easily apply formatting based on various patterns as definied in the javadoc of the DateTimeFormatter.

String originalString = "2010-07-14 09:00:02";
LocalDateTime dateTime = LocalDateTime.parse(originalString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String newString = DateTimeFormatter.ofPattern("H:mm").format(dateTime); // 9:00

In case you're not on Java 8 or newer yet, use SimpleDateFormat to convert between a date string and a real Date object. with a Date as starting point, you can easily apply formatting based on various patterns as definied in the javadoc of the SimpleDateFormat.

String originalString = "2010-07-14 09:00:02";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(originalString);
String newString = new SimpleDateFormat("H:mm").format(date); // 9:00
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • hi is it also possible to format it in hours:min if I have a unixtimestamp like: 1279080000 ?? I tried what was suggested in previous posts but did not help. Thank you – JJunior Aug 17 '10 at 17:20
  • You can construct a new `Date` object with timestamp in milliseconds and then just format it. If you're actually retrieving it from DB, you should use `ResultSet#getTimestamp()` to get a `Date` as answered in [one of your previous questions](http://stackoverflow.com/questions/3473421/). Or even better, make use of the SQL builtin date/time functions to return the timestamp already in the desired format so that there's no need for the expensive task of remassaging the data of every row afterwards in Java. In that case, just get the hour by `ResultSet#getString()`. Use the right tool for the job – BalusC Aug 17 '10 at 17:22
  • 1
    i am getting it from the database. but needed to generate a print report. so thought i could iterate on the values and change them on the fly. as i need to just print it out in the end. unable to get it done in any starightforward way :( – JJunior Aug 17 '10 at 18:38
  • @BalusC Is there a way to display it in the user's phone format, ie, am/pm or 24 hr depending on the phone preferences, without having to set a default like "H:mm"? – Banana Jul 03 '17 at 15:19
  • And how to get **AM/PM**? – Faizan Mubasher Apr 03 '19 at 14:30
  • @FaizanMubasher: As answered *"you can easily apply formatting based on various patterns as definied in the javadoc of the SimpleDateFormat (click the blue code link for the Javadoc)."* – BalusC Apr 03 '19 at 14:42
14
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2010-07-14 09:00:02");
String time = new SimpleDateFormat("H:mm").format(date);

http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

Adam
  • 43,763
  • 16
  • 104
  • 144
13

A very simple way is to use Formatter (see date time conversions) or more directly String.format as in

String.format("%tR", new Date())
Petr
  • 62,528
  • 13
  • 153
  • 317
5

The other answers were good answers when the question was asked. Time moves on, Date and SimpleDateFormat get replaced by newer and better classes and go out of use. In 2017, use the classes in the java.time package:

    String timeString = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))
            .format(DateTimeFormatter.ofPattern("H:mm"));

The result is the desired, 9:00.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
4

I'm assuming your first string is an actual Date object, please correct me if I'm wrong. If so, use the SimpleDateFormat object: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html. The format string "h:mm" should take care of it.

MikeTheReader
  • 4,200
  • 3
  • 24
  • 38
  • 1
    Wouldn't `H:mm` be a more sane choice? – Joey Aug 17 '10 at 17:09
  • Even if the String is *not* a `Date` object initially, you can use `SimpleDateFormat.parse()` to turn it into one, as per BalusC's example. – Andrzej Doyle Aug 17 '10 at 17:10
  • 1
    Indeed, without AM/PM marker (`a`), you'd rather prefer `H` above `h` to avoid ambiguity between 9:00 (AM) and 9:00 (PM). – BalusC Aug 17 '10 at 17:11
3

If you have date in integers, you could use like here:

Date date = new Date();
date.setYear(2010);
date.setMonth(07);
date.setDate(14)
date.setHours(9);
date.setMinutes(0);
date.setSeconds(0);
String time = new SimpleDateFormat("HH:mm:ss").format(date);
Bohdan Kolesnyk
  • 135
  • 2
  • 7
  • 1
    Please explain your answer to make it clearer for other readers. – Mohit Jain Jun 23 '15 at 13:03
  • You may use 07 for August if for some reason you fancy that, but be aware that 08 or 09 won’t work for September or October. The 0 prefix means the number is octal. – Ole V.V. May 03 '17 at 19:57
  • Also note that if you intended to hit the date in the question, `2010-07-14`, you missed. `Date` months are 0-based, so you got August, not July. You may argue that since you are outputting the time only, it’s not important, but it’s sure to confuse some. Further be aware that the `setXx` methods have been deprecated since Java 1.1 because they are unreliable across time zones. – Ole V.V. May 03 '17 at 20:11
0
let datestring = "2017-02-14 02:16:28"

let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.full
formatter.timeStyle = DateFormatter.Style.full

formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

let date = formatter.date(from: datestring)
let date2 = formatter.String(from: date)
BHAVIK
  • 890
  • 7
  • 35