Java基礎知識之常用類庫(1)

來源:互聯網
上載者:User

標籤:類庫   stringbuffer   date   dateformat   system   

StringBuffer

1、StringBuffer 和 String 並無直接關係
2、StringBuffer 避免了Sring內容改變時產生垃圾的現象。
3、一個StringBuffer對象可以調用toString()方法轉換為String對象。
下面通過一個小例子示範StringBuffer類的相關方法:

package StringBuffer;public class StringBufferDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        //* StringBuffer的構造        StringBuffer buf1 = new StringBuffer();//相當於一個空的字串        StringBuffer buf2 = new StringBuffer("123");//通過字串來構造StringBuffer對象        System.out.println("在下面可以看到一個空行加上字串“123”"+"\n"+buf1+"\n"+buf2);        /*         * 常用方法         *          * */        //append()方法添加字元        buf1.append("123").append(‘c‘).append(23.2).append(false);        System.out.println("下面可以看到通過append()方法銜接的字串:"+"\n"+buf1);        //insert()方法插入字串        buf1.insert(0, true).insert(0, "第一個字元錢插入");        System.out.println("下面可以看到指定位置前插入字元後的字串"+"\n"+buf1);        //delete()方法刪除指定範圍的子串(從下標0開始的一個字元)        buf1.delete(0, 1);        System.out.println("下面可以看到刪除指定位置字元後的字串"+"\n"+buf1);        //reverse()方法反轉字串內容        buf1.reverse();        System.out.println("下面可以看到反轉後的字串"+"\n"+buf1);        //replace()方法替換字元        buf1.replace(0, 1, "7788");        System.out.println("下面可以看到替換後的字串"+"\n"+buf1);    }}
Runtime

直接上例子,看效果!

public class RunTimeDemo {    /**     * @param args     * @throws IOException      * @throws InterruptedException      */    public static void main(String[] args) throws IOException, InterruptedException {        // TODO Auto-generated method stub        Runtime r = Runtime.getRuntime();//得到Runtime執行個體        Process pro = r.exec("notepad");//執行原生程式“記事本”        //Thread.sleep(5000);//休眠5秒        pro.destroy();//銷毀該進程        System.out.println("本機記憶體:"+r.totalMemory());    }}
System
public class SystemDemo {    /**     * 計算了一個程式的已耗用時間     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        long start = System.currentTimeMillis();        System.out.println("目前時間:"+start);        for (int i = 0; i < 100; i++) {            for (int j = 0; j < 100; j++) {                j++;            }        }        long end = System.currentTimeMillis();        System.out.println("程式執行時間:"+(end - start));    }}
Date、Calendar
public class DateDemo {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        //先看看今天的日期吧,格式有點怪異        System.out.println(new Date());        //calendar類,該類為抽象類別,需要使用其子類執行個體化        Calendar c = new GregorianCalendar();        StringBuffer s = new StringBuffer();        s.append(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+                c.get(Calendar.DAY_OF_MONTH)+"日"+"  "+c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE));        System.out.println(s);    }}

上面的System.out.println(new Date())輸出的日期是不是讓人不習慣,下面這個會讓你好受點:

DateFormat
import java.text.DateFormat;import java.util.Date;public class FormatDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        Date d = new Date();//執行個體化日期        DateFormat f = DateFormat.getDateInstance();//得到預設的日期格式化執行個體        DateFormat ff = DateFormat.getDateTimeInstance();//得到預設的日期時間格式化執行個體        System.out.println(f.format(d));//格式化後輸出        System.out.println(ff.format(d));//格式化後輸出    }}

輸出是不是習慣了,但是其格式化方式能不能改變呢?答案是肯定的,繼續看:

import java.text.DateFormat;import java.util.Date;public class FormatDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        Date d = new Date();//執行個體化日期        //下面的方法給了參數的        DateFormat f = DateFormat.getDateInstance(DateFormat.FULL);//得到預設的日期格式化執行個體        DateFormat ff = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);//得到預設的日期時間格式化執行個體        System.out.println(f.format(d));//格式化後輸出        System.out.println(ff.format(d));//格式化後輸出    }}

其實我們還可以有更多的格式,如:2015/5/26,下次繼續吧(-_-)

Java基礎知識之常用類庫(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.