012String类+System类+Date类+SimpleDateFromat类+Calendar类+Math类+BigInteger类与BigDecimal类

一.String类:构造字符串对象

常量对象:字符串常量对象时用双引号括起的字符序列。例如:“你好”,“12.97”,“boy”等。字符串的字符使用Unicode字符编码,一个字符占两个字节。

String类比较常用的构造方法

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)

注意:String str = “abc”;与String str1 = new String(“abc”);的区别?

String代表不可变的字符序列,底层使用char[]存放,是final的。

方法

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[]

转换

字符串 与基本数据类型、包装类之间转换:字符串 --->基本数据类型、包装类调用相应的包装类的parseXxx(String str)。基本数据类型、包装类--->字符串调用字符串的重载的valueOf()方法。

字符串与字节数组间的转换:字符串---->字节数组调用字符串的getBytes()。字节数组---->字符串,调用字符串的构造器。

字符串与字符数组间的转换:字符串---->字符数组,调用字符串的toCharArray()。字符数组---->字符串调用字符串的构造器。

二.StringBuffer

java.lang.StringBuffer代表可变的字符序列,可以对字符串内容增删。很多方法与String相同,但StringBuffer是可变长度的。StringBuffer是一个容器。

方法

添加: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)

三.StringBuilder类

可变的字符序列,是jdk5.0新加入的,线程不安全,效率要高于StringBuffer。

效率从高到底: StringBuilde > StringBuffer > String。

四.System

System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。

五、Date

       

@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
}

六.SimpleDateFromat

         

@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);
}

七.Calendar

              

@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);
}

八.Math

 

九.BigInteger类与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));
}

 

发布了23 篇原创文章 · 获赞 7 · 访问量 1654

猜你喜欢

转载自blog.csdn.net/weixin_44145972/article/details/88926790
今日推荐