Java常用类库

StringBuffer类

StringBuffer与String的区别

        StringBuffer sb = new StringBuffer();

赋值或者追加内容

        append()

        ex:sb,append("lbw");

在任意位置添加内容

        insert();

        ex:sb.insert(3,"nb");

反向操作reverse()

        System.out.println(sb.reverse());

替换

        replace

截取字符串

        substring();

删除指定范围类的字符串

        delete()

查找指定的内容是否存在

        indexof();

Runtime类

        每一个java虚拟机都对应了一个Runtime类的实例

        通过Runtime的实例可以取得空闲内存和最大内存和执行垃圾回收。

                

Runtime r = Runtime.getRuntime();
System.out.println(r.freeMemory());
System.out.println(r.maxMemory());
String s ="haohaorena dsfa dsf ads fads f ads ";
System.out.println(s);
for(int i=0;i<50;i++){
	s = s+i;
}
System.out.println(r.freeMemory());
System.out.println(r.maxMemory());
r.gc();
System.out.println(r.freeMemory());

Runtime类的exec()和Process类

        

//exec调用本地程序,Process类的实例可以操控线程
		Process p = null;
Runtime r = Runtime.getRuntime();
		try {
			p = r.exec("notepad.exe");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			Thread.sleep(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		p.destroy();

System类(其方法全部是静态方法)

        static void gc()  运行垃圾回收器。

        static void exit(int status) 终止当前正在运行的 Java 虚拟机。

         static long currentTimeMillis() 返回以毫秒为单位的当前时间。

        static Properties getProperties() 确定当前的系统属性。         System.getProperties().list(System.out);

        static String getProperty(String key) 获取指定键指示的系统属性。

日期操作类

        Date类

Date d = new Date();
System.out.println(d);

        calendar类

        calendar类本身是一个抽象类,所有要实例化必须用它的子类GregorianCalendar

Calendar c = new GregorianCalendar();
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH)+1);

DateFormat类(在java.text包)

DateFormat d1 = DateFormat.getDateInstance();
DateFormat t1 = DateFormat.getTimeInstance();
System.out.println(d1.format(new Date()));
System.out.println(t1.format(new Date()));
		
DateFormat d2 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","cn"));
DateFormat t2 = DateFormat.getTimeInstance(DateFormat.ERA_FIELD,new Locale("zh","cn"));
System.out.println(d2.format(new Date()));
System.out.println(t2.format(new Date()));

SimpleDateFormat

String strDate = "2012-10-01 11:10:10.222";
String formt1 = "yyyy-MM-dd HH:mm:ss.SSS";
String formt2 = "yyyy年MM月dd日HH时mm秒ss分SSS毫秒";
SimpleDateFormat sdf1 = new SimpleDateFormat(formt1);
SimpleDateFormat sdf2 = new SimpleDateFormat(formt2);
Date dd = null;
try {
	dd = sdf1.parse(strDate );//这里只能是sdf1不能是sdf2
} catch (ParseException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
System.out.println(sdf2.format(dd));

Math类

System.out.println(Math.PI);
System.out.println(Math.random());

Random类

System.out.println(Math.random());

System.out.println(new Random().nextInt(100));
System.out.println(new Random().nextInt());

NumberFormat

NumberFormat nf = null;
nf = NumberFormat.getInstanc();
System.out.println("格式化之后的数字:"+ nf.format(1000000));
System.out.println("格式化之后的数字:"+ nf.format(1000.345));

DecimalFormat类

序号标记位置描述
10数字代麦阿拉伯数字,每一个0表示一位阿拉伯数字,如果该位不存在则显示0
2#数字代表阿拉伯数字,每一个#表示一位阿拉伯数字,如果该位不存在则不显示
3.数字小数点分隔符或货币的小数分隔符
4-数字代表负号
5,数字分组分隔符
6E数字分隔科学计数法中的尾数和指效
7;子模式边界分隔正数和负数了模式
8%前缀或后缀数字乘以100并显示为百分数
9\u2030前缀或后缀乘以1000并显示为干分数
10\u00A4前缀或后缀货币记号,由货币号替换。如果两个同时出现、则用国际货币符号替换;如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符
11‘’前缀或后缀

用于在前缀或后缀中为特殊字符加引导,例如"##"将123格式化为"#123"。要创建单引号本身,则连续使用两个单引号,例如"#o"clock"

public class NumberFormatDemo1{
    public atatic void main(String[] args){
        FormatDemo demo = new FormatDemo();
        demo.format1("###,###.###",111222.34567);
        demo.format1("000,000.000",11222.34567);
        demo.format1("###,###.###¥",111222.34567);
        demo.format1("000,000.000¥",11222.34567);
        demo.format1("##.###%",0.345678);            //使用百分数形式
        demo.format1("00.###%",0.0345678);           //使用百分数形式
        demo.format1("##.###\u2030",0.345678);       //使用千分数形式
    }
}

BigInteger类

System.out.println("加法操作:" + sb2.add(sb));          //加法操作
System.out.println("减法操作:" + sb2.subtract(sb));     //减法操作
System.out.println("乘法操作:" + sb2.multiply(sb));     //乘法操作
System.out.println("除法操作:" + sb2.divide(sb));       //除法操作
System.out.println("最大数:" + sb2.max(sb));            //求出最大数
System.out.println("最小数:" + sb2.min(sb));            //求出最小数
BigInteger result[] = sb.divideAndRemainder(sb)         //除法操作
System.out.println("商是:" + result[0] + ":余数是:" + result[1]);     

BigDecimal类

class MyMath{
    public static double add(double d1,double d2){        //进行加法运算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b1 = new BigDecimal(d2);
        return b1.add(b2).doubleValue();
    }
    public static double sub(double d1,double d2){        //进行减法运算
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b1 = new BigDecimal(d2);
        return b1.subtract(b2).doubleValue();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值