现在的位置: 首页 > 综合 > 正文

JAVA中的时间处理

2013年08月08日 ⁄ 综合 ⁄ 共 1034字 ⁄ 字号 评论关闭
//定义一个时间格式变量
private static final String DEFAULT_PATTERN = "yyyyMMddHHmmss";

/**
* author 郝学武
* 日期计算后返回规定格式的时间字符串
*
* @param interval
* 天数
* @param pattern
* 时间格式
* @return
*/
public static String getDate(String interval,Date starttime, String pattern) {
Calendar temp = Calendar.getInstance(TimeZone.getDefault());
temp.setTime(starttime);
temp.add(temp.MONTH, Integer.parseInt(interval));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(temp.getTime());
}
/**
* author 郝学武
* 将字符串类型转换为Date类型
* @return
*/
public static Date str2Date(String str) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
d = sdf.parse(str+"000000");
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
/**
* author 郝学武
* 将时间格式化
* @return
*/
public static Date DatePattern(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
String dd=sdf.format(date);
date = str2Date(dd);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* author 郝学武
* 将Date转换成相应的字符串
* @return
*/
public static String date2Str(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
return sdf.format(date);
}
 

抱歉!评论已关闭.