当前页面: 开发资料首页 → JSP 专题 → 字符型如何转换为日期型,日期型如何转换为字符型
字符型如何转换为日期型,日期型如何转换为字符型
摘要: 字符型如何转换为日期型,日期型如何转换为字符型
字符型如何转换为日期型,日期型如何转换为字符型
有字符串啊,你用字符串和日期进行转换,之后在把字符串与字符进行转换。OK?
请帮忙
package com.sohu.blog.dennis_zane.util;
import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 日期Util类
*
* @author calvin
*/
public class DateUtil {
private static String defaultDatePattern = "yyyy-MM-dd";
/**
* 获得默认的 date pattern
*/
public static String getDatePattern() {
return defaultDatePattern;
}
/**
* 返回预设Format的当前日期字符串
*/
public static String getToday() {
Date today = new Date();
return format(today);
}
/**
* 使用预设Format格式化Date成字符串
*/
public static String format(Date date) {
return date == null ? "" : format(date, getDatePattern());
}
/**
* 使用参数Format格式化Date成字符串
*/
public static String format(Date date, String pattern) {
return date == null ? "" : new SimpleDateFormat(pattern).format(date);
}
/**
* 使用预设格式将字符串转为Date
*/
public static Date parse(String strDate) throws ParseException {
return StringUtils.isBlank(strDate) ? null : parse(strDate, getDatePattern());
}
/**
* 使用参数Format将字符串转为Date
*/
public static Date parse(String strDate, String pattern) throws ParseException {
return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(pattern).parse(strDate);
}
/**
* 在日期上增加数个整月
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
}