Summary of usage of calendar class in Android

Source: Internet
Author: User
Tags time zones

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
    1. Private Calendar C = calendar.getinstance ();

Then you can use this variable to get the time.
Get the current year

[Java]View PlainCopy
    1. LOG.I ("Mainactivityfilter", "Current year:" + C.get (calendar.year));


Get the current month

[Java]View PlainCopy
    1. 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
    1. LOG.I ("Mainactivityfilter", "DATE:" + c.get (calendar.date));
    2. LOG.I ("Mainactivityfilter", "Day_of_month:" + c.get (calendar.day_of_month));


Get today is the first day of the year

[Java]View PlainCopy
    1. 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
    1. 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
  1. Private void Printdayofweek () {
  2. switch (c.get (Calendar.day_of_week)) {
  3. Case Calendar.sunday:
  4. LOG.I ("Mainactivityfilter", "Today is Sunday");
  5. Break ;
  6. Case Calendar.monday:
  7. LOG.I ("Mainactivityfilter", "Today is Monday");
  8. Break ;
  9. Case Calendar.tuesday:
  10. LOG.I ("Mainactivityfilter", "Today is Tuesday");
  11. Break ;
  12. Case Calendar.wednesday:
  13. LOG.I ("Mainactivityfilter", "Today is Wednesday");
  14. Break ;
  15. Case Calendar.thursday:
  16. LOG.I ("Mainactivityfilter", "Today is Thursday");
  17. Break ;
  18. Case Calendar.friday:
  19. LOG.I ("Mainactivityfilter", "Today is Friday");
  20. Break ;
  21. Case Calendar.saturday:
  22. LOG.I ("Mainactivityfilter", "Today is Saturday");
  23. Break ;
  24. Default:
  25. Break ;
  26. }
  27. }


Get today is what time 12-hour system

[Java]View PlainCopy
    1. 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
    1. if (C.get (calendar.am_pm) = = calendar.am) {
    2. LOG.I ("Mainactivityfilter", "now is Morning");
    3. } Else {
    4. LOG.I ("Mainactivityfilter", "now Afternoon");
    5. }

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
    1. LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.hour_of_day) + "point");



Get now is a bit

[Java]View PlainCopy
    1. LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.minute) + "min");


Get now is a few seconds

[Java]View PlainCopy
    1. LOG.I ("Mainactivityfilter", " now is" + c.get (calendar.second) + "seconds");


Get now is a few milliseconds

[Java]View PlainCopy
    1. 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
    1. SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day");
    2. 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
    1. 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
    1. Private Calendar C = calendar.getinstance ();
    2. Private Calendar D = calendar.getinstance ();
[Java]View PlainCopy
    1. LOG.I ("Mainactivityfilter", "C and D are equal:" + c.equals (d));
    2. C.add (Calendar.year, 1);
    3. LOG.I ("Mainactivityfilter", "C and D are equal:" + c.equals (d));

Compare two dates which is more on the back.


[Java]View PlainCopy
    1. Private Calendar C = calendar.getinstance ();
    2. Private Calendar D = calendar.getinstance ();
    3. Private SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day hh:mm");
[Java]View PlainCopy
    1. D.add (Calendar.day_of_month, 1);
    2. 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
    1. 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
    1. C.clear (Calendar.month);
    2. D.clear ();
    3. LOG.I ("Mainactivityfilter", "C:" + Sdf.format (C.gettime ()));
    4. LOG.I ("Mainactivityfilter", "D:" + Sdf.format (D.gettime ()));



Get a deep copy of the Calendar object


[Java]View PlainCopy
    1. D = (Calendar) c.clone ();
    2. LOG.I ("Mainactivityfilter", "C:" + Sdf.format (C.gettime ()));
    3. LOG.I ("Mainactivityfilter", "D:" + Sdf.format (D.gettime ()));
    4. C.add (Calendar.year, 1);
    5. LOG.I ("Mainactivityfilter", "C:" + Sdf.format (C.gettime ()));
    6. 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
    1. D = (Calendar) c.clone ();
    2. LOG.I ("Mainactivityfilter", "results of comparison:" + C.compareto (d));
    3. C.add (Calendar.year, 1);
    4. LOG.I ("Mainactivityfilter", "results of comparison:" + C.compareto (d));
    5. D.add (Calendar.year, 2);
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.