Java: int month = n. Get (calendar. month) + 1; why add 1?

Source: Internet
Author: User
1 import java. util .*;
2 public class dateclass
3 {
4 public static void main (string ARGs [])
5 {
6 date M = new date ();
7 system. Out. println ("now:" + M. tostring ());
8 Calendar n = calendar. getinstance ();
9 int year = n. Get (calendar. year );
10 int month = n. Get (calendar. month) + 1;
11 int day = n. Get (calendar. date );
12 INT week = n. Get (calendar. day_of_week)-1;
13 Date t = n. gettime ();
14 string week1 = new string ("May 25, 1234 ");
15 string week2 = week1.substring (Week, week + 1 );
16 string x = new string ();
17 x = "now:" + year + "year" + month + "month" + day + "day, week" + week2;
18 system. Out. println ("now:" + t );
19 system. Out. println (X );
20}
21}

Resolution:
1 month plus 1 reason: public static final int month indicates the get and set Field Numbers of the month. This is a calendar-specific value. The first month of a year in the gregalli and Rochelle is January, which is 0; the last month depends on the number of months in a year. Simply put, because the initial value of this value is 0, we need to add 1 to indicate the correct month.
2. Reasons for reducing week by 1: Public static final int day_of_week get and set field numbers, indicating the day of the week. The valid values of this field are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday. You will find that week is used to determine how to intercept the subsequent string week1, we know that day_of_week is the day of the week to get the current date, and the week starts from Sunday, so when we get this value, to correctly extract the Chinese character in the subsequent string week1, it needs to be reduced by 1 for the purpose. You just need to trace the program running and observe the variable assignment to understand the intention of writing it like this.
3. Reasons for not using day: public static final int date get and set field numbers indicate the day of the month. It is synonymous with day_of_month. The value of the first day of a month is 1. According to the definition, we can know that after this value is obtained, no need to make any changes is the value required by the program.
4 substring (Week, week + 1): Public string substring (INT beginindex, int endindex) returns a new string, which is a substring of this string. This substring starts from the specified beginindex until the character at the index endindex-1. Therefore, the length of the substring is endindex-beginindex. According to the definition, we will know the purpose of this method. For example, if today is Thursday, the previous week value is 4, because we get day_of_week (5 here) And then subtract 1, then we use this variable week to help us intercept the week1 string. Here we call substring (), so the obtained character is "4, which is exactly the character we need.
Related Article

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.