DateFormat.format() convert Date object into readable string to a human | Android coding
After work |
How to get the current date-time via Java code in Android?
Date d = new Date(System.currentTimeMillis());Also, someone uses other way instead as below:
/* 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" */
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!)
留言
張貼留言