tl;dr
LocalDateTime.parse( // Parse as a `LocalDateTime` because the input lacks indication of zone/offset.
"Mar 19 2018 - 14:39" ,
DateTimeFormatter.ofPattern( "MMM dd uuuu - HH:mm" , Locale.US )
) // Returns a `LocalDateTime` object.
.toLocalTime() // Extract a time-of-day value.
.toString() // Generate a String in standard ISO 8601 format.
14:39
java.time
The modern approach uses the java.time classes.
Define a formatting pattern to match your input. Specify Locale
to determine human language and cultural norms used in parsing this localized input string.
String input = "Mar 19 2018 - 14:39" ;
Locale locale = Locale.US ; // Specify `Locale` to determine human language and cultural norms used in parsing this localized input string.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM dd uuuu - HH:mm" , locale ) ;
Parse as a LocalDateTime
because the input lacks any indicator of time zone or offset-from-UTC.
LocalDateTime ldt = LocalDateTime.parse( input , f );
Extract the time-of-day value, without a date and without a time zone, as that is the goal of the Question.
LocalTime lt = ldt.toLocalTime();
ldt.toString(): 2018-03-19T14:39
lt.toString(): 14:39
ISO 8601
The format of your input is terrible. When serializing date-time values as text, always use standard ISO 8601 formats.
The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings. You can see examples above in this Answer.
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.