tl;dr
Use modern java.time classes OffsetDateTime
, LocalTime
, and DateTimeFormatter
, and localize using Locale
.
OffsetDateTime // Represent a moment as a date and time-of-day with an offset of hours-minutes-seconds ahead of or behind UTC.
.parse( "2001-07-04T12:08:56.235-07:00" ) // Standard ISO 8601 strings can be parsed directly, without you needing to specify a formatting pattern. Returns an `OffsetDateTime` object.
.toLocalTime() // Extract just the time-of-day portion, omitting the date and the offset-from-UTC. Returns a `LocalTime` object.
.format( // Generate text to represent the time-of-day value held in this `LocalTime` object.
DateTimeFormatter // Class to control generate text from a date-time object.
.ofLocalizedTime( // Ask `DateTimeFormatter` to automatically localize the presentation formatting of the text.
FormatStyle.SHORT // Specify how long or abbreviated the text should be.
) // Returns a `DateTimeFormatter` object.
.withLocale( // Specify the locale to use in localization, to determine (a) the human language to use in translation, and (b) the cultural norms to decide issues such as capitalization, abbreviation, punctuation, order of elements.
Locale.US
) // Returns another `DateTimeFormatter` object. The java.time class use the immutable objects pattern, returning a fresh object base on the original’s values rather than changing (“mutating”) the original.
) // Returns a `String`.
12:08 PM
Details
like this "2001-07-04T12:08:56.235-07:00"
That text is in standard ISO 8601 format for a moment.
The modern java.time classes use those standard formats by default when parsing/generating text.
OffsetDateTime odt = OffsetDateTime.parse( "2001-07-04T12:08:56.235-07:00" ) ;
I want it to consist of Hour and Minute.
Do you want to show the time as seen through the same offset-from-UTC? That offset of -07:00
, seven hours behind UTC, is possibly from a time zone on the west coast of North America, such as America/Vancouver
or America/Los_Angeles
as you can see here.
If so, extract the time-of-day.
LocalTime lt = odt.toLocalTime() ;
Then generate a string representing the value of that LocalTime
object. Generally best to let java.time automatically localize for you.
Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale ) ;
String output = lt.format( f );
12:08 PM
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?