Showing posts with label SimpleDateFormat. Show all posts
Showing posts with label SimpleDateFormat. Show all posts

SimpleDateFormat.format() - Format Calendar Date using SimpleDateFormat

public StringBuffer format (Date date, StringBuffer buffer, FieldPosition fieldPos) was added in API level 1
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

3. Create and Set String named myString, set to "". 

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)

SimpleDateFormat() - How to Set a Pattern to SimpleDateFormat

public SimpleDateFormat (String pattern) was added in API level 1
Constructs a new SimpleDateFormat using the specified non-localized pattern and the DateFormatSymbols andCalendar for the user's default locale. See "Be wary of the default locale".
Parameters: pattern - the pattern.

1. Declare a SimpleDateFormat variable

2. Add the below line in the onCreate method. This will create the format of a 4-digit year, number month and the number day of the month.

try { 
    mySimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
} catch (NullPointerException e) {
    e.printStackTrace();

} catch (IllegalArgumentException e) {
    e.printStackTrace();
}

3. Compile and run!

4. Other symbols you could use for the format are:

SymbolMeaningKindExample
Dday in year(Number)189
Eday of week(Text)E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
Fday of week in month(Number)2 (2nd Wed in July)
Gera designator(Text)AD
Hhour in day (0-23)(Number)0
Khour in am/pm (0-11)(Number)0
Lstand-alone month(Text)L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
Mmonth in year(Text)M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
Sfractional seconds(Number)978
Wweek in month(Number)2
Ztime zone (RFC 822)(Time Zone)Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
aam/pm marker(Text)PM
cstand-alone day of week(Text)c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
dday in month(Number)10
hhour in am/pm (1-12)(Number)12
khour in day (1-24)(Number)24
mminute in hour(Number)30
ssecond in minute(Number)55
wweek in year(Number)27
yyear(Number)yy:10 y/yyy/yyyy:2010
ztime zone(Time Zone)z/zz/zzz:PST zzzz:Pacific Standard Time
'escape for text(Delimiter)'Date=':Date=
''single quote(Literal)'o''clock':o'clock

Resources:
http://developer.android.com/reference/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String)

How to Declare a SimpleDateFormat variable

SimpleDateFormat was added in API Level 1
Formats and parses dates in a locale-sensitive manner. Formatting turns a Date into a String, and parsing turns a String into a Date.

1. In the MainActivity.java file, add the below line to the imports section.

import java.text.SimpleDateFormat; 

2. Add the below line to the onCreate method.

SimpleDateFormat mySimpleDateFormat;

3. Compile and run!

Next Recommended Article: SimpleDateFormat() - How to Set a Pattern to SimpleDateFormat

Resources:
http://developer.android.com/reference/java/text/SimpleDateFormat.html