Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction >> 1.5. Text Processing and Collections
Current Topic: 1.5.2. Formatting output
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
The simplest way to display a formatted string is to use System.printf() method instead of System.print() or System.println() methods.

The printf() method requires at least two (or more) arguments. The first argument describes the format and the next argument(s) provide one or more variables that are to be formatted.

For example:
System.out.printf("Integer:%d",23); // Integer:23 (as is)
System.out.printf("Integer : %3d",23); // Integer: 23 (padded on the left with one space)
System.out.printf("Integer : %03d",23); // Integer:023 (padded on the left with one zero)

System.out.printf("Floating point with 5 decimal: %.5f\n",1.123456789); // Floating point with 5 decimal: 1.12345 (new line)
System.out.printf("Several variables including String: %s, integer: %d, float: %.3f", "ITS, Inc.", 2015,1.123456789);
// Several variables including String: "ITS, Inc.", integer: 2015, float: 1.123

Formatting Date
There is more than one way of formatting the Date.
Here is an example of creating and formatting the current Date, which will require to import a SimpleDateFormat class from the java.text.* package

import java.text.SimpleDateFormat;

public class Stringer {
/**
* @return currentDate as "yyyy-MM-dd HH:mm"
*/
public static String getCurrentDate() {
return getCurrentDate("yyyy-MM-dd HH:mm");
}
/**
* @param format
* @return current date formatted
*/
public static String getCurrentDate(String format) {
if(format == null) {
format = "yyyy-MM-dd HH:mm"; // default
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date date = new java.util.Date(); // current Date and Time
String currentDate = sdf.format(date); // formatted Date and Time
return currentDate;
}
}
Was it clear so far?


Assignments:
1. Add the formatting methods to the Stringer class. Provide in the main() method several lines to test these methods.
2. Read more about SimpleDateFormat class and related format patterns.
3. Try different format patterns in the main() while testing the getCurrentDate() method. Keep in mind that not valid format patterns will produce exceptions
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>import java.text.SimpleDateFormat;
<br/>
<br/>   public class Stringer {
<br/>	/**
<br/>	 * @return currentDate as "yyyy-MM-dd HH:mm"
<br/>	 */
<br/>	public static String getCurrentDate() {
<br/>		return getCurrentDate("yyyy-MM-dd HH:mm");
<br/>	}
<br/>	/**
<br/>	 * @param format
<br/>	 * @return current date formatted
<br/>	 */
<br/>	public static String getCurrentDate(String format) {
<br/>		if(format == null) {
<br/>			format = "yyyy-MM-dd HH:mm"; // default
<br/>		}
<br/>		SimpleDateFormat sdf = new SimpleDateFormat(format);
<br/>		java.util.Date date = new java.util.Date(); // current Date and Time
<br/>		String currentDate = sdf.format(date); // formatted Date and Time
<br/>		return currentDate;
<br/>	}
<br/>   }
<br/>






Was it clear so far?



Assignments:
1. Add the formatting methods to the Stringer class. Provide in the main() method several lines to test these methods.
2. Read more about SimpleDateFormat class and related format patterns.
3. Try different format patterns in the main() while testing the getCurrentDate() method. Keep in mind that not valid format patterns will produce exceptions

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy