-3

I am quite new to Java windowbuilder, and this is part of my first project.

String starttime = JOptionPane.showInputDialog(null, "What time would you like to start your revision ? (ie:12:24) ");

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalTime start = LocalTime.parse(starttime,dtf);

I want to convert the format of start from ("HH:mm:ss..etc") to ("HH:mm"), but I get an error for the LocalTime.parse for some reason. Any suggestions what I should do.

I'm using Joda Time

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Ganges
  • 1
  • 1
  • 2

2 Answers2

4

You're incorrectly referencing ofPattern in java.time. Use JodaTime's forPattern

DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:mm");
LocalTime start = LocalTime.parse(starttime, dtf);
System.out.println(dtf.print(start));

or simply

System.out.println(LocalTime.parse(startTime).toString("HH:mm"));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • The error no longer appears, but the value of start still shows seconds and milliseconds (which I do not want). Do you have any suggestions as to how I can get start to just hours and minutes ? – Ganges Aug 13 '18 at 15:47
  • use the `DateTimeFormatter` you created in this question when displaying the time as shown in the updated answer – Reimeus Aug 13 '18 at 15:53
1

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154