Java中的类型转化是件很平常的事,尤其是时间格式的转换,很频繁,很麻烦,于是总结了一个工具类TimeUtils.java,里面写了一些静态方法,主要涉及:util.Date < - > String ,util.Date < - > sql.Date , String < - > Timestamp ,util.Date< - > Timestamp等之间的转换,也许可以帮到大家,有什么意见或者建议,可以写在回复里!That 's all , thanks !
package com.endy.string;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @auther endy.niu
* @create in 2011-06-15
*/
public class TimeUtils {
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYYIMMIDD = "yyyy/MM/dd";
public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";
/**
* if string1 is yyyy-MM-dd return yyyy/MM/dd , and vice versa
*/
public static String formatChange(String string1){
String string2 = null;
if(string1.contains("-")){
string2 = string1.replace('-', '/');
}else if(string1.contains("/")){
string2 = string1.replace('/', '-');
}
return string2;
}
/**
* String --> java.util.Date
*/
public static Date string2date(String string,String format) {
if (string == null || string.trim().length() == 0) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(string);
} catch (ParseException e) {
return null;
}
}
/**
* String --> java.util.Date ,the format is default yyyy-MM-dd
*/
public static Date string2date(String string) {
if (string == null || string.trim().length() == 0) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
try {
return sdf.parse(string);
} catch (ParseException e) {
return null;
}
}
/**
* java.util.Date --> String
*/
public static String date2string(Date date,String format) {
if (date == null) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
*java.util.Date -- > java.sql.Date
*/
public static java.sql.Date utilDate2sqlDate(Date uDate){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date sDate=new java.sql.Date(uDate.getTime());
return sDate;
}
/**
* java.sql.Date --> java.util.Date is auto.
*/
/**
* java.util.Date --> java.sql.Timestamp
*/
public static java.sql.Timestamp utilDate2sqlTimestamp(Date uDate){
Timestamp timestamp = new Timestamp(uDate.getTime());
return timestamp;
}
/**
*java.sql.Timestamp --> java.util.Date
*/
public static Date sqlTimestamp2utilDate(Timestamp timestamp){
//
Timestamp timestamp = new Timestamp(new Date().getTime());
String time=timestamp.toLocaleString();
//
System.out.println(time);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date=sdf.parse(time);
System.out.println(date);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* java.sql.Timestamp --> LocaleString not include nanos
*/
public static String sqlTimestamp2localeString(Timestamp timestamp){
String time=timestamp.toLocaleString();
return time;
}
/**
* java.sql.Timestamp --> String include nanos
*/
public static String sqlTimestamp2string(Timestamp timestamp){
String string = timestamp.toString();
return string;
}
/**
* String --> java.sql.Timestamp the format is "yyyy-MM-dd HH:mm:ss.nnnnnnn", nnn can omit.
*/
public static Timestamp string2sqlTimestamp(String time){
Timestamp timestamp = Timestamp.valueOf(time);
System.out.println(timestamp.toString());
return timestamp;
}
/**
* compare two util.Date ,if date1 is earlier return -1,equal return 0, later return 1
*/
public static int compareDate(Date date1,Date date2){
int result = date1.compareTo(date2);
return result;
}
/**
* compare two sql.Timestamp ,if timestamp1 is earlier return -1,equal return 0, later return 1
*/
public static int compareTimestamp(Timestamp timestamp1,Timestamp timestamp2){
int result = timestamp1.compareTo(timestamp2);
return result;
}
public static void main(String[] args) {
//
Date date2 = TimeUtils.string2date("2010-05-05", YYYY_MM_DD);
//
Date date1 = new Date();
//
System.out.println(TimeUtils.compareDate(date1, date2));
System.out.println(TimeUtils.formatChange(TimeUtils.YYYY_MM_DD));
}
}