select 来自 jdatechooser 的日期并在文本字段中自动显示年龄
select date from jdatechooser and automatically diplay age in a Textfield
我正在尝试从 Jdatechooser 中获取选定的日期并在文本字段中显示与该日期对应的年龄,我看到所有讨论此问题的线程,但它不适用于 me.here 是我的代码:
private void datenaiss_choozPropertyChange(java.beans.PropertyChangeEvent evt) {
Date date = (Date) datenaiss_chooz.getDate();
LocalDate ld = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Period p =Period.between(ld, LocalDate.now());
age.setText(p);
}
使用 joda-time 解决您的问题:
DateTime birthDate = new DateTime(1990, 1, 15, 0, 0, 0, 0);
DateTime now = new DateTime();
Period period = new Period(birthDate, now);
int years = period.getYears();
System.out.println(years);
这将为您提供所需的差异!
经过长时间的努力,我终于得到了解决方案,感谢大家的帮助me.Here我的代码运行得很好:
DateTime start = new DateTime(datenaiss_chooz.getDate());
Years h = Years.yearsBetween(start, new DateTime());
StringBuilder b = new StringBuilder();
b.append(h.getYears());
String str = b.toString();
if ( h.getYears()==0)
age.setText("");
else
age.setText(str+" "+"ans");
我正在尝试从 Jdatechooser 中获取选定的日期并在文本字段中显示与该日期对应的年龄,我看到所有讨论此问题的线程,但它不适用于 me.here 是我的代码:
private void datenaiss_choozPropertyChange(java.beans.PropertyChangeEvent evt) {
Date date = (Date) datenaiss_chooz.getDate();
LocalDate ld = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Period p =Period.between(ld, LocalDate.now());
age.setText(p);
}
使用 joda-time 解决您的问题:
DateTime birthDate = new DateTime(1990, 1, 15, 0, 0, 0, 0);
DateTime now = new DateTime();
Period period = new Period(birthDate, now);
int years = period.getYears();
System.out.println(years);
这将为您提供所需的差异!
经过长时间的努力,我终于得到了解决方案,感谢大家的帮助me.Here我的代码运行得很好:
DateTime start = new DateTime(datenaiss_chooz.getDate());
Years h = Years.yearsBetween(start, new DateTime());
StringBuilder b = new StringBuilder();
b.append(h.getYears());
String str = b.toString();
if ( h.getYears()==0)
age.setText("");
else
age.setText(str+" "+"ans");