如何格式化LocalTime变量 [英] How to format a LocalTime variable

查看:412
本文介绍了如何格式化LocalTime变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想将起始格式从("HH:mm:ss..etc")转换为("HH:mm"),但是由于某些原因,我在LocalTime.parse上遇到了错误.任何建议我应该怎么做.

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.

我正在使用Joda Time

I'm using Joda Time

推荐答案

您错误地引用了 forPattern

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

或者简单地

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

这篇关于如何格式化LocalTime变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆