Date,Timestamp,String之間的轉換(java.sql.Date和java.util.Date的理解)
package com.koal.test;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
strToDate();
dateToStr();
strToTimestamp();
timestampToStr();
timestampToDate();
dateToTimestamp();
}
/**string-->Date*/
@SuppressWarnings("deprecation")
public static void strToDate(){
String dateStr = "2010/02/01 12:34:23";
Date date = null;
//注意format的格式要和日期String的格式相匹配,大小寫是固定的。年用y不能用Y。
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
date = df.parse(dateStr);
System.out.println(date.toLocaleString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** Date->String
* 日期向字符串轉換,可以設置任意的轉換格式format
*/
@SuppressWarnings("deprecation")
public static void dateToStr (){
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
try {
dateStr = df1.format(date);
System.out.println(dateStr);
dateStr = df2.format(date);
System.out.println(dateStr);
//方法二
System.out.println(date.toLocaleString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**String ->Timestamp
* 注:String的類型必須形如: yyyy-MM-dd HH:mm:ss[.f...] 這樣的格式,中括號表示可選,否則報錯!!!
* 如果String爲其他格式,可考慮重新解析下字符串,再重組~~
*/
public static void strToTimestamp(){
String tsStr ="2011-05-09 11:49:45";
Timestamp ts = null;// new Timestamp(System.currentTimeMillis());
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
}
/**Timestamp ->String
* 使用Timestamp的toString()方法或者借用DateFormat
* */
@SuppressWarnings("deprecation")
public static void timestampToStr(){
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr ="";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//方法一使用DateFormat:因爲Date轉化爲String可應用DateFormat,Timestamp是Date的子類,所以可以用。
tsStr = df.format(ts);
System.out.println(tsStr);
//方法二
System.out.println(ts.toLocaleString());//本地時間
System.out.println(ts.toString()+"dd");//帶毫秒的時間
}
/**Timestamp ->Date*/
@SuppressWarnings("deprecation")
public static void timestampToDate(){
Timestamp ts = new Timestamp(System.currentTimeMillis());
//Timestamp 是Date的子類
Date date =null;
date = ts;
System.out.println(date.toLocaleString());
System.out.println(date.toString());
}
/** Date -->Timestamp
*/
//父類不能直接向子類轉化,可藉助中間的String,或者使用date.getTime();
@SuppressWarnings("deprecation")
public static void dateToTimestamp(){
Date date = new Date();
Timestamp ts = null;
ts = new Timestamp(date.getTime());
System.out.println(ts.toString());
//方法二,使用中間的String,不可以直接使用date.toString()。因爲那樣的格式不符合Timestamp.valueOf(),方法所
//要求的格式。
System.out.println(date.toLocaleString());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ts = Timestamp.valueOf(df.format(date));
System.out.println(ts.toString());
}
}
/**
* 關於java.sql.Date和java.util.Dat的立即
* java.sql.Date 只存儲日期數據不存儲時間數據
// 會丟失時間數據
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));
//可以這樣來處理
preparedStatement.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime()));
//想要得到完整的數據,包括日期和時間,可以這樣
java.util.Date d = resultSet.getTimestamp(1);
//這樣處理更合適一些,可以避免一些潛在Timestamp 問題
java.util.Date d = new java.util.Date(resultSet.getTimestamp(1).getTime());
自己補的話
這樣的話:
往數據庫存儲的時候可以接收 java.util.Date類型 再用getTime()方法得到代表那個Date對象的long值,再以這個long值 構造一個Timestamp對象 存進數據庫中。
從存數據庫裏取的時候,可以先得到Timestamp用他的getTime()方法得到long值,再以這個long值構造一個java.util.Date對象,這樣就可以對這個Date對象操作了。
不如說 new SimpleTimeFormat("yyyyy-MM-dd HH:mm:ss").format()等等
*/
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
strToDate();
dateToStr();
strToTimestamp();
timestampToStr();
timestampToDate();
dateToTimestamp();
}
/**string-->Date*/
@SuppressWarnings("deprecation")
public static void strToDate(){
String dateStr = "2010/02/01 12:34:23";
Date date = null;
//注意format的格式要和日期String的格式相匹配,大小寫是固定的。年用y不能用Y。
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
date = df.parse(dateStr);
System.out.println(date.toLocaleString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** Date->String
* 日期向字符串轉換,可以設置任意的轉換格式format
*/
@SuppressWarnings("deprecation")
public static void dateToStr (){
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
try {
dateStr = df1.format(date);
System.out.println(dateStr);
dateStr = df2.format(date);
System.out.println(dateStr);
//方法二
System.out.println(date.toLocaleString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**String ->Timestamp
* 注:String的類型必須形如: yyyy-MM-dd HH:mm:ss[.f...] 這樣的格式,中括號表示可選,否則報錯!!!
* 如果String爲其他格式,可考慮重新解析下字符串,再重組~~
*/
public static void strToTimestamp(){
String tsStr ="2011-05-09 11:49:45";
Timestamp ts = null;// new Timestamp(System.currentTimeMillis());
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
}
/**Timestamp ->String
* 使用Timestamp的toString()方法或者借用DateFormat
* */
@SuppressWarnings("deprecation")
public static void timestampToStr(){
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr ="";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//方法一使用DateFormat:因爲Date轉化爲String可應用DateFormat,Timestamp是Date的子類,所以可以用。
tsStr = df.format(ts);
System.out.println(tsStr);
//方法二
System.out.println(ts.toLocaleString());//本地時間
System.out.println(ts.toString()+"dd");//帶毫秒的時間
}
/**Timestamp ->Date*/
@SuppressWarnings("deprecation")
public static void timestampToDate(){
Timestamp ts = new Timestamp(System.currentTimeMillis());
//Timestamp 是Date的子類
Date date =null;
date = ts;
System.out.println(date.toLocaleString());
System.out.println(date.toString());
}
/** Date -->Timestamp
*/
//父類不能直接向子類轉化,可藉助中間的String,或者使用date.getTime();
@SuppressWarnings("deprecation")
public static void dateToTimestamp(){
Date date = new Date();
Timestamp ts = null;
ts = new Timestamp(date.getTime());
System.out.println(ts.toString());
//方法二,使用中間的String,不可以直接使用date.toString()。因爲那樣的格式不符合Timestamp.valueOf(),方法所
//要求的格式。
System.out.println(date.toLocaleString());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ts = Timestamp.valueOf(df.format(date));
System.out.println(ts.toString());
}
}
/**
* 關於java.sql.Date和java.util.Dat的立即
* java.sql.Date 只存儲日期數據不存儲時間數據
// 會丟失時間數據
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));
//可以這樣來處理
preparedStatement.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime()));
//想要得到完整的數據,包括日期和時間,可以這樣
java.util.Date d = resultSet.getTimestamp(1);
//這樣處理更合適一些,可以避免一些潛在Timestamp 問題
java.util.Date d = new java.util.Date(resultSet.getTimestamp(1).getTime());
自己補的話
這樣的話:
往數據庫存儲的時候可以接收 java.util.Date類型 再用getTime()方法得到代表那個Date對象的long值,再以這個long值 構造一個Timestamp對象 存進數據庫中。
從存數據庫裏取的時候,可以先得到Timestamp用他的getTime()方法得到long值,再以這個long值構造一個java.util.Date對象,這樣就可以對這個Date對象操作了。
不如說 new SimpleTimeFormat("yyyyy-MM-dd HH:mm:ss").format()等等
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.