Convert time in long to specific date format – SimpleDateFormat in action!

Here is that pretty snippet for converting long to specific date format:

public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}

I took source from: http://stackoverflow.com/a/7954038/619673

And here is pattern structure for formatting:

Symbol Meaning Kind Example
D day in year (Number) 189
E day of week (Text) E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
F day of week in month (Number) 2 (2nd Wed in July)
G era designator (Text) AD
H hour in day (0-23) (Number) 0
K hour in am/pm (0-11) (Number) 0
L stand-alone month (Text) L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
M month in year (Text) M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
S fractional seconds (Number) 978
W week in month (Number) 2
Z time zone (RFC 822) (Time Zone) Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
a am/pm marker (Text) PM
c stand-alone day of week (Text) c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
d day in month (Number) 10
h hour in am/pm (1-12) (Number) 12
k hour in day (1-24) (Number) 24
m minute in hour (Number) 30
s second in minute (Number) 55
w week in year (Number) 27
y year (Number) yy:10 y/yyy/yyyy:2010
z time zone (Time Zone) z/zz/zzz:PST zzzz:Pacific Standard Time
' escape for text (Delimiter) 'Date=':Date=
'' single quote (Literal) 'o''clock':o’clock

I took source from: http://developer.android.com/reference/java/text/SimpleDateFormat.html

Leave a Reply

Your email address will not be published. Required fields are marked *