Home > Back-end >  Java review chapter 6
Java review chapter 6

Time:04-28

Calendar
 
/* *
Java. Util. Calendar Calendar class: class
We can use the method to get in the Calendar class Calendar fields (year, month, day,, points, SEC... )
We can use the Calendar in the class that the value of the method to modify Calendar - 2019 & gt; In November 2029 - & gt; February
We can use the Calendar class methods in the Calendar of the value of the field increase/decrease in November 2019-5=2014 + 3 month
=2 monthWe can use the method in the Calendar class object is converted to a Calendar Date object
Calendar is an abstract class to create the object directly, in the Calendar class has a static method, returned to the Calendar class a subclass of object
The static Calendar getInstance ()
Using the default time zone and language environment for a calendar,
Note:
In the west: 0 - November
In the east: 1-12 month
West week: Sunday - Saturday
The east week: Monday - Sunday
*/
Public class Demo01Calendar {
/* *
The source of the getInstance method:
Calendar CAL=null;
CAL=new GregorianCalendar (zone, aLocale);
*/
C=Calendar Calendar. GetInstance ();
System.out.println(c);

}

Output:
Java. Util. GregorianCalendar [time=1619535222065, areFieldsSet=true, areAllFieldsSet=true, lenient=true, zone=sun. Util. Calendar. ZoneInfo [id="Asia/Shanghai", offset=28800000, dstSavings=0, useDaylight=false, transitions=29, lastRule=null], firstDayOfWeek=1, minimalDaysInFirstWeek=1, ERA=1, YEAR=2021, the MONTH=3, WEEK_OF_YEAR=18, WEEK_OF_MONTH=5, DAY_OF_MONTH=27, DAY_OF_YEAR=117, DAY_OF_WEEK=3, DAY_OF_WEEK_IN_MONTH=4, AM_PM=1, HOUR=10, HOUR_OF_DAY=22, MINUTE=53, SECOND=42, MILLISECOND=65, ZONE_OFFSET=28800000, DST_OFFSET=0]

2. The Calendar class methods
 
/* *
Common members of the Calendar class methods:
Int the get (int field) to obtain the value of the specified calendar
Void the set (int field, int value) sets the given calendar field to the given value,
Void the add (int field, int amount) the calendar field to increase or reduce the value of the specified
The Date getTime () the calendar object is converted to a Date object
Note:
To the above method parameter int field, let's pass the Calendar fields, these calendars field in the Calendar class is defined to constant
Public final static int YEAR=1;
Public final static int the MONTH=2;
Public final static int the DATE=5;
Public final static int DAY_OF_MONTH=5;
Public final static int HOUR=10;
Public final static int MINUTE=12;
Public final static int SECOND=13;
Public final static int MILLISECOND=14;
*/
Public class Demo02Calendar {
Public static void main (String [] args) {
//get the Calendar class a subclass of object
C=Calendar Calendar. GetInstance ();
Show04 (c);
}

/* *
The Date getTime () the calendar object is converted to a Date object
*/
Private static void show04 Calendar (c) {
The Date the Date be sad etTime chtistina georgina rossetti.british poetess=();
System. The out. Println (date);//Tue Apr 27 23:19:10 CST 2021
}

/* *
Void the add (int field, int amount) the calendar field to increase or reduce the value of the specified
Parameters:
Int field: the specified field
Int amount: add | to field reduced the value of the
Increases the transfer positive
Deliver negative: reduce
*/
Private static void show03 Calendar (c) {
//the year to 5 years
C.a. dd (Calendar YEAR, 5);
//the + 4 months
C.a. dd (Calendar MONTH, 4);
//the day - 10
C.a. dd (Calendar DATE, - 10);

Be sad et chtistina georgina rossetti.british poetess int year=(Calendar year);
System. The out. Println (year);//2016 2017
Be sad et chtistina georgina rossetti.british poetess int the month=(. Calendar month);
System. The out. Println (month);//8
Be sad et chtistina georgina rossetti.british poetess int the date=(Calendar. The date);
System. The out. Println (date);//17
}

/* *
Void the set (int field, int value) sets the given calendar field to the given value,
Parameters:
Int field: the specified field
The int value: the value of the specified
Set the date at the same time, it is recommended to use the following methods:
During void set (int, int the month, int the date) set calendar fields year, month and DAY_OF_MONTH value,
*/
Private static void show02 Calendar (c) {
//the set to 8888
C.s. et (Calendar YEAR, 8888);
//set the month for August
C.s. et (Calendar MONTH, 8);
//set the day to 8,
C.s. et (Calendar. The DATE, 8);
//at the same time, set the date for the 9999-9-9
C.s. et,9,9 (9999);

Be sad et chtistina georgina rossetti.british poetess int year=(Calendar year);
System. The out. Println (year);
Be sad et chtistina georgina rossetti.british poetess int the month=(. Calendar month);
System. The out. Println (month);
Be sad et chtistina georgina rossetti.british poetess int the date=(Calendar. The date);
System. The out. Println (date);
}

/* *
Int the get (int field) to obtain the value of the specified calendar
Parameters:
Int field: pass the calendar field
*/
Private static void show01 Calendar (c) {
//get in
Be sad et chtistina georgina rossetti.british poetess int year=(Calendar year);
System. The out. Println (year + "year");
//get month
Be sad et chtistina georgina rossetti.british poetess int the month=(. Calendar month);
System. The out. Println ((the month + 1) + "month");
//get day
//int the date be sad et chtistina georgina rossetti.british poetess=(Calendar. The date);
//int the date be sad et chtistina georgina rossetti.british poetess=(Calendar. DAY_OF_MONTH);
Be sad et chtistina georgina rossetti.british poetess int the date=(5);
System. The out. Println (date + ", ");
}
}


3. Marh class
 
/* *
Java. Lang. Math classes: mathematical tools
Math class contains methods used to perform basic mathematical operations, the same index, such as log, square root, and trigonometric functions,
Methods are static methods in the Math class through the class name. The method name (parameters) can be used directly
Private constructor, don't let us create the object method is called
Member methods:
Static int abs (int a/double d) return int/double value of absolute value,
Take up the whole static double ceil (double a)
The static double floor (double) a rounded down
Static long round round (double a)
The static double Max (double a, double b) return to a maximum of two decimal
Static int Max (int a, int b) return to a maximum of two integers
The static double min (double a, double b) returns two decimal minimum
Static int min (int a, int b) returns the smallest two integer
The static double pow (double a, double b) a b
The static double SQRT (double a) calculate the square root
The static double random () returns a random decimal, range [0.0-1.0)

Alt + left mouse button, can operate on column
*/
Public class Demo01Math {
Public static void main (String [] args) {
//head atic int abs (int a/double d) return int/double value of absolute value,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related