012String + System Class Class Class + SimpleDateFromat + Date + Calendar category class category + Math + BigInteger classes and class BigDecimal

A .String categories: string object constructor

Constant Object: a sequence of characters enclosed in double quotation marks object with the string constants. For example: "Hello", "12.97", "boy " and so on. Character string using the Unicode character encoding, a character occupies two bytes.

String-based constructor commonly used

String s1 = new String();
String s2 = new String(String original);
String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count)

Note: String str = "abc"; distinction; and String str1 = new String ( "abc")?

String character sequence represents immutable, using the underlying char [] storage, is final.

method

public int length()
public char charAt(int index):返回在指定index位置的字符。index从0开始
public boolean equals(Object anObject):比较两个字符串是否相等。相等返回true。否则返回false
public int compareTo(String anotherString)
public int indexOf(String s):返回s字符串在当前字符串中首次出现的位置。若没有,返回-1
public int indexOf(String s ,int startpoint):返回s字符串从当前字符串startpoint位置开始的,首次出现的位置。
public int lastIndexOf(String s):返回s字符串最后一次在当前字符串中出现的位置。若无,返回-1
public int lastIndexOf(String s ,int startpoint)
public boolean startsWith(String prefix):判断当前字符串是否以prefix开始。
public boolean endsWith(String suffix):判断当前字符串是否以suffix结束。
public boolean regionMatches(int firstStart,String other,int otherStart ,int length): 判断当前字符串从firstStart开始的子串与另一个字符串other从otherStart开始,length长度的字串是否equals
public String substring(int startpoint)
public String substring(int start,int end):返回从start开始到end结束的一个左闭右开的子串。start可以从0开始的。
pubic String replace(char oldChar,char newChar)
public String replaceAll(String old,String new)
public String trim():去除当前字符串中首尾出现的空格,若有多个,就去除多个。
public String concat(String str):连接当前字符串与str
public String[] split(String regex):按照regex将当前字符串拆分,拆分为多个字符串,整体返回值为String[]

Change

String basic data type conversion between packaging: string ---> basic data types, packaging , call the appropriate wrapper class parseXxx (String str). Basic data types, packaging ---> string , calling string overloaded valueOf () method.

Conversion between strings and bytes: string ----> byte array , calling string getBytes (). Byte array ----> string, the string constructor call.

Conversion between strings and arrays of characters: string ----> array of characters, calling string toCharArray (). An array of characters ----> string , call the constructor string.

Two .StringBuffer class

java.lang.StringBuffer variable represents the sequence of characters, you can add or delete the contents of the string. String many ways and the same, but StringBuffer variable length. StringBuffer is a container.

method

添加:append()
删除:delete(int i,int j)
修改:setCharAt(int index,char ch)
查 charAt(int n);
插入:insert(int index,String str) 
反转:reverse()
长度:length()
替换:replace(int startIndex ,int endIndex, String str)
截取:substring(int start,int end)

Three .StringBuilder class

The variable sequence of characters, is jdk5.0 new entrants, thread-safe, efficiency is higher than StringBuffer.

From the high efficiency in the end: StringBuilde> StringBuffer> String.

Four .System class

The System public static class provided with currentTimeMillis Long () returns the current time at 0:00 on January 1, 1970 0 seconds milliseconds time difference.

Five, Date class

       

@Test
public void test(){
       //创建一个Date的实例
       Date d1 = new Date();
       System.out.println(d1.toString());//Mon May 12 15:17:01 CST 2014
       System.out.println(d1.getTime());//1399879144434
       Date d2 = new Date(1399879144434L);
       System.out.println(d2);//Mon May 12 15:17:01 CST 2014
}

Six .SimpleDateFromat class

         

@Test
public void test() throws Exception{
       //1.格式化1
       SimpleDateFormat sdf1 = new SimpleDateFormat();
       String date1 = sdf1.format(new Date());
       System.out.println(date1);//14-5-12 下午3:24
       //2.格式化2
       SimpleDateFormat sdf2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
       date1 = sdf2.format(new Date());
       System.out.println(date1);//星期一, 12 五月 2014 15:29:16 +0800
       
       //3.解析:
       Date date2 = sdf1.parse("14-5-12 下午3:24");
       System.out.println(date2);
       date2 = sdf2.parse("星期一, 12 五月 2014 15:29:16 +0800");
       System.out.println(date2);
}

Seven .Calendar class

              

@Test
//日历:Calendar类   
//方法:get()/add()/set()/Date getTime()/setTime(Date d)
public void test(){
       Calendar c = Calendar.getInstance();
       int day = c.get(Calendar.DAY_OF_MONTH);
       System.out.println(day);
       
       c.add(Calendar.DAY_OF_MONTH, -2);
       day = c.get(Calendar.DAY_OF_MONTH);
       System.out.println(day);
       
       c.set(Calendar.DAY_OF_MONTH, 23);
       Date d = c.getTime();
       System.out.println(d);
}

Eight .Math class

 

Nine .BigInteger class and class BigDecimal

        


@Test
public void testBigInteger() {
       BigInteger bi = new BigInteger("12433241123");
       BigDecimal bd1 = new BigDecimal("12435.351");
       BigDecimal bd2 = new BigDecimal("11");
       System.out.println(bi);;
       System.out.println(bd1.divide(bd2, BigDecimal.ROUND_HALF_UP));
       System.out.println(bd1.divide(bd2, 15, BigDecimal.ROUND_HALF_UP));
}

 

Published 23 original articles · won praise 7 · views 1654

Guess you like

Origin blog.csdn.net/weixin_44145972/article/details/88926790