How to format a LocalTime variable

萝らか妹 提交于 2021-02-05 12:36:25

问题


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


回答1:


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"));



回答2:


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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use 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.



来源:https://stackoverflow.com/questions/51825146/how-to-format-a-localtime-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!