DateFormat.format() convert Date object into readable string to a human | Android coding


after working
After work

How to get the current date-time via Java code in Android?
Date d = new Date(System.currentTimeMillis());
/* A specific moment in time, with millisecond precision. Values typically come from currentTimeMillis(), and are always UTC, regardless of the system's time zone. This is often called "Unix time" or "epoch time" */
Also, someone uses other way instead as below:

Calendar.get(Calendar.MONTH);  // get month of today.
Calendar.get(Calendar.YEAR);   // get year of today.

Calendar.get(Calendar.SECOND); // get second of today.

Calendar.get(Calendar.MINUTE); // get minute of today.
.....

but it is not a good ideal to capture the information of Date().

Why? 
(Maybe player wants to get more point in game for cheating, and then turn the date of calendar to be earlier.... The man is I. Hahaha...!!) Because of changing the time of Calendar, It absolutely affects the data you want to get is incorrect.


However, there is a good way to be faster, and capture a correct information of Date() as sample code below:

Date d = new Date();
CharSequence updateTime = DateFormat.format("yyyy-MM-dd kk:mm:ss", d.getTime());
TextView TodayDate = (TextView)mView.findViewById(R.id.TodayDate);
TodayDate.setText(updateTime.toString());


Note!! format string, "yyyy-MM-dd kk:mm:ss", is better than use "yyyy-MM-dd HH:mm:ss". Because "HH" return error or "??" in some Android mobile device. For example, my Samsung Tab 8 showed "??" string value on screen, and I waste so much time to figure it out.  

(Damn!)

留言

這個網誌中的熱門文章

7-ELEVEN 電子發票明細查詢方式

Java 使用 regular expression 正則表達,過濾特殊字元,反斜線處理重點

Ubuntu GUI中,無法啟動WIFI連結解決方法