繁体   English   中英

如何在jframe标题栏上显示当前日期?

[英]How to show current date on a jframe title bar?

我搜索了解决方案,但找不到。 我想在我的jframe标题栏上显示当前日期。 我知道如何使用Java获取当前日期,但需要在标题栏上显示它。 有人可以帮我吗?

提前致谢。

要获取当前日期>,请使用java.util.Date

要格式化日期>,请使用java.text.SimpleDateFormat

为JFRame设置标题>

JFrame jFrame= new JFrame("title");

要么

jFrame.setTitle("title");

所以解决方案是

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();

JFrame jFrame= new JFrame("Current Date: "+dateFormat.format(date));

or 

jFrame.setTitle("Current Date"+dateFormat.format(date));

Nidheesh回答是正确的。

使用Joda-Time进行本地化

如果您想要日期的本地化格式,请使用Joda-Time库。

DateTime now = DateTime.now( DateTimeZone.getDefault() );

或者,如果您有java.util.Date可以转换为Joda-Time。

DateTime dateTime = new DateTime( date, DateTimeZone.forID( "America/Montreal" ) );

使用本地化格式生成日期时间值的字符串表示形式。 语言环境对象与Joda-Time DateTimeFormat.forStyle

java.util.Locale locale = new Locale( "fr", "CA" ); // Québécois style.
DateTimeFormatter formatter = DateTimeFormat.forStyle( "SS" ).withLocale( locale );
String output = formatter.print( now );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM