【问题标题】:How to show current date on a jframe title bar?如何在 jframe 标题栏上显示当前日期?
【发布时间】:2014-07-06 13:07:45
【问题描述】:

我搜索了解决方案,但找不到。我想在我的 jframe 标题栏上显示当前日期。我知道如何使用 java 获取当前日期,但我需要在标题栏上显示它。谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

  • 我知道如何使用 java 获取当前日期。我的问题是我需要在标题栏上显示它。
  • @Pappu 我不相信您“寻找解决方案”并且找不到更改 JFrame 标题的方法。
  • @DSquare 我知道如何更改它,但只能使用 netbeans 中的属性选项。

标签: java swing jframe titlebar


【解决方案1】:

获取当前日期> 使用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));

【讨论】:

  • 我在你发布之前得到了解决方案:)
【解决方案2】:

answer by 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" ) );

使用本地化格式生成日期时间值的字符串表示形式。使用 Locale 对象和 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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    相关资源
    最近更新 更多