Java基础知识之常用类库(1)

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,下次继续吧(-_-)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值