1. StringTokenizer:
字符串分割类:
public class TestALL {
public static void main(String[] args) {
System.out.println("默认以空格,\\t,\\r,\\n分割")
StringTokenizer st = new StringTokenizer("www ooobj com")
while(st.hasMoreElements()){
System.out.println("Token:" + st.nextToken())
}
System.out.println("指定以.分割")
st = new StringTokenizer("www.ooobj.com",".")
while(st.hasMoreElements()){
System.out.println("Token:" + st.nextToken())
}
System.out.println("指定以.分割,并在结果中包含分隔符")
st = new StringTokenizer("www.ooobj.com",".",true)
while(st.hasMoreElements()){
System.out.println("Token:" + st.nextToken())
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
输出:
默认以空格,\t,\r,\n分割
Token:www
Token:ooobj
Token:com
指定以.分割
Token:www
Token:ooobj
Token:com
指定以.分割,并在结果中包含分隔符
Token:www
Token:.
Token:ooobj
Token:.
Token:com
想必大家对SimpleDateFormat并不陌生。SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调试的问题,因为 DateFormat 和 SimpleDateFormat 类不都是线程安全的,在多线程环境下调用 format() 和 parse() 方法应该使用同步代码来避免问题。
最好的方法,使用ThreadLocal:
package com.peidasoft.dateformat;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConcurrentDateUtil {
private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public static Date parse(String dateStr) throws ParseException {
return threadLocal.get().parse(dateStr);
}
public static String format(Date date) {
return threadLocal.get().format(date);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3. Java获取路径:
public String getCurrentPath(){
//取得根目录路径
String rootPath=getClass().getResource("/").getFile().toString()
//当前目录路径
String currentPath1=getClass().getResource(".").getFile().toString()
String currentPath2=getClass().getResource("").getFile().toString()
//当前目录的上级目录路径
String parentPath=getClass().getResource("../").getFile().toString()
return rootPath
}