shubhendu mahajan
shubhendu mahajan

Reputation: 816

How can i perform the following conversion?

Formatter fmt=new Formatter();
Calendar cal=Calendar.getInstance();
fmt.format("%tc",cal);
System.out.print(fmt);

// Instead of System.out.print(fmt); i want to print the value of fmt on JTextArea.But as we know JTextArea accepts only String value.So how can i convert Formatter fmt into equivalent String value.

Upvotes: 3

Views: 107

Answers (3)

Philipp Reichart
Philipp Reichart

Reputation: 20961

Check out Formatter.toString():

Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
yourTextarea.setText(fmt.format("%tc",cal).toString());

Less code if you use String.format() which creates a Formatter internally:

Calendar cal = Calendar.getInstance();
yourTextarea.setText(String.format("%tc", cal));

If you plan on doing this multiple times, you also might want to use append() instead of setText() as not to replace any previous content in your JTextArea, probably appending the value of System.getProperty("line.separator") to get the leading line break like println() does.

Upvotes: 3

Philipp Wendler
Philipp Wendler

Reputation: 11433

Just call fmt.toString() which will give you a regular String (System.out.print does the same thing internally).

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

textField.setText(fmt.toString());

Upvotes: 3

Related Questions