Java日期时间工具类集合
先上日期字符格式
public static final String DefaultDateFormat = "yyyy-MM-dd";
public static final String dateFormatSimple = "yyyyMMdd";
public static final String defaultDateTimeFormat = "yyyy-MM-dd HH:mm:ss";
1、转换日期为特定的格式字符串
public static String formatDate(Date date, String format) {
if (date == null){
return null;
}
SimpleDateFormat sdf;
try {
sdf = new SimpleDateFormat(format);
return sdf.format(date);
} catch (Exception e) {
return null;
}
}
2、String时间字符串转换为Timestamp时间戳
public static Timestamp stringToTimestamp(String dateTimeStr, String dateFormat) {
SimpleDateFormat sdf;
try {
sdf = new SimpleDateFormat(dateFormat);
return new Timestamp(sdf.parse(dateTimeStr).getTime());
} catch (Exception e) {
logger.error("转换失败!" + e.getMessage(), e);
return null;
}
}
3、验证日期是否有效
public static boolean isValidateDate(String date, String dateFormat) {
SimpleDateFormat df = new SimpleDateFormat(dateFormat);
/**
* 是否严格解析日期,如果 1/55/1999这种格式是错误日期格式,setLenient(true)时会自动转化为2003年的日期,55个月为4年
* 如果设置为false,则会抛异常,不解析格式,即这种日期格式是错误的
*/
df.setLenient(false);
try {
df.parse(date);
} catch (ParseException e) {
return false;
}
return true;
}
4、**将String字符时间按照指定格式转化为Date时间**
public static Date parseDate(String dateStr, String dateFormat) {
SimpleDateFormat sdf;
try {
sdf = new SimpleDateFormat(dateFormat);
return sdf.parse(dateStr);
} catch (Exception e) {
throw new RuntimeException("日期格式不正确");
}
}
5、获取当前日期 n天后的日期
public static String getNextNDate(int n) {
try {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, n);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(cal.getTime());
} catch (Exception e) {
throw new RuntimeException("进行日期运算时输入的参数不符合系统规格." + e);
}
}
6、获取指定日期n天后的日期
public static Date getNextNDate(Date date, int n,String format) {
try {
String dateStr = DateFormatUtil.format(date, defaultDateFormat);
Calendar cal = Calendar.getInstance();
String year = dateStr.substring(0, 4);
String month = dateStr.substring(5, 7);
String day = dateStr.substring(8, 10);
cal.set(Calendar.YEAR, Integer.parseInt(year));
cal.set(Calendar.MONTH, Integer.parseInt(month) - 1); // Calendar 计算月份从0开始
cal.set(Calendar.DATE, Integer.parseInt(day));
cal.add(Calendar.DATE, n); // 做日期加法的运算
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(cal.getTime());
} catch (Exception e) {
throw new RuntimeException("进行日期运算时输入得参数不符合系统规格." + e);
}
}
7、获取指定日期n天前的日期
cal.add(Calendar.DATE, 0 - n); //其他代码相似,只需变成负数
8、获取指定日期前n个月、后n个月、前几年、后几年的日期
cal.add(Calendar.MONTH, 0 - n); // 只需在month这里做减法改变
cal.add(Calendar.MONTH, n); // 只需在month这里做加法改变
cal.add(Calendar.YEAR, n);
cal.add(Calendar.YEAR, 0 - n);
9、日期比较
public static int compareDate(String DATE1, String DATE2) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
10、计算2个日期之间相差天数
直接通过计算两个日期的毫秒数,他们的差除以一天的毫秒数,即可得到我们想要的两个日期相差的天数
public static int differentDaysByMillisecond(Date date1,Date date2)
{
int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
return days;
}
还没有评论,来说两句吧...