一、String与Date(java.util.Date)互转
1.1 String -> Date
1. String dateStr = "2010/05/04 12:34:23";
2. new
3. //注意format的格式要与日期String的格式相匹配
4. new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
5. try
6. date = sdf.parse(dateStr);
7. System.out.println(date.toString());
8. catch
9. e.printStackTrace();
10. }
String dateStr = "2010/05/04 12:34:23";
Date date = new Date();
//注意format的格式要与日期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
date = sdf.parse(dateStr);
System.out.println(date.toString());
} catch (Exception e) {
e.printStackTrace();
}
1.2 Date -> String
日期向字符串转换,可以设置任意的转换格式format
1. String dateStr = "";
2. new
3. //format的格式可以任意
4. new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
5. new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
6. try
7. dateStr = sdf.format(date);
8. System.out.println(dateStr);
9. dateStr = sdf2.format(date);
10. System.out.println(dateStr);
11. catch
12. e.printStackTrace();
13. }
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
try {
dateStr = sdf.format(date);
System.out.println(dateStr);
dateStr = sdf2.format(date);
System.out.println(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
二、String与Timestamp互转
2.1 String ->Timestamp
使用Timestamp的valueOf()方法
1. Timestamp ts = new
2. "2011-05-09 11:49:45";
3. try
4. ts = Timestamp.valueOf(tsStr);
5. System.out.println(ts);
6. catch
7. e.printStackTrace();
8. }
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "2011-05-09 11:49:45";
try {
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
} catch (Exception e) {
e.printStackTrace();
}
注:String的类型必须形如: yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,否则报错!!!
如果String为其他格式,可考虑重新解析下字符串,再重组~~
2.2 Timestamp -> String
使用Timestamp的toString()方法或者借用DateFormat
1. Timestamp ts = new
2. "";
3. new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
4. try
5. //方法一
6. tsStr = sdf.format(ts);
7. System.out.println(tsStr);
8. //方法二
9. tsStr = ts.toString();
10. System.out.println(tsStr);
11. catch
12. e.printStackTrace();
13. }
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "";
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
//方法一
tsStr = sdf.format(ts);
System.out.println(tsStr);
//方法二
tsStr = ts.toString();
System.out.println(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
很容易能够看出来,方法一的优势在于可以灵活的设置字符串的形式。
三、Date( java.util.Date )和Timestamp互转
声明:查API可知,Date和Timesta是父子类关系
3.1 Timestamp -> Date
1. Timestamp ts = new
2. new
3. try
4. date = ts;
5. System.out.println(date);
6. catch
7. e.printStackTrace();
8. }
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
try {
date = ts;
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
很简单,但是此刻date对象指向的实体却是一个Timestamp,即date拥有Date类的方法,但被覆盖的方法的执行实体在Timestamp中。
3.2 Date -> Timestamp
父类不能直接向子类转化,可借助中间的String~~~~
注:使用以下方式更简洁
Timestamp ts = new Timestamp(date.getTime());