介绍一下几个java中经常用到的类
1.1 String 类的常用方法
public class StringTest { /** * 1. append() 方法: 把字符串加入到以后的字符序列的后面 * 注意: append() 方法的返回值还是当前的 StringBuffer 对象. 可以使用方法的连缀(只要调用一个方法,这个方法的返回值是调用者本身,就可以实现方法的连缀,这里指连续使用append). * * 2. StringBuilder VS StringBuffer: * StringBuilder 是线程不安全的, 效率更高. 所以更多的时候使用 StringBuilder * StringBuffer 是线程安全的, 效率偏低, 在多线程的情况下下使用. */ @Test public void testAppend(){ StringBuilder stringBuilder = new StringBuilder(); // ... = new StringBuilder(5); 如果预置一个容量,当超出这个容量时,会继续加入 stringBuilder.append("<html>") .append( "<body>") .append( "</body>") .append("</html>"); System.out.println(stringBuilder); } @Test public void testStringBuilder() { StringBuffer stringBuffer = new StringBuffer("abcde"); System.out.println(stringBuffer); stringBuffer.replace(1, 3, "mvp"); System.out.println(stringBuffer); } }
public SimpleDateFormat(String pattern);
该构造方法可以用 参数pattern 指定的格式创建一个对象,该对象调用:
/** * Date() 封装了时间和日期. * * DateFormat: 把日期对象格式化为一个字符串 & 把一个字符串转为一个 Date 对象 * 1. DateFormat: 是一个抽象类. * 抽象类获取对象的方式: * 1). 创建其子类对象 * 2). 有的抽象类中提供了静态工厂方法来获取抽象类的实例. */ public class DateTest { //创建其子类对象 //这种样式是自定义的(多用) @Test public void testSimpleDateFormat() throws Exception{ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println(dateFormat.format(date)); //字符串转化为Date,字符串形式必须是dateFormat的格式 String dateStr = "1990-12-12 12:12:12"; Date date2 = dateFormat.parse(dateStr); System.out.println(date2); } //提供了静态工厂方法来获取抽象类的实例,可以不用new //这种样式只能是系统指定的,系统提供的样式 @Test public void testDateFormat() throws Exception{ DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); Date date = new Date(); String dateStr = dateFormat.format(date); System.out.println(dateStr); //字符串转化为Date,字符串形式必须是dateFormat的格式 dateStr = "2013年10月10日 下午08时08分08秒"; Date date2 = dateFormat.parse(dateStr); System.out.println(date2); } @Test public void testDate() { Date date = new Date(); System.out.println(date); } }
4. Random,Math类
原文:http://www.cnblogs.com/tech-bird/p/3526983.html