Java Tour (23) mathematical operations of--system,runtime,date,calendar,math

Source: Internet
Author: User
Tags pow

Java Tour (23) mathematical operations of--system,runtime,date,calendar,math

Map is too difficult to write, finishing I have been dizzy to turn, it seems to need to open a topic to say this, and now we come to learn some new small things

One. System

System meaning, System class, the method inside is static, you can call directly

Let's take a demo and start with getting system information:

 PackageCom.lgl.hellojava;ImportJava.util.Properties; Public  class Hellojjava {     Public Static void Main(string[] args) {/** * Some information about System Information Acquisition System: * Properties = getProperties * out: Standard output, default is console * in: Standard input , default console * /Properties Properties = System.getproperties ();/** * Because Properties is a subclass of Hashtab, which is a subclass object of the Map collection * Then you can use the Map method to remove the elements in the collection, which are all strings, * no generic definitions */         for(Object Obj:properties.keySet ())            {String value = (string) properties.get (obj); System.out.println (obj +":"+ value); }    }}

Get too much information, it is not listed, you can see

He prints everything out, if you want to customize some information, you can do it

        /**         * 如何在系统中自定义一些特有信息         */        System.setProperty("mykey""myvalue");

This allows the information to be obtained using the method above, and we can also get a single attribute information, such as getting the system name

package com.lgl.hellojava;publicclass HelloJJAVA {    publicstaticvoidmain(String[] args) {        /**         * 获取指定属性信息         */        String property = System.getProperty("os.name");        System.out.println(property);    }}

OK, you can get the system name.

Two. RunTime

This class is a singleton design mode, does not provide a constructor, that is, cannot new object, and found that the class also has a non-static method, he will certainly provide a method to get the class object, and the method is static and return value is the object of this class, runtime translation comes from the meaning of execution, is very powerful, Just like CMD, you can run Linux commands and we'll show you, like we do open Gitbash

package com.lgl.hellojava;import java.io.IOException;publicclass HelloJJAVA {    publicstaticvoidmain(String[] args) {        Runtime r = Runtime.getRuntime();        try {            r.exec("C:\\Program Files\\Git\\git-bash");        catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

In this way, a run is turned on. Kill the words. Return to a process and execute destroy.

Three. Date

Date Description class, this is more practical, is also relatively simple, such as

package com.lgl.hellojava;import java.util.Date;publicclass HelloJJAVA {    publicstaticvoidmain(String[] args) {        new Date();        System.out.println(date);    }}

So we can print out the time.

Date, month, number, time, year, but a little laborious, we can output according to the format, we want to see here

What we're going to use is dateformat, and all we need is his sub-class SimpleDateFormat

package  com.lgl.hellojava;< Span class= "Hljs-keyword" >import  Java.text.SimpleDateFormat; import  java.util.Date; public  class  hellojjava  {  public  static  void  main  (string[] args)        {Date date = new  date ();        //to encapsulate the pattern  SimpleDateFormat format = new  simpledateformat ( "        yyyy mm month DD Day HH:MM:SS ");        //formatting a Date object         String time = Format.format (date);    System.out.println (time); }}

So that we can use the format to represent the

Four. Calendar

What if I want to get a time alone? such as the year, such as the month, this time can be used calendar

Package Com.lgl.hellojava;import Java.util.Calendar; Public classHellojjava { Public Static void Main(string[] args)        {Calendar calendar = calendar.getinstance (); String[] Mons = {"January","February","March","April","May","June","July","August","September","October","November","December"};intindex = Calendar.Get(Calendar.month);//Query the current dateSOP (Calendar.Get(calendar.year) +"Year"); SOP (Calendar.Get(Calendar.month) +1) +"Month");        SOP (Mons[index]); SOP (Calendar.Get(Calendar.day_of_month) +"Day"); Sop"Week:"+ Calendar.Get(Calendar.day_of_week)); } Public Static void SOP(Object obj) {System. out. println (obj); }}

OK, so you can get the

He is more flexible, there are a lot of small tricks, this we go on our own to explore

Five. Math

Mathematical class, in a specific area with a lot of, the inside degree is static, that is, tool class, we come to know him

1.ceil

Returns the smallest integer greater than the specified data

//返回大于指定数据的最小整数doubleceil = Math.ceil(12.34);

The output here is 13.0.

2.floor

Returns the smallest integer less than the specified data

//返回小于指定数据的最小整数double ceil1 = Math.floor(12.34);sop(ceil1);

Back is 12.0.

3.round

Rounded

//四舍五入long ceil2 = Math.round(12.34);sop(ceil2);

Obviously, the output is 12.

4.pow

Arithmetic of power

// 2的3次方double ceil3 = Math.pow(23);sop(ceil3);

Get 8

5.random

Random number

/ 随机数int ceil4 = (int) (Math.random()*10);sop(ceil4);

This must be learned, very useful, but also very common

You can also write this

RandomRandom();sop(r.nextInt(10));

Most of these are the use of tools, of course, these are also more practical classes, we must master, this is a little short, because I want to open a more special knowledge class, because Java learning also quite a long time, this knowledge point we must master, is I/O, file flow operation, is very important, For the end of knowledge and induction, I decided to write a new article, this blog post here is over,

If interested, can add group: 555974449, we learn technology together!

Java Tour (23) mathematical operations of--system,runtime,date,calendar,math

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.