Calendar is the Android development needs to acquire time is necessary for a tool class, through this class can get the time information is very rich, the following to do a summary, the use of the future will not always go to the book or check information.
Get an instance of the Calendar class before you get the time:
[Java]View PlainCopy
- Private Calendar C = calendar.getinstance ();
Then you can use this variable to get the time.
Get the current year
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "Current year:" + C.get (calendar.year));
Get the current month
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "Current Month:" + (C.get (calendar.month) + 1));
This special note that the month in the calendar is starting from 0, that is, the value of January is 0, so need to add 1 is the actual month
Get today is the day of the month, there are two ways of writing, the result is the same
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "DATE:" + c.get (calendar.date));
- LOG.I ("Mainactivityfilter", "Day_of_month:" + c.get (calendar.day_of_month));
Get today is the first day of the year
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "Today is this year's first" + C.get (calendar.day_of_year) + "Day");
Get today in the first few weeks of the month
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "today in this month's first" + C.get (calendar.day_of_week_in_month) + "Week");
Get today is the day of the week, here can not directly use the return value as a week, but should be used with the calendar for the week to represent the constants used with, the return value of 3 does not mean today is Wednesday
[Java]View PlainCopy
- Private void Printdayofweek () {
- switch (c.get (Calendar.day_of_week)) {
- Case Calendar.sunday:
- LOG.I ("Mainactivityfilter", "Today is Sunday");
- Break ;
- Case Calendar.monday:
- LOG.I ("Mainactivityfilter", "Today is Monday");
- Break ;
- Case Calendar.tuesday:
- LOG.I ("Mainactivityfilter", "Today is Tuesday");
- Break ;
- Case Calendar.wednesday:
- LOG.I ("Mainactivityfilter", "Today is Wednesday");
- Break ;
- Case Calendar.thursday:
- LOG.I ("Mainactivityfilter", "Today is Thursday");
- Break ;
- Case Calendar.friday:
- LOG.I ("Mainactivityfilter", "Today is Friday");
- Break ;
- Case Calendar.saturday:
- LOG.I ("Mainactivityfilter", "Today is Saturday");
- Break ;
- Default:
- Break ;
- }
- }
Get today is what time 12-hour system
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.hour) + "point");
But it's actually three o'clock in the afternoon, so I need to distinguish between morning and afternoon.
[Java]View PlainCopy
- if (C.get (calendar.am_pm) = = calendar.am) {
- LOG.I ("Mainactivityfilter", "now is Morning");
- } Else {
- LOG.I ("Mainactivityfilter", "now Afternoon");
- }
Use calendar.am and calendar.pm to help determine whether it is morning or afternoon, if C.get (calendar.am_pm) Gets the value and calendar.am equals is morning, if and calendar.pm equals is the afternoon
Calendar also provides a way to get 24-hour time
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.hour_of_day) + "point");
Get now is a bit
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.minute) + "min");
Get now is a few seconds
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.second) + "seconds");
Get now is a few milliseconds
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", " Now" + c.get (calendar.millisecond) + "MS");
Although the calendar does not provide a way to display canonical format time, you can use SimpleDateFormat to output a canonical string, here is a simple demonstration of usage
[Java]View PlainCopy
- SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day");
- LOG.I ("Mainactivityfilter", Sdf.format (C.gettime ()));
A more detailed usage of SimpleDateFormat will be used in another article.
Calendar also provides a number of other methods, the next thing about the output of time is to use SimpleDateFormat to output, do not paste the output of the code.
Increase the value of a field in a date, for example, to get tomorrow's date, you need to add 1 to the day field
[Java]View PlainCopy
- C.add (Calendar.day_of_month, 1);
Plus the former
Plus after
This is convenient for programmers, not to consider the various boundaries of the problem
Compare two dates for equality
[Java]View PlainCopy
- Private Calendar C = calendar.getinstance ();
- Private Calendar D = calendar.getinstance ();
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "C and D are equal:" + c.equals (d));
- C.add (Calendar.year, 1);
- LOG.I ("Mainactivityfilter", "C and D are equal:" + c.equals (d));
Compare two dates which is more on the back.
[Java]View PlainCopy
- Private Calendar C = calendar.getinstance ();
- Private Calendar D = calendar.getinstance ();
- Private SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day hh:mm");
[Java]View PlainCopy
- D.add (Calendar.day_of_month, 1);
- LOG.I ("Mainactivityfilter", "D after C:" + D.after (c));
Similarly, there are methods that can be used to determine which of the two dates is more forward
[Java]View PlainCopy
- LOG.I ("Mainactivityfilter", "C in front of D:" + C.before (d));
Empties a field and replaces and empties all fields with the smallest value in the field and replaces it with the smallest value.
[Java]View PlainCopy
- C.clear (Calendar.month);
- D.clear ();
- LOG.I ("Mainactivityfilter", "C:" + Sdf.format (C.gettime ()));
- LOG.I ("Mainactivityfilter", "D:" + Sdf.format (D.gettime ()));
Get a deep copy of the Calendar object
[Java]View PlainCopy
- D = (Calendar) c.clone ();
- LOG.I ("Mainactivityfilter", "C:" + Sdf.format (C.gettime ()));
- LOG.I ("Mainactivityfilter", "D:" + Sdf.format (D.gettime ()));
- C.add (Calendar.year, 1);
- LOG.I ("Mainactivityfilter", "C:" + Sdf.format (C.gettime ()));
- LOG.I ("Mainactivityfilter", "D:" + Sdf.format (D.gettime ()));
Compare two calendar which in front which in the back method
If the calendar instance that calls this method is more than the calendar instance given by the argument, return 1, if you want to back some, return 1, if equal, return 0
[Java]View PlainCopy
- D = (Calendar) c.clone ();
- LOG.I ("Mainactivityfilter", "results of comparison:" + C.compareto (d));
- C.add (Calendar.year, 1);
- LOG.I ("Mainactivityfilter", "results of comparison:" + C.compareto (d));
- D.add (Calendar.year, 2);
- LOG.I ("Mainactivityfilter", "results of comparison:" + C.compareto (d));
Converts the current calendar object to a Date object
Some ways to set values for a calendar object's domain
There are also methods for setting the Date object to the Calendar object
The calendar also provides some methods for time zones, geographic aspects, and some strange ways, such as getting a calendar where the time is BC or A.D. ... These methods should be used in actual combat later, and then update this article.
Some of the code used in this article has been uploaded to GitHub, Address: Https://github.com/sysukehan/AndroidTests.Git, for Android studio projects, The Calendartest module contains the code used in this article.
Summary of usage of calendar class in Android