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
<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?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=49&intro=general&group=aitu&ur=f'">
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