在用户在JDateChooser中选择出生日期之后,在文本字段中显示年龄 [英] To display age in textfield after user has selected date of birth in JDateChooser
问题描述
我的程序中有一个JDateChooser.每当从JDateChooser中选择他的出生日期时,我都希望他的年龄显示在JTextField中.
I have a JDateChooser in my program. Whenever the selects his date of birth from JDateChooser , I want his age to be displayed in a JTextField.
最初,我尝试使它与MouseListener一起使用:
Initially, I tried to make it work with the MouseListener as :
private void jDateChooser1MouseExited(java.awt.event.MouseEvent evt)
{
Calendar dob = Calendar.getInstance();
//utilDob is a java.util.Date variable which stores date selected by user
dob.setTime(utilDob);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH))
age--;
else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)
&& today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH))
age--;
jTextField11.setText(Integer.toString(age));
displayAge=Integer.parseInt(jTextField11.getText());
}
但是,上述功能对我没有帮助.我还可以使用其他事件/动作侦听器吗?
But, the above mentioned function didn't help me. Is there any other event/action listener I can use?
推荐答案
您正在使用过时的类.
You are using outmoded classes.
Java 8和更高版本内置了java.time框架.
Java 8 and later comes with the java.time framework built-in.
LocalDate
类表示仅日期的值,没有日期和时区.但是时区对于确定今天"至关重要.在任何给定时刻,日期可能会在世界各地随时区变化.巴黎午夜过后的片刻仍是蒙特利尔的昨天".
The LocalDate
class represents a date-only value, without time-of-day and without time zone. But time zone is critical it determining "today". For any given moment the date may vary around the world by time zone. A few moments after midnight in Paris is still "yesterday" in Montréal.
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
LocalDate today = LocalDate.now ( zoneId );
根据您的用户输入来建立出生日期.
Build a date-of-birth from your user input.
LocalDate dob = LocalDate.of ( 1955 , 1 , 2 );
或解析符合 ISO 8601 标准的字符串.
Or parse a string compliant with the ISO 8601 standard.
LocalDate dob = LocalDate.parse( "1955-01-02" );
或使用DateTimeFormatter
来帮助解析其他字符串.
Or use a DateTimeFormatter
to help parse a different string.
如果手头有java.util.Date
对象,请使用添加到该旧类中的新方法来转换为java.time:java.util.Date::toInstant
. Instant
是UTC时间轴上的时刻.
If you have a java.util.Date
object on hand use a new method added to that old class for conversion to java.time: java.util.Date::toInstant
. An Instant
is a moment on the timeline in UTC.
Instant instant = myJavaUtilDate.toInstant();
苹果公司所在的时区.
Apple a time zone.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
提取一个LocalDate
.
LocalDate localDate = zdt.toLocalDate();
Period
年龄只是一种消逝的时间,是一段时间.在java.time中,Period
类表示以年,月和日为单位的时间跨度.
Period
Age is just one kind of elapsed time, a span of time. In java.time, the Period
class represents a span of time tracked in terms of years, months, and days.
Period age = Period.between ( dob , today );
从Period
获取年份
如果您想要的话,请询问年份.
Get years from Period
Ask for the years if that is all you want.
int years = age.getYears ();
请注意,您可能会认为getYears
不是 归一化为全部年数.根据Period
的构造方式,您可能会得到意想不到的结果.另一种方法是调用toTotalMonths()
并除以12(十二个月到一年).在此快速示例中,请参见两种方法.比较y
和y2
.
Beware that getYears
is not normalized to the full number of years as you may intuit. Depending on how a Period
is constructed, you may get an unexpected result. Another approach is to call toTotalMonths()
and divide by 12 (twelve months to a year). See both approaches in this quick example; compare y
and y2
.
Period p = Period.ofMonths ( 25 );
int y = p.getYears ();
long y2 = ( ( p.toTotalMonths () ) / 12 );
System.out.println ( "p: " + p + " | y: " + y + " | y2: " + y2 );
p:P25M | y:0 | y2:2
p: P25M | y: 0 | y2: 2
更好的是,强制将Period
对象规范化为完整的年份,然后是几个月,然后是几天.好吧,不是完全强制对象:由于不可变对象,您实际上是在实例化一个基于原始对象的值.
Even better, force the Period
object to be normalized to a full count of years then months then days. Well, not exactly force the object: Because of immutable objects, you are actually instantiating a new object based on the original object’s values.
因此,尽管我们的age
对象由于其特殊的构造而已经被规范化,但总是调用normalized
可能是一个好习惯.
So while our age
object happens to already be normalized because of its particular construction, it might be a good habit to always call normalized
.
Period age = Period.between ( dob , today ).normalized();
转储到控制台.默认情况下,Period::toString
方法根据持续时间的ISO 8601标准生成字符串.例如在这里看到的P61Y2M27D
.
Dump to console. By default, the Period::toString
method generates a string according to the ISO 8601 standard for Durations such as P61Y2M27D
seen here.
System.out.println ( "zoneid: " + zoneId + " | today: " + today + " | dob: " + dob + " | age: " + age + " | years: " + years );
zoneid:美国/蒙特利尔|今天:2016-03-29 | dob:1955-01-02 |年龄:P61Y2M27D |年:61
zoneid: America/Montreal | today: 2016-03-29 | dob: 1955-01-02 | age: P61Y2M27D | years: 61
将int
转换为Integer
最后,如果您需要一个对象而不是原始对象,请将int
转换为Integer
.
Convert int
to Integer
Lastly, if you need an object rather than a primitive, convert int
to Integer
.
Integer yearsObj = Integer.valueOf(years);
Integer yearsObj = Integer.valueOf ( years );
这篇关于在用户在JDateChooser中选择出生日期之后,在文本字段中显示年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!