简体   繁体   中英

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. I know how to get current date using java but I need to show it on the title bar. Can anyone help me regarding this ?

Thanks in advance.

To get current date> use java.util.Date

To format the date> use java.text.SimpleDateFormat

To set title for 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.

Localizing With Joda-Time

If you want a localized format for the date, use the Joda-Time library.

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

Or, if you have a java.util.Date to convert to 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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