Java Basic Learning final keyword, traversal collection, use of Date class objects, use of the Math class object, runtime class object usage, Time object date (two date subtraction)

Source: Internet
Author: User
Tags date1 dateformat

1.final keywords
As with the const keyword in. NET, it is the modifier of a constant, but final can also modify the class, method.
Specification: Constant All letters are capitalized, the middle of multiple words with "_" connection.

2. Iterating through the collection
arraylist<integer> list = new arraylist<integer> ();
List.add (1);
List.add (3);
List.add (5);
List.add (7);
Traverse List Method 1, using the normal for loop:
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));
List.remove (i);//using this traversal to delete elements can be problematic. List.size () will change after deletion
}
Traverse List Method 2, using the enhanced for loop (the generic definition type should be used!). ):
for (int temp:list) {
SYSTEM.OUT.PRINTLN (temp);
}
To Traverse list Method 3, use the iterator iterator:
for (Iterator iter = List.iterator (); Iter.hasnext ();) {
System.out.println (Iter.next ());
}
How to traverse the Delete collection using the following methods
Iterator Iter=list.iterator ();
while (Iter.hasnext ()) {
Object Obj=iter.next ();
Iter.remove ();//If you want to traverse the deletion of elements in a collection, this is the recommended way!
System.out.println (obj);
}

3. Use of Date class objects

Get current time
Calendar C = calendar.getinstance ();
C.set (2014, 2, 1);//change of time, date specified
int year = C.get (calendar.year);
int month = C.get (calendar.month) +1;
int date = C.get (calendar.date);
int hour = C.get (Calendar.hour_of_day);
int minute = C.get (Calendar.minute);
int second = C.get (Calendar.second);
Int week = C.get (Calendar.day_of_week);
String[] weeks = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
SYSTEM.OUT.PRINTLN (Year + "/" + month + "/" + Date + "" +hour + ":" +minute + ":" + second+ "" +weeks[week]);

Specify the format of the current time
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
System.out.println (Df.format (new Date ()));//new Date () to get the current system time

Use of 4.Math class objects
/*
* Math: Provides methods for manipulating mathematical operations, all static.
* Common methods:
* Ceil (): Returns the smallest integer greater than the parameter.
* Floor (): Returns the largest integer less than the parameter.
* ROUND (): Returns the rounded integer.
* POW (A, B): A is the second party.
*/
for (int i = 0; i < i++) {
//
int d = (int) (Math.random () * 10 + 1);//Generate 1-10 random number
//
int d= (int) Math.ceil (12.23);
// //
Double d = Math.ceil (12.31);
// //
Double D=math.floor (12.36);
// //
Long D=math.round (63.50);
Double D=math.pow (2, 3);

System.out.println (d);

}

Use of 5.Runtime class objects
/*
* Runtime: There is no constructor summary, stating that the class is not available to create objects.
* Also found that there are non-static methods. Indicates that the class should provide a static method to return the class object.
* And only one, indicating that the runtime class uses a singleton design pattern.
*
*/
Runtime r = Runtime.getruntime ();
//
Execute: Execute. Xxx.exe
Process p = r.exec ("notepad.exe");
Thread.Sleep (5000);
P.destroy ();

6. Time Object Date (two date subtraction)
String str_date1 = "2012-3-17";
String str_date2 = "2012-4-18";
DateFormat DateFormat = Dateformat.getdateinstance ();
DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
Date date1 = Dateformat.parse (str_date1);
Date date2 = Dateformat.parse (STR_DATE2);
Long time1 = Date1.gettime ();
Long time2 = Date2.gettime ();
Long time = Math.Abs (time1-time2);//The number of milliseconds to get two time difference
int day = (int) (TIME/1000/60/60/24);
System.out.println (day);//32 Days Difference

Java Basic Learning final keyword, traversal collection, use of Date class objects, use of the Math class object, runtime class object usage, Time object date (two date subtraction)

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.