场景:
java的calendar,Date,Timestamp与字符串相互转换
1. 示例
/**
* 1.使用Calendar获取格式化的日期时间 根据格式化字符串获取时间
* */
public static String getFormatDateTime(Calendar calendar, String format) {
if (calendar != null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(calendar.getTime());
}
return null;
}
/**
* 2.使用Date获取格式化的日期时间 根据格式化字符串获取时间
* */
public static String getFormatDateTime(java.util.Date date, String format) {
if (date != null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date.getTime());
}
return null;
}
/**
* 3.使用Date获取格式化的日期时间 根据格式化字符串获取时间
* */
public static String getFormatDateTime(java.sql.Timestamp date,
String format) {
if (date != null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date.getTime());
}
return null;
}
/**
* 4.将时间字符串按照格式转换为Timestamp时间
* */
public static Timestamp strToTimestamp(String dateStr,
String simpleDateFormat) {
if (dateStr != null && !dateStr.trim().equals("")) {
try {
if (simpleDateFormat == null
|| simpleDateFormat.trim().equals("")) {
simpleDateFormat = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat dateFormat = new SimpleDateFormat(
simpleDateFormat);
return new Timestamp(dateFormat.parse(dateStr).getTime());
} catch (Exception e) {
logger.error("", e);
}
}
return null;
}
/**
* 5.将时间字符串按照格式转换为Date时间
* */
public static java.util.Date strToDate(String dateStr,
String simpleDateFormat) throws Exception {
if (dateStr != null && !dateStr.trim().equals("")) {
if (simpleDateFormat == null || simpleDateFormat.trim().equals("")) {
simpleDateFormat = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat dateFormat = new SimpleDateFormat(simpleDateFormat);
return dateFormat.parse(dateStr);
}
return null;
}
/**
* 6.根据具体指定时区获取Calendar "GMT+8"
* */
public static Calendar getCalendarInstance(String timeZone) {
Calendar cCreateTime = Calendar.getInstance();
long needTimeRowOff = TimeZone.getTimeZone(timeZone).getRawOffset();
long defaultTimeRowOff = TimeZone.getDefault().getRawOffset();
if (needTimeRowOff != defaultTimeRowOff) {
cCreateTime.setTimeZone(TimeZone.getTimeZone(timeZone));
}
return cCreateTime;
}
/**
* 7.根据指定时间字符串获取date
* */
public static Date parseDate(String str) {
if ((str == null) || (str.equals(""))) {
return null;
}
Date dt = null;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
dt = new Date(dateFormat.parse(str).getTime());
} catch (ParseException e) {
logger.error("", e);
}
return dt;
}
/**
* 8.时间字符串转换为date
* */
public static Date parseDate(String str, String pattern) {
if ((str == null) || (str.equals(""))) {
return null;
}
Date date = null;
DateFormat dateFormat = new SimpleDateFormat(pattern);
try {
date = new Date(dateFormat.parse(str).getTime());
} catch (ParseException e) {
logger.error("", e);
}
return date;
}
/** 9.将date转换为指定格式的时间 */
public static String toString(Date date, String pattern) {
if (date == null)
return null;
DateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
}
/** 10.将Timestamp转换为指定格式的时间 */
public static String toString(Timestamp timestamp, String pattern) {
if (timestamp == null)
return null;
DateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(timestamp);
}
以上,TKS.