Tool class: system, runtime, math, date, and calendar

Source: Internet
Author: User
Tags time in milliseconds

API ---Java. Lang.System:Both attributes and behaviors are static.

Long currenttimemillis ();// Returns the current time in milliseconds.

Exit ();// Exit the VM

PropertiesGetproperties (); // Obtain the attribute information of the current system

 

Properties prop = system. getproperties ();// Obtain the system property information and store the information in the properties set.

 

System. setproperty ("myname", "instructor bi ");// Add specific property information to the system property information set

// Temporary setting method: when running JVM, you can use JVM parameters to set System Properties temporarily. You can add-D <name >=< value> usage: Java-Dmyname = Xiaoming class name.

 

String name = system. getproperty ("OS. Name ");// Obtain the information of the specified attribute

 

// You want to know whether the system is one of the systems supported by the software.

Set <string> HS = new hashset <string> ();

HS. Add ("Windows XP ");

HS. Add ("Windows 7 ");

If (HS. Contains (name ))

System. Out. println ("supported ");

Else

System. Out. println ("not supported ");

Bytes --------------------------------------------------------------------------------------------------------------------

API ---Java. Lang.Runtime:Class.

But there are non-static methods. The object should be defined in this class and can be obtained through a static method. Use this object to call non-static methods. This method is static runtime getruntime ();

 

This Runtime is actually designed in the singleton design mode.

 

Class runtimedemo {

Public static void main (string [] ARGs) throws exception {

Runtime r = runtime. getruntime ();

PROCESS p = R.Exec("Notepad.exe systemdemo. Java"); // run the specified program

Thread. Sleep (4000 );

P. Destroy ();// Kill the process

}

}

Bytes --------------------------------------------------------------------------------------------------------------------

API ---Java.Util.Math:Tools used for mathematical operations, attributes and behaviors are static. This class is not allowed to inherit from final.

 

Static double Ceil (double A); // returns the smallest integer greater than the specified value.

Static double floor (double A); // returns the maximum integer smaller than the specified value.

Static long round (double A); // rounding to an integer

Static double POW (double A, double B); // B power of

Static double random (); // returns 0 ~ 1 pseudo-random number

 

Public static void main (string [] ARGs ){

Random r = new random ();

For (INT x = 0; x <10; X ++ ){

// Double D = math. Floor (math. Random () * 10 + 1 );

// Int d = (INT) (math. Random () * 10 + 1 );

Int d = R. nextint (10) + 1;

System. Out. println (d );

}

}

Bytes --------------------------------------------------------------------------------------------------------------------

API ---Java.Util.Date:Date class. The month ranges from 0 to 11;

/*

The conversion between the date object and the millisecond value.

1. Convert the date object to the millisecond value. The gettime method in the date class.

2. How do I convert the obtained millisecond value to a specific date?

The settime method in the date class. You can also use constructor.

*/

// Convert the date object to a millisecond Value

Date d = new date ();

Long time1 = D. gettime ();

Long time2 = system. currenttimemillis (); // millisecond value.

 

// Convert the millisecond value to a specific date

Long time = 1322709921312l;

Date d = new date ();

D. settime (time );

/*

Convert a date string to a date object: In the dateformat methodDate parse (string source);

*/

Public static void method () throws exception {

String str_time = "2011/10/25 ";

Dateformat df = new simpledateformat ("yyyy/mm/DD ");// Simpledateformat can be used as a custom format to complete formatting.

Date d = DF. parse (str_time );

}

/*

If you do not need to use a specific formatting style, you can use the static factory method in the dateformat class to obtain the specific encapsulated style objects.Getdateinstance (); getdatetimeinstance ();

*/

Date d = new date ();

Dateformat df = dateformat. getdateinstance (dateformat. Long );

DF = dateformat. getdatetimeinstance (dateformat. Long, dateformat. Long );

String str_time = DF. Format (d );

 

// Convert a date object to a string using the format method in the dateformat class.

// Create Date Format object.

Dateformat df = new simpledateformat ();// The creation of this object encapsulates a default date format. 11-12-1 pm

// If You Want to customize the date format. You can use the simpledateformat constructor. Pass the specific format as a parameter to the constructor. How to represent the middle-aged part of the date? You may need to participate in the format object document.

DF = new simpledateformat ("mm DD, yyyy hh: mm: SS ");

// Call the format method in dateformat. Format the existing date object.

String str_time = DF. Format (d );

Bytes --------------------------------------------------------------------------------------------------------------------

API ---Java.Util.Calendar:Calendar

Public static void method (){

Calendar c = calendar. getinstance ();

System. Out. println (C. Get (calendar. Year) + "year" + (C. Get (calendar. month) + 1) + "month"

+ Getnum (C. Get (calendar. day_of_month) + "day"

+ "Week" + getweek (C. Get (calendar. day_of_week )));

}

Public static string getnum (INT num ){

Return num> 9? Num + "": "0" + num;

}

Public static string getweek (INT index ){

/*

Table query: establishes a data correspondence.

Best: the number of data is determined and has a corresponding relationship. If one of the mappings is a number and can be used as the badge, an array can be used as a table.

*/

String [] weeks = {"", "day", "one", "two", "three", "four", "five", "Six "};

Return weeks [Index];

}

Tool class: system, runtime, math, date, and calendar

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.