2

The title is quite self explanatory.

I realize this might seem trivial and already answered but it is not.

All the answers I found either:

  1. Ask for a format to be specified/hardcoded as "yyyyMMdd HH:mm" (e.g. this answer)

  2. Or they return both date and time (e.g. this).

I need just the time returned from a existing Date object as a string in the user's current locale.

Community
  • 1
  • 1
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • **For future visitors of this page**: The legacy date-time API (`java.util` date-time types and their formatting type, `SimpleDateFormat`) is outdated and error-prone. It is recommended to stop using it completely and switch to `java.time`, the [modern date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html). You can check [this answer](https://stackoverflow.com/a/67449876/10819573) that uses the `java.time` API. – Arvind Kumar Avinash May 09 '21 at 15:23

1 Answers1

2

I might be missing something here, but wouldn't the following work?

final DateFormat f = DateFormat.getTimeInstance();
f.format(new Date()));

This outputs e.g.: 10:24:24 AM

Similarly, if you wish to extract only the date, use DateFormat.getDateInstance().

Tadej
  • 2,901
  • 1
  • 17
  • 23
  • True. I don't know how I missed that. Any idea how to use a similar approach to get just the date? I am assuming `getDateInstance()`? – Dzhuneyt Sep 22 '14 at 09:34