Formats the specified date as a string using the pattern of this date format and appends the string to the specified string buffer. If the field member of field contains a value specifying a format field, then its beginIndex and endIndex members will be updated with the position of the first occurrence of this field in the formatted text.
Parameters:
date - the date to format.
buffer - the target string buffer to append the formatted date/time to; optional field
fieldPos - on input: an optional alignment field; on output: the offsets of the alignment field in the formatted text.
Returns: the string buffer.
1. Set a Date to the Calendar object
2. Set a Pattern to SimpleDateFormat
try {
String myString = mySimpleDateFormat.format(myCalendar.getTime());
} catch IllegalArgumentException e) {
e.printStackTrace();
}
5. Compile and run!
Resources:
http://developer.android.com/reference/java/text/SimpleDateFormat.html#format(java.util.Date, java.lang.StringBuffer, java.text.FieldPosition)
4. In the MainActivity.java file, add the below line to the onCreate method. This will retrieve the date from the Calendar object and save it in a string named myString. The SimpleDateFormat object formats the myCalendar object by the calling getTime() method on the myCalendar object. This method returns a date object that the SimpleDateFormat object parses into the date pattern that was specified earlier, and then set the string result into a local variable.
try {
String myString = mySimpleDateFormat.format(myCalendar.getTime());
} catch IllegalArgumentException e) {
e.printStackTrace();
}
5. Compile and run!
Resources:
http://developer.android.com/reference/java/text/SimpleDateFormat.html#format(java.util.Date, java.lang.StringBuffer, java.text.FieldPosition)