DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = fmt.parseLocalTime("02:51:20");
System.out.println("LocalTime: "+localTime);
我只需要02:51:20,但输出结果是02:51:20.000。有没有办法可以从输出中去掉.000(毫秒)?
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = fmt.parseLocalTime("02:51:20");
System.out.println("LocalTime : " + localTime);
System.out.println("LocalTime with DateTimeFormatter : " + fmt.print(localTime));
输出结果如下:
LocalTime : 02:51:20.000
LocalTime with DateTimeFormatter : 02:51:20
toString()
表示。如果您对已有的格式化程序感到满意,只需使用以下代码即可:System.out.println("LocalTime: " + fmt.print(localTime));
Fixed this using
DateTime time = new DateTime();
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
String localTime = time != null ? format.print(new LocalTime(time.getHourOfDay(),
time.getMinuteOfHour(), time.getSecondOfMinute())) : null;
DateTime and LocalTime are from joda
LocalTime
的.toString()
是固定的。你需要通过格式化程序来获得所需的格式。 - fge