Java calendar, get month less January issues and other considerations
Java calendar, getting month less January issues and other considerations: code block
Import java.util.*;
public class Dateclass
{public
static void Main (String args[])
{
date m = new Date ();
System.out.println ("Now Is:" +m.tostring ());
Calendar n = calendar.getinstance ();
int year = N.get (calendar.year);
int month = N.get (calendar.month) +1;
int day = N.get (calendar.date);
Int week = N.get (Calendar.day_of_week)-1;
Date t = n.gettime ();
String week1 = new String ("Day 123456");
String week2=week1.substring (week, week+1);
string x = new string ();
x = "Now is:" +year+ "year" +month+ "month" +day+ "Day, Week" +WEEK2;
System.out.println ("Now is:" +t);
SYSTEM.OUT.PRINTLN (x);
}
}
the reason for month plus 1:
The public static final int MONTH The field numbers that indicate the get and set of the month. This is a calendar-specific value. In the Gregorian calendar and the Julian calendar, the first month of the year is January, which is 0; the last one months depend on the number of months of the year. In short, because the initial value of this value is 0, we need to add 1 when we use it to represent the correct month.
the reason for week minus 1:
public static final int day_of_week The field number of get and set, indicating the day of the one week. The preferred values for this field are SUNDAY, MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, and SATURDAY observation code, and you will find that week is used to determine the subsequent string week1 how to intercept, we know Day_ Of_week is to get the current date is the first day of the week, and a week from Sunday, so when we get this value, to properly intercept the character in the subsequent string week1, it needs to be reduced to achieve the goal. You just keep track of the program and the assignment of the variable will understand how it's written.
The reason that
does not use when defining day:
public static final int DATE get and set's field numbers, which indicate one of the days of the one month. It is synonymous with day_of_month. The value of the first day of the one month is 1. By definition, we can know that this value is not required to make any changes to the value of the program. substring (week, week+1): The
public string substring (int beginindex, int endindex) returns a new string, which is a substring of this string. The substring starts at the specified beginindex until the character at the index endIndex-1. Therefore, the length of the substring is endindex-beginindex. By definition, we know the purpose of this method. For example, if today is Thursday, then the value of the previous WEEK is 4, because we took the Day_of_week (5) and then lost 1, and then we used this variable WEEK to help us intercept the string week1, which called substring (4,5), So the character that is taken is "four, exactly the character we need."