Common library of Basic Java Knowledge (2)

Source: Internet
Author: User
Tags square root

SimpleDateFormat

Set the date and time you want to display the way:

import Java.text.simpledateformat;import Java.util.Date;public  class  Formatdemo { public  static  void  main  (string[] args) {//TODO auto-generated method stub  Date d = n EW  Date ();        //instantiation date         //yyyy represents the year, MM for the month, DD for days, minutes and seconds to say   SimpleDateFormat s = new  simpledateformat ( "Yyyy,MM,dd Hh:mm:SSS ");        //generate the format you want to display  System. out . println (S.format (d)); //date formatted }}  

In addition to setting the format you want, we can also set the given string as a date:

ImportJava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Date; Public  class formatdemo {     Public Static void Main(string[] args) {the date format in the//string should be the same as the date format declared below ("Yyyy,mm,dd hh:mm")String str ="2015,1,3 3:44"; SimpleDateFormat SS =NewSimpleDateFormat ("Yyyy,mm,dd hh:mm");Try{The //parse () method throws an exceptionDate dd = Ss.parse (Str.trim ());        SYSTEM.OUT.PRINTLN (DD); }Catch(ParseException e) {//TODO auto-generated catch blockE.printstacktrace (); }    }}
Math

The Math class provides a number of mathematical arithmetic-related methods:

 Public classMathdemo { Public Static void Main(string[] args) {//TODO auto-generated method stubSystem. out. println ("To find the absolute value of a number"+math.abs (-111)); System. out. println ("Ask for the square root of a number"+MATH.SQRT (121)); System. out. println ("To find the sine of a corner"+math.signum (Math.PI)); System. out. println ("Ask for a larger number"+math.max ( A, the));//Too many methods don't show up, check the documentation.}}
import java.util.Random;publicclass RandomDemo {    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        new Random();//创建一个Random的实例        int10;        //生成随机数,并打印        System.out.println("这是一个给定范围内的随机数:"+r.nextInt(x));    }}
NumberFormat
Import Java.text.NumberFormat; Public classNumberformatdemo { Public Static void Main(string[] args) {//Get the default format instanceNumberFormat n = numberformat.getinstance ();//format and outputSystem. out. println (N.format (23123123)); System. out. println (N.format (23123.123));//decimals reserved up to 3 bitsSystem. out. println (N.format (23.123123));/* * Result: * 23,123,123 * 23,123.123 * 23.123 * */}}
DecimalFormat

As with Simpdateformat, you can specify the format to display:

Import Java.text.decimalformat;class demo{String p;DoubleN Public Demo(String p,DoubleN) {//Initialize         This. p = p; This. N = n; } Public void format(){//Get formatDecimalFormat format =NewDecimalFormat (P);//Formatted outputSystem. out. println (Format.format (n)); }} Public classDemo1 { Public Static void Main(string[] args) {//TODO auto-generated method stub        NewDemo ("000,000.00",2345.326). Format ();NewDemo ("###,000.00",2345.326). Format ();NewDemo ("# #0, 000.00¥",2345.326). Format ();NewDemo ("###,000.00%",2345.326). Format (); }}

Description of related symbols:

BigInteger, BigDecimal
Import Java. Math. BigDecimal;Import Java. Math. BigInteger;public class Demo1 {public static void main (string[] args) {//instancing large number BigInteger i = new BigInteger ("0");Instantiate a large number of floating-point BigDecimal j = new BigDecimal ("7654321");BigDecimal k = new BigDecimal ("1234567");System. out. println("i="+i);System. out. println("j="+J);System. out. println("k="+K);System. out. println("Addition"+j. Add(k));System. out. println("Subtraction"+j. Subtract(k));System. out. println("Multiplication"+j. Multiply(k));System. out. println("Division"+j. Divide(k));Bigdecimal[] result = J. Divideandremainder(k);//retention quotient and remainderSystem. out. println("Business"+result[0]+", remainder"+result[1]);}}

Common library of Basic Java Knowledge (2)

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.