今天用到了时间语字符串的转换,自己总结了一下一些常用的转换:
/**
* 时间转换工具类
*
* @author Administrator
*
*/
public class TimeUtils {
/**
* 时间戳转成字符串
*/
public static String SimpleString(Long Time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long time = new Long(Time);
String d = format.format(time);
return d;
}
public static String SimpleString1(Long Time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Long time = new Long(Time);
String d = format.format(time);
return d;
}
/**
* 时间戳转成Date
*/
public static Date SimpleDate(Long Time) {
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long time = new Long(Time);
String d = format.format(time);
try {
date = format.parse(d);
} catch (ParseException e) {
try {
date = format.parse(TimeUtils.SimpleString(System
.currentTimeMillis()));
} catch (ParseException e1) {
e1.printStackTrace();
}
}
return date;
}
/**
* 字符串转成时间戳
*/
public static Long StringSimple(String Time){
Date date=null;
long simpleTime=0;
SimpleDateFormat format = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
try {
date = format.parse(Time);
simpleTime=date.getTime();
} catch (ParseException e) {
simpleTime=System.currentTimeMillis();
}
return simpleTime;
}
}