如何格式化 LocalTime 变量

How to format a LocalTime variable

我对 Java windowbuilder 还很陌生,这是我第一个项目的一部分。

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

我想将开始的格式从 ("HH:mm:ss..etc") 转换为 ("HH:mm"),但由于某种原因我收到 LocalTime.parse 的错误。有什么建议我应该怎么做。

我正在使用 Joda Time

您引用的内容不正确 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));

或者干脆

System.out.println(LocalTime.parse(startTime).toString("HH:mm"));

tl;博士

使用 java.time classes 内置于 Java,而不是 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

您指定的格式模式必须与输入字符串匹配——您的不匹配。

如上面的代码所示,如​​果输入标准 ISO 8601 格式,内置的 LocalTime.parse 功能会处理变化。所以根本不需要指定格式模式。

详情

是正确的,但使用了过时的 Joda-Time classes。虽然它是一个优秀的库,但它的创建者(主要是 Stephen Colebourne)继续创建 java.time classes 内置到 Java 8 及更高版本中。 Joda-Time 项目现在处于维护模式,建议迁移到 java.time

java.time

如果您的用户输入采用标准 ISO 8601 格式,您无需指定格式模式来解析字符串输入。这意味着使用 1-23 小时的 24 小时时钟,以及 1-9 小时的前导填充零:01-09。当 parsing/generating 字符串时,java.time classes 默认使用标准 ISO 8601 格式。

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

抑制显示

如果要禁止显示任何秒或小数秒,请使用 DateTimeFormatter class。 class 有详细记录,并且已经在 Stack Overflow 上多次介绍,因此请搜索更多讨论和示例。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "HH:mm" , Locale.US ) ;
String output = myLocalTime.format( f ) ;  

23:45

截断

如果您想从日期时间值中删除任何秒数或小数秒,请截断。

LocalTime.parse( "23:45:12" )
         .truncatedTo( ChronoUnit.MINUTES ) 
         .toString()

23:45

……或者……

LocalTime.parse( "23:45:12.123456789" )
         .truncatedTo( ChronoUnit.MINUTES ) 
         .toString()

23:45


关于java.time

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.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* classes.

从哪里获得java.time classes?

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.