简体   繁体   English

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

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

I searched for the solution but couldn't find one. 我搜索了解决方案,但找不到。 I want to show the current date on my jframe title bar. 我想在我的jframe标题栏上显示当前日期。 I know how to get current date using java but I need to show it on the title bar. 我知道如何使用Java获取当前日期,但需要在标题栏上显示它。 Can anyone help me regarding this ? 有人可以帮我吗?

Thanks in advance. 提前致谢。

To get current date> use java.util.Date 要获取当前日期>,请使用java.util.Date

To format the date> use java.text.SimpleDateFormat 要格式化日期>,请使用java.text.SimpleDateFormat

To set title for JFRame> 为JFRame设置标题>

JFrame jFrame= new JFrame("title");

or 要么

jFrame.setTitle("title");

So the solution is, 所以解决方案是

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

The answer by Nidheesh is correct. Nidheesh回答是正确的。

Localizing With Joda-Time 使用Joda-Time进行本地化

If you want a localized format for the date, use the Joda-Time library. 如果您想要日期的本地化格式,请使用Joda-Time库。

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

Or, if you have a java.util.Date to convert to Joda-Time. 或者,如果您有java.util.Date可以转换为Joda-Time。

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

Generate a string representation of the date-time value using a localized format. 使用本地化格式生成日期时间值的字符串表示形式。 Use a Locale object with the Joda-Time DateTimeFormat.forStyle . 语言环境对象与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