JFormattedTextField :输入持续时间值
我想使用 JFormattedTextField
允许用户在表单中输入时间duration 值。有效值示例为:
2h 30m
72h 15m
6小时
0h
但是我在这方面取得的成功有限。有人可以建议如何实现这一目标吗?如果使用 JTextField
也能实现此结果,我就可以了。
谢谢!
如果它有价值的话,这是我当前的尝试:
mFormattedText.setFormatterFactory(
new DefaultFormatterFactory(
new DateFormatter(
new SimpleDateFormat("H mm"))));
这种方法有效,除了:
我无法让*h
和m
显示为纯文本(我尝试转义)- 小时数有上限
*:参见 @nanda 的回答
I want to use a JFormattedTextField
to allow the user to input time duration values into a form. Sample valid values are:
2h 30m
72h 15m
6h
0h
However I am having limited success with this. Can some one please suggest how this can be accomplished? I am OK if this result can be achieved using a JTextField
as well.
Thanks!
If it is worth anything, here's my current attempt:
mFormattedText.setFormatterFactory(
new DefaultFormatterFactory(
new DateFormatter(
new SimpleDateFormat("H mm"))));
This sorta works except that:
I cannot get*h
andm
to appear as plain text (I tried escaping)- The number of hours has a max
*: See @nanda's answer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面是使用
InputVerifier
以适应多种输入格式。Here's an example of using
InputVerifier
to accommodate multiple input formats.代码:
The code:
您尝试过
H'h'mm'm'
吗?Have you tried
H'h' mm'm'
?嗯,我认为您可以通过为所有三个时间组件创建三个不同的 JTextField 来实现相同的目标,一个用于小时、分钟和秒(如果包含的话)来获取您的输入...只是一个想法,如果有必要的话,你可以将它们连接起来......只是一个想法......
hmm, what i think is that you can achieve the same goal by, creating three different JTextField for all the three time components, one for the HOUR, MINUTE and Second (if it's included) to get your input... just a thought, you could just concatenate them if you it's necessary... just a thought...
tl;dr
ISO 8601
如果您愿意重新定义所需的输入格式,我建议使用 ISO 8601 标准。
模式
PnYnMnDTnHnMnS
使用P
标记开始,使用T
将任何年-月-日部分与任何小时-分钟-秒分开部分。例如,一个半小时是
PT1H30M
。java.time
java.time 类在解析/生成字符串时默认使用 ISO 8601 格式。这包括
期间
< /a> 和持续时间
用于表示未附加到时间线的时间跨度的类。转向另一个方向,解析字符串。
请参阅IdeOne.com 中的实时代码。
请参阅我对的类似答案类似的问题。
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
ISO 8601
If you are willing to redefine your desired input formats, I suggest using the already-existing formats defined by the ISO 8601 standard.
The pattern
PnYnMnDTnHnMnS
uses aP
to mark the beginning, aT
to separate any years-months-days portion from any hours-minutes-seconds portion.An hour-and-a-half is
PT1H30M
, for example.java.time
The java.time classes use the ISO 8601 formats by default when parsing/generating strings. This includes the
Period
andDuration
classes for representing spans of time not attached to the timeline.Going the other direction, parsing a string.
See live code in IdeOne.com.
See my similar Answer to a similar Question.
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.
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.