tl;dr
Use java.time classes built into Java, not Joda-Time.
LocalTime.parse( "23:45:12.123456789" ) // Represent a time-of-day without date and without time zone. Parsing standard ISO 8601 format HH:MM:SS.SSSSSSSSS in 24-hour clock. Returns a `LocalTime` object.
.truncatedTo( ChronoUnit.MINUTES ) // Produce a new value, based on original, but lopping off any seconds or fractional-second. Returns another `LocalTime` object.
.toString() // Generate a `String` with text representing the value of this date-time value, using standard ISO 8601 format. Returns a `String` object.
23:45
but I get an error for the LocalTime.parse
Your specified formatting pattern must match the input string – yours was a mismatch.
As seen in code above, if inputting standard ISO 8601 format, the built-in LocalTime.parse
feature handles variations. So no need to bother specifying a formatting pattern at all.
Details
The Answer by Reimeus is correct, but uses the outmoded Joda-Time classes. While an excellent library, its creators (principally Stephen Colebourne) went on to create the java.time classes built into Java 8 and later. The Joda-Time project is now in maintenance-mode, and advises migration to java.time.
java.time
If your user’s inputs go by the standard ISO 8601 formats, you need not specifying a formatting pattern to parse the string inputs. This means 24-hour clock using hours 1-23, and a leading padding zero for hours 1-9: 01-09. The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings.
String input1 = "23:45";
String input2 = "23:45:12";
String input3 = "23:45:12.123456789";
LocalTime lt1 = LocalTime.parse( input1 );
LocalTime lt2 = LocalTime.parse( input2 );
LocalTime lt3 = LocalTime.parse( input3 );
System.out.println( lt1 );
System.out.println( lt2 );
System.out.println( lt3 );
23:45
23:45:12
23:45:12.123456789
Suppress display
If you want to suppress display of any seconds or fractional-second, use the DateTimeFormatter
class. That class is well-documented, and has been covered many many times already on Stack Overflow, so search for more discussion and examples.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "HH:mm" , Locale.US ) ;
String output = myLocalTime.format( f ) ;
23:45
Truncate
If you want to drop any seconds or fractional-second from your date-time value, truncate.
LocalTime.parse( "23:45:12" )
.truncatedTo( ChronoUnit.MINUTES )
.toString()
23:45
…or…
LocalTime.parse( "23:45:12.123456789" )
.truncatedTo( ChronoUnit.MINUTES )
.toString()
23:45
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.