Why Calendar.day_of_week needs to be reduced in Java

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_45c06e600100pm77.html

——————————————————————————————————————————————————————————————————————

Online search, just contact Calendar class, a little complicated, write down.


The Calendar class is used for processing dates in Java, and there are several methods that need to be noticed by novice users.

1. Reason for Calendar.month + 1 when getting the month
the months in Java follow the rules in the Roman calendar: The number of months in a year is not fixed, and the first month is January. The value returned by Calendar.month in Java is actually the number of months from the first month of the current month, and January returns "0" in Java, so we need +1.

2. Reasons for the calendar.day_of_week–1 of the day of the week
In Java, Calendar.day_of_week actually says: The day of the week, so he will be affected by the days of the week .
Some regions take Sunday as the first day of the week, while some regions take Monday as the first day of the week, and these 2 cases need to be differentiated.
Look at the return value of the following table

Sunday is the first day of the week SUN MON TUE WED THU FRI Sat
Day_of_week return value 1 2 3 4 5 6 7
Monday is the first day of the week MON TUE WED THU FRI Sat SUN
Day_of_week return value 1 2 3 4 5 6 7

So calendar.day_of_week need to determine if "1" is required depending on the locale setting
Output from different locales in Java can be implemented using Locale.setdefault (locale. Region name).

3. Calendar.day_of_month does not require special action when getting the date, he returns the day of the one month directly

Why Calendar.day_of_week needs to be reduced in Java

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.