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类
序号 | 标记 | 位置 | 描述 |
1 | 0 | 数字 | 代麦阿拉伯数字,每一个0表示一位阿拉伯数字,如果该位不存在则显示0 |
2 | # | 数字 | 代表阿拉伯数字,每一个#表示一位阿拉伯数字,如果该位不存在则不显示 |
3 | . | 数字 | 小数点分隔符或货币的小数分隔符 |
4 | - | 数字 | 代表负号 |
5 | , | 数字 | 分组分隔符 |
6 | E | 数字 | 分隔科学计数法中的尾数和指效 |
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();
}
}