package com.cw.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 时间工具类
*
* @author Wanglizhuo
* @date 2016年6月23日 下午2:48:27
* @comment
*/
public class DateUtil {
/**
* 把时间戳转换成日期格式的字符串
*
* @param format要转换成时间字符串的格式
* ,如yyyy-MM-dd或yyyy-MM-dd HH或yyyy-MM-dd HH:mm等
* @param time要转换的时间戳
* @return
*/
public static String transTimeStampToDate(String format, Long time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = new Date(time);
return sdf.format(date);
} catch (Exception e) {
return "";
}
}
/**
* 把时间格式的字符串转换成时间戳
*
* @param time
* 要转换的时间格式字符串
* @param format时间格式字符串的格式
* ,如yyyy-MM-dd或yyyy-MM-dd HH或yyyy-MM-dd HH:mm等
* @return
*/
public static long transDateToTimeStamp(String time, String format) {
Long date = 0L;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateTime;
dateTime = sdf.parse(time);
date = dateTime.getTime();
} catch (Exception e) {
}
return date;
}
/**
* 获得2个时间相差的月份
*
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static int getMonthSpace(String date1, String date2)
throws Exception {
int result = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));
result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
return result == 0 ? 1 : Math.abs(result);
}
/**
* 计算2个日期之间的月份
* @param date1
* @param date2
* @return
*/
public static int calculateMonthIn(Date date1, Date date2) {
Calendar cal1 = new GregorianCalendar();
cal1.setTime(date1);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(date2);
int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12
+ cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
return Math.abs(c);
}
/**
* 判断字符串是否符合日期格式 符合返回true ,不符合返回false
* @param str 字符串
* @param dateFormat 何种日期格式 如 yyyy/MM/dd,yyyy-MM-dd HH:mm:ss yyyy-MM等
* @return
*/
public static boolean isValidDate(String str,String dateFormat) {
boolean convertSuccess=true;
SimpleDateFormat format = new SimpleDateFormat(dateFormat); // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
try {
format.setLenient(false);// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.parse(str);
} catch (ParseException e) {
convertSuccess=false;
}
return convertSuccess;
}
/**
* 比较2个时间型字符串大小 后者大于等于前者返回true ,否则返回false
* @param startTime
* @param endTime
* @param dateFormat 何种日期格式 如 yyyy/MM/dd,yyyy-MM-dd HH:mm:ss yyyy-MM等
* @return
*/
public static boolean dateCompare(String startTime,String endTime,String dateFormat){
if(isValidDate(startTime,dateFormat) && isValidDate(endTime,dateFormat)){
long start = transDateToTimeStamp(startTime,dateFormat);
long end = transDateToTimeStamp(endTime,dateFormat);
return end>=start;
}
return false ;
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 时间工具类
*
* @author Wanglizhuo
* @date 2016年6月23日 下午2:48:27
* @comment
*/
public class DateUtil {
/**
* 把时间戳转换成日期格式的字符串
*
* @param format要转换成时间字符串的格式
* ,如yyyy-MM-dd或yyyy-MM-dd HH或yyyy-MM-dd HH:mm等
* @param time要转换的时间戳
* @return
*/
public static String transTimeStampToDate(String format, Long time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = new Date(time);
return sdf.format(date);
} catch (Exception e) {
return "";
}
}
/**
* 把时间格式的字符串转换成时间戳
*
* @param time
* 要转换的时间格式字符串
* @param format时间格式字符串的格式
* ,如yyyy-MM-dd或yyyy-MM-dd HH或yyyy-MM-dd HH:mm等
* @return
*/
public static long transDateToTimeStamp(String time, String format) {
Long date = 0L;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateTime;
dateTime = sdf.parse(time);
date = dateTime.getTime();
} catch (Exception e) {
}
return date;
}
/**
* 获得2个时间相差的月份
*
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static int getMonthSpace(String date1, String date2)
throws Exception {
int result = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(sdf.parse(date1));
c2.setTime(sdf.parse(date2));
result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH);
return result == 0 ? 1 : Math.abs(result);
}
/**
* 计算2个日期之间的月份
* @param date1
* @param date2
* @return
*/
public static int calculateMonthIn(Date date1, Date date2) {
Calendar cal1 = new GregorianCalendar();
cal1.setTime(date1);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(date2);
int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12
+ cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
return Math.abs(c);
}
/**
* 判断字符串是否符合日期格式 符合返回true ,不符合返回false
* @param str 字符串
* @param dateFormat 何种日期格式 如 yyyy/MM/dd,yyyy-MM-dd HH:mm:ss yyyy-MM等
* @return
*/
public static boolean isValidDate(String str,String dateFormat) {
boolean convertSuccess=true;
SimpleDateFormat format = new SimpleDateFormat(dateFormat); // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
try {
format.setLenient(false);// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.parse(str);
} catch (ParseException e) {
convertSuccess=false;
}
return convertSuccess;
}
/**
* 比较2个时间型字符串大小 后者大于等于前者返回true ,否则返回false
* @param startTime
* @param endTime
* @param dateFormat 何种日期格式 如 yyyy/MM/dd,yyyy-MM-dd HH:mm:ss yyyy-MM等
* @return
*/
public static boolean dateCompare(String startTime,String endTime,String dateFormat){
if(isValidDate(startTime,dateFormat) && isValidDate(endTime,dateFormat)){
long start = transDateToTimeStamp(startTime,dateFormat);
long end = transDateToTimeStamp(endTime,dateFormat);
return end>=start;
}
return false ;
}
}