0

I have a datetime stamp from the server in the form of 2017-12-27 03:31:31.243 and would like to extract the time part for display as say 3:31 am. The actual date aspect is irrelevant for my current requirements.

I realize that the Date class has been deprecated and I know how to format the date into human readable form but have not been able to figure out extracting only the time.

Any insight is appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
FevtheDev
  • 113
  • 2
  • 10
  • Possible duplicate of [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string) – Usi May 05 '18 at 21:00
  • 1
    @JeffColeman I am only trying to retrieve the time though, and without referencing the Date class because that has become deprecated and would rather not. I figured it out regardless. Will post solution below – FevtheDev May 05 '18 at 21:04

1 Answers1

3

I found this to do the trick!

String dateStamp = "2017-12-27 03:31:31.243";
Timestamp timestamp = Timestamp.valueOf(dateStamp);
SimpleDateFormat fmt = new SimpleDateFormat("hh:mm a");
String formattedTime = fmt.format(timestamp);
return formattedTime;

output:

03:31 AM
FevtheDev
  • 113
  • 2
  • 10