注:
本文的Date为java.util包下的类,即java.util.Date的缩写;TimeStamp为java.sql包下的类,即java.sql.Timestamp的缩写
String与Date互转
String转为Date
主要代码
Date date= dateFormat.parse(String dateString);
测试
/**
* 详述:String转为Date
* @author nowuseeme
*/
@Test
public void Str2Date(){
String dateStr = "2017/02/21 19:34:23";
Date date = null;
//注意format的格式要与日期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
date = sdf.parse(dateStr);
System.out.println(date.toString()); //Tue Feb 21 19:34:23 CST 2017
} catch (Exception e) {
e.printStackTrace();
}
}
Date转为String
主要代码
String str = dateFormat.format(Date date);
测试
@Test
public void Date2Str(){
Date date = new Date();
String dateStr = "";
//format的格式可以任意
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
dateStr = sdf.format(date);
System.out.println(dateStr); //2017/02/21 19:52:01
dateStr = sdf2.format(date);
System.out.println(dateStr); //2017-02-21 19/52/01
}
String与Timestamp互转
String转为Timestamp
主要代码
Timestamp timestamp = Timestamp.valueOf("yyyy-mm-dd hh:mm:ss[.f...]");
测试
/**
* 详述:String转为Timestamp
* @author nowuseeme
*/
@Test
public void Str2Timestamp(){
//String的类型必须为 yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,否则报错
Timestamp ts = new Timestamp(System.currentTimeMillis());
System.out.println(ts); //2017-02-21 19:50:50.901
String tsStr = "2017-02-21 20:49:45";
ts = Timestamp.valueOf(tsStr);
System.out.println(ts); //2017-02-21 20:49:45.0
}
Timestamp转为String
主要代码
String str = dateFormat.format(Timestamp ts);
测试
/**
* 详述:Timestamp转为String
* @author nowuseeme
*/
@Test
public void Timestamp2Str(){
String tsStr = "";
Timestamp ts = new Timestamp(System.currentTimeMillis());
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//方法一
tsStr = sdf.format(ts);
System.out.println(tsStr); //2017/02/21 19:57:07
//方法二
System.out.println(ts.toString()); //2017-02-21 19:57:07.902
}
Date和Timestamp互转
Date转为Timestamp
思路
java.util.Date
是 java.sql.Timestamp
的父类 , 父类无法转为子类,借助String
或Long
完成
主要代码
Timestamp ts = new Timestamp(new Date().getTime());
测试
/**
* 详述:Date转为Timestamp
* @author nowuseeme
*/
@Test
public void Date2Timestamp(){
//java.util.Date 是 java.sql.Timestamp的父类 , 父类无法转为子类,借助String或Long完成
Timestamp ts = new Timestamp(new Date().getTime());
System.out.println(ts); //2017-02-21 20:24:06.211
}
Timestamp转为Date
思路
java.util.Date
是 java.sql.Timestamp
的父类 , 子类转父类直接转换
主要代码
Date date = new Timestamp(System.currentTimeMillis());
测试
/**
* 详述:Timestamp转为Date
* @author nowuseeme
*/
@Test
public void Timestamp2Date(){
//java.util.Date 是 java.sql.Timestamp的父类 , 子类转父类直接转换
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
System.out.println(date); //Tue Feb 21 20:19:53 CST 2017
date = ts;
System.out.println(date); //2017-02-21 20:19:53.905
}
Util工具类
DateUtil.java
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
/**
* 类名:DateUtil.java</br>
* 描述:日期转换工具类</br>
* @author nowuseeme</br>
*/
public class DateUtil {
// 日期格式,年份,例如:2017
public static final String DATE_FORMAT_YYYY = "yyyy";
// 日期格式,年份和月份,例如:201702
public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
// 日期格式,年月日,例如:20170221
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
// 日期格式,年月日时分,数字都连在一起,例如:201702213045
public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";
// 日期格式,年月日时分秒,数字都连在一起,例如:20170221304552
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
// 日期格式,年月日时分秒毫秒,例如:20001230120000123,20080808200808456
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSMILL = "yyyyMMddHHmmssSSS";
// 日期格式,年月日时分,例如:20001230 12:00,20080808 20:08
public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";
// 日期格式,年份和月份,用横杠(hyphen)分开,例如:2017-02
public static final String DATE_FORMAT_HYPHEN_YYYY_MM = "yyyy-MM";
// 日期格式,年月日,用横杠(hyphen)分开,例如:2017-02-21
public static final String DATE_FORMAT_HYPHEN_YYYY_MM_DD = "yyyy-MM-dd";
// 日期格式,年月日,用横杠(hyphen)分开,例如:17-02-21
public static final String DATE_FORMAT_HYPHEN_YY_MM_DD = "yy-MM-dd";
// 日期格式,年月日时分,,年月日用横杠(hyphen)分开,时分用冒号分开,例如:2017-02-21 20:20
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";
// 日期格式,年月日时分秒,年月日用横杠分开,时分秒用冒号分开,例如:2005-05-10 20:20:00
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
// 日期格式,年月日,年月日用中文分开,例如:2017年02月21日
public static final String DATE_TIME_FORMAT_CHN_YYYY_MM_DD = "yyyy年MM月dd日";
// 日期格式,年月日,年月日用点分开,例如:2017.02.21
public static final String DATE_FORMAT_POINT_YYYYMMDD = "yyyy.MM.dd";
/**
* 将Date转换为指定格式的时间字符串
* @author nowuseeme
*/
public static String parseDate2FormatStr(Date date, String timeFromat){
if(date==null || StringUtils.isBlank(timeFromat))
return null;
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(date);
}
/**
* 将Timestamp转换为指定格式的时间字符串
* @author nowuseeme
*/
public static String parseTimestamp2FormatStr(Timestamp timestamp,String timeFromat){
if(timestamp==null || StringUtils.isBlank(timeFromat))
return null;
DateFormat df = new SimpleDateFormat(timeFromat);
return df.format(timestamp);
}
/**
* 将时间字符串按照指定格式转为Date
* @author nowuseeme
*/
public static Date parseStr2Date(String time, String timeFromat){
return parseStr2Date(time, timeFromat, null);
}
/**
* 将时间字符串按照指定格式转为Date
* @author nowuseeme
*/
public static Date parseStr2Date(String time, String timeFromat, Date defaultDate){
if (StringUtils.isBlank(time) || StringUtils.isBlank(timeFromat))
return defaultDate;
try{
DateFormat dateFormat=new SimpleDateFormat(timeFromat);
return dateFormat.parse(time);
}catch(Exception e){
e.printStackTrace();
return defaultDate;
}
}
/**
* 将格式化的时间字符串转为Timestamp
* @param formattedStr 格式必须为 yyyy-mm-dd hh:mm:ss[.fffffffff]
* @author nowuseeme
*/
public static Timestamp formattedStr2Timestamp(String formattedStr){
if (StringUtils.isBlank(formattedStr))
return null;
try {
return Timestamp.valueOf(formattedStr);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将Timestamp转为时间字符串
* @param
* @author nowuseeme
*/
public static String parseTimestamp2Str(Timestamp timestamp){
return timestamp.toString();
}
/**
* 将Timestamp转为格式化的时间字符串
* @param
* @author nowuseeme
*/
public static String parseTimestamp2FormattedStr(Timestamp timestamp, String timeFromat){
return parseDate2FormatStr(timestamp, timeFromat);
}
/**
* 将Timestamp转为格式化的时间字符串
* @author nowuseeme
*/
public static Timestamp parseDate2Timestamp(Date date){
return new Timestamp(date.getTime());
}
}