package org.jeecg.modules.dnc.utils.date;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
/**
|
* Created by Administrator on 2015/8/28.
|
*/
|
@Slf4j
|
public class DateUtil {
|
public static final String STR_DATE = "yyyy-MM-dd";
|
public static final String STR_DATE_STRING = "yyyy_MM_dd";
|
public static final String STR_YEAR_MONTH = "yyyy-MM";
|
public static final String STRDATE = "yyyyMMdd";
|
public static final String STR_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
|
public static final String STR_DATE_TIME_SMALL = "yyyy-MM-dd HH:mm:ss";
|
public static final String STR_DATE_TIME_MIN = "yyyy-MM-dd HH:mm";
|
public static final String STR_DATE_TIME_HOUR = "yyyy-MM-dd HH";
|
public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS";
|
public static final String STR_YEARMONTH = "yyyyMM";
|
public static final String STR_YEARMONTHDAY = "yyyyMMdd";
|
public static final String STR_YEAR = "yyyy";
|
|
/**
|
* 获取当前时间
|
* @return
|
*/
|
public static Date getNow() {
|
return new Date(System.currentTimeMillis());
|
}
|
|
/**
|
* <p>
|
* Description: 去掉日期时间中的时间部分
|
* </p>
|
* 如: 2013-11-11 18:56:33 ---> 2013-11-11 00:00:00
|
*
|
* @param date 需要修改的时间
|
* @return 修改后的时间
|
*/
|
public static Date removeTime(Date date) {
|
Date result = null;
|
try {
|
SimpleDateFormat df = new SimpleDateFormat(STR_DATE);
|
String dateStr = df.format(date);
|
result = df.parse(dateStr);
|
} catch (ParseException e) {
|
log.error(e.getMessage(), e.getStackTrace());
|
return null;
|
}
|
return result;
|
}
|
|
/**
|
* <p>
|
* Description: 按指定格式获取时间字符串
|
* </p>
|
*
|
* @param date 要转换的日期
|
* @param format 格式,例如:yyyy-MM-dd HH:mm:ss.SSS
|
* @return 转换后的时间字符串
|
*/
|
public static String format(Date date, String format) {
|
SimpleDateFormat df = new SimpleDateFormat(format);
|
return df.format(date);
|
}
|
|
/**
|
* 获取日期的天字符串
|
* @param date
|
* @return
|
*/
|
public static String getDayStr(Date date) {
|
Calendar calendar = Calendar.getInstance(); // 得到日历
|
calendar.setTime(date);// 把当前时间赋给日历
|
int day = calendar.get(Calendar.DATE);//获取日
|
String dayStr = day < 10 ? "0" + day : day + "";
|
return dayStr;
|
}
|
|
/**
|
* 获取日期的年份字符串
|
* @param date
|
* @return
|
*/
|
public static String getYearStr(Date date) {
|
Calendar calendar = Calendar.getInstance(); // 得到日历
|
calendar.setTime(date);// 把当前时间赋给日历
|
String yearStr = calendar.get(Calendar.YEAR) + "";//获取日
|
return yearStr;
|
}
|
|
/**
|
* 获取日期的月份字符串
|
* @param date
|
* @return
|
*/
|
public static String getMonthStr(Date date) {
|
Calendar calendar = Calendar.getInstance(); // 得到日历
|
calendar.setTime(date);// 把当前时间赋给日历
|
int month = calendar.get(Calendar.MONTH);//获取日
|
String monthStr = month < 10 ? "0" + month : month + "";
|
return monthStr;
|
}
|
|
/**
|
* 获取当前时间 前后i天
|
* 精确到秒
|
* @param i 为正数代表加i天,为负数代表减i天
|
* @return
|
*/
|
public static Date addDay(Date date, int i){
|
Calendar calendar = Calendar.getInstance(); // 得到日历
|
calendar.setTime(date);// 把当前时间赋给日历
|
calendar.add(Calendar.DAY_OF_MONTH, i); // 设置天数加减
|
return calendar.getTime();
|
}
|
|
/**
|
* 获取当前时间 前后i秒
|
* 精确到秒
|
* @param i 为正数代表加i秒,为负数代表减i秒
|
* @return
|
*/
|
public static Date addSeconds(Date date, int i){
|
Calendar calendar = Calendar.getInstance(); // 得到日历
|
calendar.setTime(date);// 把当前时间赋给日历
|
calendar.add(Calendar.SECOND, i); // 设置秒数加减
|
return calendar.getTime();
|
}
|
|
public static Date toDate(String date, String format) {
|
SimpleDateFormat df = new SimpleDateFormat(format);
|
try {
|
return df.parse(date);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|