Based on the basic concepts of Android for some time created
Get the current time
method one:
= DATE DATE new new DATE ();
Log.e (the TAG, "current time =" + date);
result:
E / TimeActivity: current time = Wed Sep 12 07:03:25 GMT + 00: 00 2018
Second way:
DATE = System.currentTimeMillis Long (); // get the system time is a value obtained from the present to 1970 milliseconds Long
Log.e (the TAG, "millisecond time value =:" + date);
result:
E / TimeActivity: millisecond time = value: 1536736055737
Method three:
Calendar calendar = Calendar.getInstance();
calendar.getTimeInMillis();
Time acquisition device is turned on at this time to the opposite
long time = SystemClock.elapsedRealtime();
After obtaining sleep to wake up at this time of relative time
SystemClock.uptimeMillis();
SimpleDateFormat format time
Formatting alphabet
Symbol | Meaning | Presentation | Example |
---|---|---|---|
G | era designator era | Text | AD |
Y | year in | Number | 2009 |
M | month in year 月 | Text & Number | July & 07 |
d | day in month 日 | Number | 10 |
h | hour in am / pm (1-12) during the 12-hour | Number | 12 |
H | hour in day (0-23) during the 24-hour | Number | 0 |
m | minute in hour minutes | Number | 30 |
s | second in minute 秒 | Number | 55 |
S | millisecond ms | Number | 978 |
E | day in week week | Text | Tuesday |
D | day in year in the first few days of the year | Number | 189 |
F | day of week in month the first few weeks of the month of the week | Number | 2 (2nd Wed in July) |
w | week in year in the first few weeks of the year | Number | 27 |
W | week in month in the first few weeks of January | Number | 2 |
a | am / pm marker AM / PM | Text | PM |
k | hour in day (1-24) the first few hours of the day | Number | 24 |
K | hour in am/pm (0-11) | Number | 0 |
with | time zone time zone | Text | Pacific Standard Time |
' | escape for text | Delimiter | (none) |
' | single quote | Literal | ' |
Formatting out all the time:
Time = Long System.currentTimeMillis ();
// format day - month (M is equal to three months in text form) - in hours: minutes: seconds: milliseconds the SimpleDateFormat Formatter = new new the SimpleDateFormat ( "dd-MMM-YYYY HH: mm : SS: the SSS " ); String timeText = formatter.format (time); Log.e (the TAG," value after formatting time =: "+ timeText);
result:
E / TimeActivity: formatted time value =: 12-Sep-2018 07: 22: 38: 596
Only when formatting - minutes - seconds - milliseconds:
Formatter = the SimpleDateFormat new new the SimpleDateFormat ( "SS-HH-mm-the SSS"); // delimiter can set their own
Log.e (TAG, "value after formatting time =:" + formatter.format (System.currentTimeMillis ()));
result:
E / TimeActivity: value = formatted time: 07-29-18-317
Formatting a month for the first few weeks:
Formatter = the SimpleDateFormat new new the SimpleDateFormat ( "MW"); // month - the week of the month
// if "MM-WW" format may be a 09-03 Log.e (the TAG, "time value formatted =: "+ formatter.format (System.currentTimeMillis ( )));
result:
E / TimeActivity: = formatted time value: 9-3
Calendar Calendar class
Example One:
= the mFormat new new the SimpleDateFormat ( "the MM-YYYY-dd: mm-HH-SS" );
Calendar Calendar Calendar.getInstance = (); // get a single embodiment calendar.set (Calendar.YEAR, 2018); // in calendar. SET (the Calendar.MONTH, 8); // month month note is counted from 0, so here 8, after formatting equal calendar.set. 9 (Calendar.DAY_OF_MONTH, 12 is); // day calendar.set ( Calendar.HOUR_OF_DAY, 15); // the HOUR_OF_DAY 24 hours calendar.set (Calendar.MINUTE, 52) made of; // sub calendar.set (Calendar.SECOND, 20 is); // second Log.e (TAG, " unformatted millisecond time = "+ calendar.getTimeInMillis ()); Log.e (the TAG," manual formatting time = "+mFormat.format (calendar.getTimeInMillis ())); Log.e (TAG, "the system comes formatted time =" + calendar.getTime ());
effect:
E / TimeActivity: unformatted millisecond = 1536767540133 Time
E / TimeActivity: manual formatting = 2018-09-12 Time: 15-52-20
E / TimeActivity: formatting system comes time = Wed Sep 12 15:52: 20 GMT + 00: 00 2018
Example Two:
calendar.set (2017,9,13,14,24,30 );
Log.e (the TAG, "another way input mode time =" + mFormat.format (calendar.getTimeInMillis ( )));
effect:
E / TimeActivity: Another way input mode time = 2017-10-13: 14-24-30
Example Three:
calendar.set (the Calendar.AM_PM, 0); // 0 = 1 = AM PM
calendar.set (Calendar.HOUR,. 6); // 12 is the hour Log.e (TAG, "AM = Time" + mFormat.format (calendar.getTimeInMillis ())); calendar.set (the Calendar.AM_PM,. 1 ); calendar.set (Calendar.HOUR,. 7 ); Log.e (the TAG, "afternoon time =" + mFormat.format (calendar.getTimeInMillis ()));
effect:
E / TimeActivity: AM = 2017-10-13 Time: 06-24-30
E / TimeActivity: Afternoon time = 2017-10-13: 19-24-30
Jump up or down using the Calendar year / month / day / hour / minute / second
The handover date is still quite simple just need to switch the current timestamp minus 24 hours a timestamp, year or month is the need for fairly complex calculations necessary to calculate the size of the Month / flat leap year. Fortunately, Calendar class has helped us to achieve this method
add () method:
Explain add () method using add () method switching time, the time will affect the other regions (Increase Decrease the date or the month of the year will also change), for example: 2018-11-31 (here equal to 11 12), we switched to one extra day will become 2019-0-1
= Calendar Calendar Calendar.getInstance ();
Calendar.add (Calendar.YEAR, -1); // decrease year
Calendar.add (Calendar.YEAR,. 1); // increase year calendar.add (Calendar.MONTH, -1); // decrease month Calendar.add (the Calendar.MONTH,. 1); // additional month Calendar.add (Calendar.DAY_OF_MONTH, -1); // decrease one day calendar.add (Calendar.DAY_OF_MONTH, 1 ); // add a day
roll () method:
roll () method coincided with the add () Instead, use the roll () method switching time, the time will not affect other areas, such as: 2018-11-31, we switch to one extra day will become 2018-11-1
= Calendar Calendar Calendar.getInstance ();
calendar.roll (Calendar.YEAR, -1); // decrease year
calendar.roll (Calendar.YEAR,. 1); // increase year calendar.roll (Calendar.MONTH, -1); // decrease month calendar.roll (the Calendar.MONTH,. 1); // additional month calendar.roll (Calendar.DAY_OF_MONTH, -2); // decrease two days calendar.roll (Calendar.DAY_OF_MONTH, 2); // increase two days // course, you can use Boolean value calendar.roll (Calendar.YEAR, to true); // decrease year calendar.roll (Calendar.YEAR, false); // additional year calendar. roll (of the Calendar.MONTH, to true); // reduce month calendar.roll (Calendar.MONTH,false); // additional month calendar.roll (Calendar.DAY_OF_MONTH, to true); // reduce one day calendar.roll (Calendar.DAY_OF_MONTH, false); // add a day
Use the time to get Calendar
CAL = Calendar Calendar.getInstance ();
// current year
int year = cal.get (Calendar.YEAR); Log.e (TAG, "the current year:" + year); // current month int month = (cal. GET (of the Calendar.MONTH)) + 1 ; Log.e (TAG, "the current month:" + month the); // the first few days of the current month: the current day int DAY_OF_MONTH = cal.get (Calendar.DAY_OF_MONTH); the Log .e (TAG, "current month:" + DAY_OF_MONTH); // current time: HOUR_OF_DAY-24 hour; hOUR-12 hour int hour = cal.get (Calendar.HOUR_OF_DAY); Log.e (the TAG, "current when: "+ hour); // this sub- int = minute cal.get (Calendar.MINUTE); Log.e (the TAG," current point: "+minute); // this second int = SECOND cal.get (Calendar.SECOND); Log.e (the TAG, "Current seconds:" + SECOND); // current millisecond int millisecond = Calendar.get (Calendar.MILLISECOND); Log.e (TAG, "current ms:" + millisecond); // 0- am; afternoon 1- int = AMPM cal.get (the Calendar.AM_PM); Log.e (TAG, "aM / PM:" + AMPM ); // the first few weeks of the current year int WEEK_OF_YEAR = cal.get (Calendar.WEEK_OF_YEAR); Log.e (TAG, "the first few weeks of the current year:" + WEEK_OF_YEAR); // the first few weeks of the current month int = WEEK_OF_MONTH cal.get (Calendar.WEEK_OF_MONTH); Log.e (TAG, "the first few weeks of the current month:" + WEEK_OF_MONTH);// The first few days of the current year int day_of_year = cal.get (Calendar.DAY_OF_YEAR); Log.e (TAG, "the first few days of the current year:" + day_of_year);
Also explain why int month The = (cal.get (of the Calendar.MONTH)) + 1 ; need to add +1, because the acquisition of the month is starting from 0