Using java.time
The other answers are correct but now outdated.
The java.time.LocalTime
class represents a time-of-day without a date and without a time zone. The class can directly parse a string in standard ISO 8601 format using 24-hour clock: HH:MM or HH:MM:SS
LocalTime lt = LocalTime.parse( "14:30" );
String output = lt.toString(); // 14:30
Formatting
Use a DateTimeFormatter
if you want other formats. Even localize automatically.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL );
f = f.withLocale( Locale.CANADA_FRENCH ); // Cultural norms & human language
String output = lt.format( f );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
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.