Dark Horse programmer--23, Properties,runtime,date class, Math class,

Source: Internet
Author: User
Tags time and seconds

------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Dark Horse programmer--23, Properties,runtime,date class, Math class,

/*

Properties is a subclass of Hashtable and a subclass of the map collection.

It stores a string, without a generic definition.

The properties function is used to obtain system information

*/

Import java.util.*;class sys{public static void Main (string[] args) {Properties                   P=system.getproperties ();                   Gets all the property information for (object Obj:p.keyset ())///Here P.keyset () returns an object of Type objects                     {String s = (string) p.get (obj);                   Extract the value corresponding to the key, and then force the transformation of the SOC (obj+ "---" +s);                     }//Get specific information String uc= system.getproperty ("User.country");                    Soc ("user.country=" +UC);                                             String uc2= system.getproperty ("Tashi"); Soc ("tashi=" +uc2);//print is tashi=null/* Here Special Tip: Search system Information there is no corresponding value will be returned                     Back to NULL, however, when you javac the compile command in the DOS command line and then prepare to run the command, you can write the Run command: java-dtashi=45 Sys Press ENTER again, you can see the print is tashi=45 this way is at run timeSet System Properties. General format: java-d< name >=< value > < run file name > *//Set System Information system.se                     Tproperty ("of the", "meaning");                     String uc3= System.getproperty ("of the");                                                   SOC (The---of the "+uc3");                                             } public static void Soc (Object obj) {System.out.println (obj); }}

—————— Split Line ——————

/*

Introduction to the Runtime class:

The runtime class is in the Java.lang package.

There is no constructor, so an external object cannot be created.

There are both static and non-static methods in this class.

This class provides methods to obtain this class object and the method is static,

The return type is of this class type.

This is also a singleton design pattern.

Each Java application has an instance of the runtime class that enables the application to connect to its running environment.

An application cannot create its own instance of the runtime class.

A process is an instance of program execution

*/

 Class Sys2 {public static void main (string[] args) throws Exception {Runtime A=runtime.getruntime ();//Static call//runtime.getruntime () returns the Runtime object of the current application Process p= a.                   EXEC ("E:\\myeclipse\\myeclipse10\\myeclipse.exe");//This sentence compilation runs successfully////The Exec method here throws an exception.                             exec creates a native process that also executes the specified executable program and returns an instance of the process subclass that corresponds to the child process.                   In fact, Runtime.getruntime (). EXEC () is used to run external programs in a Java program.                   P.destroy ();//This sentence is used to end the child process.                    If P.destroy (), this sentence exists, in the compile run time because the main thread execution speed quickly, when the execution to this sentence, the child thread has just been established for a long time, so we look at the surface is nothing shows, nothing open.                    */*//process p2=a.exec ("notepad.exe Sys2.java");//* This sentence compiles successfully                    Open a Sys2.java file with your notebook we can open a file in this way, just by adding the appropriate program to the front. And why is there no way to write the word? Because the virtual opportunity to look for the path when the environment variable was set, and find notepad.exe */* Process p3=a.exec ("notepad.exe Sys2.java Je" in the path path                      4.java ");                     This sentence runs when a popup box shows that the Sys2.java Je4.java file is not found and asks whether to create a new file.          Obviously this kind of writing will let the virtual machine take Sys2.java Je4.java as a file name. */*//process P                     4= a.exec ("MyEclipse.exe");                       This sentence will run in error, the display cannot find the system files, because the virtual opportunity to set the environment variable when the path to find, and in the path path is not found MyEclipse.exe         */System.out.println ("helloworld!"); }}

—————— Split Line ——————

/*

Description of the Date class:

This class is also defined in the Java.util package.

Represents a specific time, accurate to a millisecond.

*/

Import java.util.*;import java.text.*;class  sys3{         public   static  void  main (string[] args)         {                   date  da=new date ();                   Soc (DA);//This can be printed directly, but the format looks uncomfortable                   SimpleDateFormat    sim= new   SimpleDateFormat ("yyyy years mm DD Day e  hh: Mm:ss ");       /*                    SimpleDateFormat class is defined in the Java.text package that the class object can encapsulate the date pattern, which is case-sensitive.                                                                       where y represents the year, M for the month, D for Day, E for the week, and H for minutes, and s for seconds.                    e has been included in the week, so there is no word "week" in parentheses.                   */                   String   str       = Sim.format (DA);                    The Format method returns the formatted Date                    SOC (str) of type string;                           }         Public  static void  Soc (Object  obj)         {              System.out.println (obj);}                                 }

—————— Split Line ——————

/*

Application of the Calendar abstract class:

The class is defined in the Java.util package,

The meaning of the text is the calendar.

*/

Import java.util.*;class sys4{public static void Main (string[] args) {                   Exercise 1: Get the February of any year with the number of days int x=2015;                        LX1 (x);         Exercise 2: Get the present moment yesterday lx2 ();                   } public static void Yx () {Calendar ca=calendar.getinstance ();//static method invocation                     /* Get accurate calendar time, getinstance return a calendar */SOC (CA),//For printing time, print out a bunch of information about the time Ca.set (2012,1,5);//You can set the month day, but the time and seconds these did not change/* here to note that the free time to set the month fill is 1, but the display                   February */Xianshi (CA);                   Ca.add (calendar.year,-1);                          It is possible to adjust the time freely, for example this is to reduce the year by one year Xianshi (CA);                    } public static void lx1 (int nian)//Exercise 1 {Calendar ca=calendar.getinstance ();          Ca.set (nian,2,1);          Ca.add (calendar.day_of_month,-1);          Xianshi (CA);               } public static void Lx2 ()//Exercise 2 {Calendar ca=calendar.getinstance ();        Ca.add (calendar.day_of_month,-1);                         Xianshi (CA); } public static void Xianshi (Calendar CA)//display time method {int year =ca.get (calendar.ye  AR); Gets the year int month = Ca.get (calendar.month);//Gets the month int hao = Ca.get (Calendar.                            Day_of_month);//Gets the number of months in int week = Ca.get (Calendar.day_of_week);//Get Week                            int Hour=ca.get (calendar.hour_of_day);//Get hour int minute=ca.get (calendar.minute);//Get minutes  int Sceond=ca.get (calendar.second);//Get seconds//Note here is the data for int type!!!                 Do not forget to add calendar/* In the Get method because the computer stores data starting with the corner label 0,           Therefore, the weeks and months obtained directly with the Get method are biased, so use the look-up table method to modify */ String[] sm={"January", "February", "March", "April", "May", "June", "July", "August                             "," September "," October "," November "," December "};            String[] sw={"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",};                             Soc (year+ "+sm[month]+hao+", "+sw[week]+", "+hour+": "+minute+": "+sceond");                               } public static void Soc (Object obj) {System.out.println (obj); }}

———————— Split Line ——————

/*

Math class, the class defined in the Java.lang package

Used to deal with some basic mathematical operations

It defines a lot of static methods.

*/

Import java.util.*;class sys5{public static void Main (string[] args)//main function {Mathrandomdemo ();                       public static void Mathrandomdemo () {SOC ("Below is Mathrandomdemo---"); for (int x=0;x<20;x++) {double d=math.random ();//Returns a Doub between 0 and 1                      Pseudo-random number of Le type (not including 1) int d2= (int) (Math.random () *10);                      Returns a pseudo-random number of type double (excluding 10) between 0 and 10, followed by a forced transfer to the INT type data SOC ("d=" +d);                   Soc ("d2=" +d2);                   } random m=new random ();                            for (int x=0;x<20;x++) {int i= m.nextint (20);//returns an int pseudo-random number from 0 to 20 (excluding 20)                   Soc ("i=" +i);                   } The//random class is defined in the Java.util package} public static void Ceildemo () {         Soc ("Below is Ceildemo---");          Double Dou=math.ceil (26.14);//returns the smallest integer soc ("dou=" +dou) that is greater than the specified double type data;//Print dou=27.0                   Double Dou2=math.ceil (-26.14);                          Soc ("dou2=" +DOU2);//Print dou=-26.0} public static void Floordemo () {                   Soc ("Below is Floordemo---");                   Double Dou3=math.floor (23.21);//returns the largest integer soc ("dou3=" +dou3) that is less than the specified double type data;//Print dou3=23.0                   Double Dou4=math.floor (-23.21);          Soc ("dou4=" +dou4);//Print dou4=-24.0} public static void Rounddemo ()                   {SOC ("Rounddemo---below"); Long Lo=math.round (53.18);//Rounding Soc ("lo=" +lo);//print lo=53 long lo2=math.round (-53.1                   8);                   Soc ("lo2=" +lo2);//print lo2=-53 long Lo3=math.round (53.78);       Soc ("lo3=" +LO3);//Print lo3=54            Long Lo4=math.round (-53.78);            Soc ("lo4=" +LO4);//Print lo4=-54} public static void Powdemo () {               Soc ("Below is Powdemo---");         Double K=math.pow (2,-3);//Returns a value of 2-3, which is the 0.125 Soc ("k=" +k);//Print k=0.125}                               public static void Soc (Object obj) {System.out.println (obj); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse programmer--23, Properties,runtime,date class, Math class,

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.