-1

How to get time from String (14-04-2021 18:04:87) ?

How to get only time from this String "14-04-2021 18:04:87" ?

I need output as 06:04 pm

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

1

You can use SimpleDateFormat

   String dateString = "14-04-2021 18:04:87";
     
   Date date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(dateString);
    
   String result = new SimpleDateFormat("hh:mm aa").format(date);
Emerson Micu
  • 450
  • 8
  • 17
  • Thank you so much Emerson Micu , it works well :) – Beginner Dev Apr 14 '21 at 15:54
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table). – Ole V.V. Apr 14 '21 at 16:42
  • Thank youfor the suggestions Ole V.V – Beginner Dev Apr 16 '21 at 00:06