Java 的Date操作保留 测试可以采用junitTest 或者main方法测试验证
public static String formatDate(java.util.Date date) {
return formatDateByFormat(date, "yyyy-MM-dd");
}
public static String formatDateByFormat(java.util.Date date, String format) {
String result = "";
if (date != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
result = sdf.format(date);
} catch (Exception ex) {
logger.error(ex.getMessage());
}
}
return result;
}
public static java.util.Date parseDate(java.sql.Date date) {
return date;
}
public static java.sql.Date parseSqlDate(java.util.Date date) {
if (date != null) {
return new java.sql.Date(date.getTime());
} else {
return null;
}
}
public static String format(java.util.Date date, String format) {
String result = "";
try {
if (date != null) {
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
result = df.format(date);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return result;
}
public static String format(java.util.Date date) {
if(null!=date){
return format(date, "yyyy/MM/dd");
}
return null;
}
public static String format1(java.util.Date date) {
if(null!=date){
return format(date, "yyyy-MM-dd");
}
return null;
}
public static int getYear(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.YEAR);
}
public static int getMonth(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MONTH) + 1;
}
public static int getDay(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.DAY_OF_MONTH);
}
public static int getHour(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.HOUR_OF_DAY);
}
public static int getMinute(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MINUTE);
}
public static int getSecond(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.SECOND);
}
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
public static int getWeek(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
int dayOfWeek = c.get(java.util.Calendar.DAY_OF_WEEK);
dayOfWeek = dayOfWeek - 1;
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
return dayOfWeek;
}
public static String getDate(java.util.Date date) {
return format(date, "yyyy/MM/dd");
}
public static String getDate(java.util.Date date,String formatStr) {
return format(date, formatStr);
}
public static String getTime(java.util.Date date) {
return format(date, "HH:mm:ss");
}
public static String getDateTime(java.util.Date date) {
return format(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 日期月份相加减
* @param date 具体的时间
* @param month 相加减的月份
* @return 处理后的时间
*/
public static Date addMonth(Date date, int month) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
return calendar.getTime();
}
/**
* 日期相加
* @param date Date
* @param day int
* @return Date
*/
public static java.util.Date addDate(java.util.Date date, int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/**
* 日期相减
* @param date Date
* @param date1 Date
* @return int
*/
public static int diffDate(java.util.Date date, java.util.Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
}
/**
* 日期相减(返回秒值)
* @param date Date
* @param date1 Date
* @return int
* @author
*/
public static Long diffDateTime(java.util.Date date, java.util.Date date1) {
return (Long) ((getMillis(date) - getMillis(date1))/1000);
}
public static java.util.Date getdate(String date) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(date);
}
public static java.util.Date getdate1(String date) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(date);
}
public static java.util.Date getMaxTimeByStringDate(String date) throws Exception {
String maxTime = date + " 23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(maxTime);
}
/**
* 获得当前时间
* @return
* @throws ServiceException
*/
public static Date getCurrentDateTime() throws Exception
{
Date date=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = DateUtil.getDateTime(date);
return sdf.parse(result);
}
public static String getCurrentDateTimeToStr() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(getCurrentDateTime());
}
public static String getCurrentDateTimeToStr2() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(getCurrentDateTime());
}
public static Long getWmsupdateDateTime() {
Calendar cl=Calendar.getInstance();
return cl.getTimeInMillis();
}
public static Integer getLeftSeconds(String date)throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date condition = sdf.parse(date);
long n = condition.getTime();
long s = sdf.parse(getCurrentDateTimeToStr2()).getTime();
// System.out.println("开始时间:"+date+"-->"+(int)((s-n)/1000));
return (int)((s-n)/1000);
}
/**
* 获得时间戳
* @return
* @throws Exception
*/
public static String getTime(){
Date date=new Date();
return String.valueOf(date.getTime());
}
/** 判断日期格式是否正确 */
public static boolean validateDate(String dateString){
//使用正则表达式 测试 字符 符合 dddd-dd-dd 的格式(d表示数字)
Pattern p = Pattern.compile("\\d{4}+[-]\\d{2}+[-]\\d{2}+");
Matcher m = p.matcher(dateString);
if(!m.matches()){
return false;
}
//得到年月日
String[] array = dateString.split("-");
int year = Integer.parseInt(array[0]);
int month = Integer.parseInt(array[1]);
int day = Integer.parseInt(array[2]);
if(month<1 || month>12){
return false;
}
int[] monthLengths = new int[]{0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear(year)){
monthLengths[2] = 29;
}else{
monthLengths[2] = 28;
}
int monthLength = monthLengths[month];
if(day<1 || day>monthLength){
return false;
}
return true;
}
/** 是否是闰年 */
public static boolean isLeapYear(int year){
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ;
}
/**
* 将时间格式的字符串,转换为时间
*
* @param str 时间格式的字符串
* @param pattern 转换格式
* @return java.util.Date
*/
public static Date parseToDate(String str, String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(str);
} catch (ParseException e) {
}
return null;
}
/**
* 将时间格式的字符串,转换为时间
*
* @param str 时间格式的字符串
* @param pattern 转换格式
* @return java.util.Date
*/
public static Date parseToDateWithThrowException(String str, String pattern) throws ParseException{
DateFormat df = new SimpleDateFormat(pattern);
return df.parse(str);
}
public static Date getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, -1);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return cal.getTime();
}
public static Date getFirstDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, -1);
return cal.getTime();
}
public static String getFirstDayOfMonthStr(String yearMonth) {
int year = getYear(yearMonth);
int month = getMonth(yearMonth);
return getFirstDayOfMonthStr(year, month);
}
private static int getYear(String yearMonth) {
return Integer.parseInt(yearMonth.substring(0, 4));//get year
}
private static int getMonth(String yearMonth) {
return Integer.parseInt(yearMonth.substring(4, 6));//get month
}
public static String getLastDayOfMonthStr(int year, int month) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH,1);
cal.add(Calendar.DAY_OF_MONTH,-1);
return sdf.format(cal.getTime());
}
public static String getLastDayOfMonthStr(String yearMonth) {
int year = getYear(yearMonth);
int month = getMonth(yearMonth);
return getLastDayOfMonthStr(year, month);
}
public static String getFirstDayOfMonthStr(int year, int month) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE));
return sdf.format(cal.getTime());
}
/**
* 返回当月最后一天的日期
*/
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = convert(date);
calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
return calendar.getTime();
}
/**
* 返回当月最后一天的日期
*/
public static Date getFirstDayOfMonth(Date date) {
Calendar calendar = convert(date);
calendar.set(Calendar.DATE, calendar.getMinimum(Calendar.DATE));
return calendar.getTime();
}
/**
* 将日期转换为日历
* @param date 日期
* @return 日历
*/
private static Calendar convert(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
public static Date getDayOfThisMonth(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.add(Calendar.MONTH, -1);
return cal.getTime();
}
/**
* 获得指定日期的前一天
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getCurrentDayBefore(Date date,String format) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = new SimpleDateFormat(format).format(c.getTime());
return dayBefore;
}
/**
* 检查日期时间段是否重叠
* @param checkDate
* @param startDate
* @param endDate
* @return
*/
public static boolean checkDateRepeart(Date checkStartDate,Date checkEndtDate,Date startDate,Date endDate){
if(checkStartDate.getTime()==startDate.getTime() ||checkStartDate.getTime()==endDate.getTime()){
return true;
}
if(checkEndtDate.getTime()==startDate.getTime() ||checkEndtDate.getTime()==endDate.getTime()){
return true;
}
if(checkStartDate.getTime()>startDate.getTime() && checkStartDate.getTime()<endDate.getTime()){
return true;
}
if(checkEndtDate.getTime()>startDate.getTime() && checkEndtDate.getTime()<endDate.getTime()){
return true;
}
return false;
}
//得到本月的第一天
public static String getMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
SimpleDateFormat firstDay= new SimpleDateFormat("yyyy-MM-dd");
return firstDay.format(calendar.getTime());
}
//得到本月的最后一天
public static String getMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat lastDay= new SimpleDateFormat("yyyy-MM-dd");
return lastDay.format(calendar.getTime());
}
// 得到上月的第一天
public static String getPerMonthFirstDay() {
Calendar cal = Calendar.getInstance();
// 上个月
cal.add(Calendar.MONTH, -1);
// 设置为1号,当前日期既为本月第一天
cal.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
String firstDay = sm.format(cal.getTime());
return firstDay;
}
// 得到上月的最后一天
public static String getPerMonthLastDay() {
Calendar call = Calendar.getInstance();
// 设置上个月最后一天
call.set(Calendar.DAY_OF_MONTH, 0);
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
String lastDay = sm.format(call.getTime());
return lastDay;
}
// 得到下个月的第一天
public static String getNextMonthFirstDate(String dateStr){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nextMonthFirstDate = "";
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 1);
nextMonthFirstDate = sdf.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return nextMonthFirstDate;
}