1.SimpleDateFormat宽泛解析实验
1.1 代码
package testJava;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatTest {
public static void main(String[] args) {
SimpleDateFormatTest sdft = new SimpleDateFormatTest();
sdft.main();
}
public void main() {
errFenxi();
errFenxi2();
errFenxi3();
errFenxi4();
errFenxi5();
}
void errFenxi() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse("2007-09-06");
System.out.println("源时间:2007-09-06");
System.out.println(date);
Timestamp time = new Timestamp(date.getTime());
System.out.println(time);
} catch (Exception e) {
System.out.println(e.toString());
}
}
void errFenxi2() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse("2007-09-");
System.out.println("源时间:2007-09-");
System.out.println(date);
Timestamp time = new Timestamp(date.getTime());
System.out.println(time);
} catch (Exception e) {
System.out.println(e.toString());
}
}
void errFenxi3() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse("20070090");
System.out.println("源时间:20070090");
System.out.println(date);
Timestamp time = new Timestamp(date.getTime());
System.out.println(time);
} catch (Exception e) {
System.out.println(e.toString());
}
}
void errFenxi4() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2007-00-90");
System.out.println("源时间:2007-00-90");
System.out.println(date);
Timestamp time = new Timestamp(date.getTime());
System.out.println(time);
} catch (Exception e) {
System.out.println(e.toString());
}
}
void errFenxi5() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2007-00");
System.out.println("源时间:2007-00");
System.out.println(date);
Timestamp time = new Timestamp(date.getTime());
System.out.println(time);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
1.2.结果
源时间:2007-09-06
Sat Dec 09 00:00:00 CST 2006
2006-12-09 00:00:00.0
源时间:2007-09-
Sat Dec 09 00:00:00 CST 2006
2006-12-09 00:00:00.0
源时间:20070090
Wed Feb 28 00:00:00 CST 2007
2007-02-28 00:00:00.0
源时间:2007-00-90
Wed Feb 28 00:00:00 CST 2007
2007-02-28 00:00:00.0
java.text.ParseException: Unparseable date: "2007-00"
1.3.代码分析
2 关于多线程
2.1 多线程引起异常
2.1.1 代码
package testJava;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatThread {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormatThread sdft = new SimpleDateFormatThread();
sdft.main();
}
void main() {
MyThread m = new MyThread();
for(int i=0;i<20;i++) {
new Thread(m ).start();
}
}
class MyThread implements Runnable{ // 实现Runnable接口,作为线程的实现类
public void run(){ // 覆写run()方法,作为线程 的操作主体
Timestamp time = new Timestamp(System.currentTimeMillis());// 获取系统当前时间
try {
Date date = df.parse("2007-09-06 10:01:59");
System.out.println(date);
}catch(Exception e) {
System.out.println(e.toString());
}
}
};
}
2.1.2 运行效果
java.lang.NumberFormatException: For input string: "E210"
java.lang.NumberFormatException: For input string: ""
java.lang.NumberFormatException: For input string: ""
java.lang.NumberFormatException: For input string: "E2"
java.lang.NumberFormatException: multiple points
java.lang.NumberFormatException: For input string: "E2"
java.lang.NumberFormatException: multiple points
java.lang.NumberFormatException: empty String
Mon Nov 05 00:01:59 CST 2007
Fri Aug 31 01:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 70
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 20074
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 70
Thu Sep 06 10:01:59 CST 2007
2.1.3 分析
因为多线程,导致了处理的数据错位,造成了类型转换的异常。
2.2 解决异常
2.2.1 加锁
package testJava;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatThread {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormatThread sdft = new SimpleDateFormatThread();
sdft.main();
}
void main() {
MyThread m = new MyThread();
for (int i = 0; i < 20; i++) {
new Thread(m).start();
}
}
class MyThread implements Runnable { // 实现Runnable接口,作为线程的实现类
public void run() { // 覆写run()方法,作为线程 的操作主体
Timestamp time = new Timestamp(System.currentTimeMillis());// 获取系统当前时间
try {
Date date = null;
synchronized (this) {
date = df.parse("2007-09-06 10:01:59");
}
System.out.println(date);
} catch (Exception e) {
System.out.println(e.toString());
}
}
};
}
2.2.2 运行效果
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
2.3 解决异常2
2.3.1 代码
package testJava;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatThread {
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormatThread sdft = new SimpleDateFormatThread();
sdft.main();
}
void main() {
MyThread m = new MyThread();
for (int i = 0; i < 20; i++) {
new Thread(m).start();
}
}
class MyThread implements Runnable { // 实现Runnable接口,作为线程的实现类
public void run() { // 覆写run()方法,作为线程 的操作主体
Timestamp time = new Timestamp(System.currentTimeMillis());// 获取系统当前时间
try {
Date date = null;
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2007-09-06 10:01:59");
System.out.println(date);
} catch (Exception e) {
System.out.println(e.toString());
}
}
};
}
2.2.2 运行结果
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007
Thu Sep 06 10:01:59 CST 2007