| | |
| | |
|
| | | import java.text.ParseException;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Calendar;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | |
|
| | | import org.yeshi.utils.entity.DateInfo;
|
| | |
|
| | | public class DateUtil {
|
| | | |
| | |
|
| | | public static String dateDiff(String startTime, String endTime) {
|
| | | |
| | | String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟" ;
|
| | |
|
| | | String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
|
| | | SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
| | | |
| | |
|
| | | try {
|
| | | long nm = 1000 * 60;// 一分钟的毫秒数
|
| | | long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
| | | long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
| | | |
| | |
|
| | | // 获得两个时间的毫秒时间差异
|
| | | long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
|
| | | |
| | |
|
| | | long day = diff / nd;// 计算差多少天
|
| | | long hour = diff % nd / nh;// 计算差多少小时
|
| | | long min = diff % nd % nh / nm;// 计算差多少分钟
|
| | | |
| | | datatime = day + "天" + hour + "小时" + min + "分钟" ;
|
| | | |
| | |
|
| | | datatime = day + "天" + hour + "小时" + min + "分钟";
|
| | |
|
| | | } catch (ParseException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | |
| | |
|
| | | return datatime;
|
| | | }
|
| | |
|
| | | public static String dateDiff2(Date startTime, Date endTime) throws Exception {
|
| | |
|
| | | String datatime = "0分";
|
| | |
|
| | | long nm = 1000 * 60;// 一分钟的毫秒数
|
| | | long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
| | | long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
| | |
|
| | | // 获得两个时间的毫秒时间差异
|
| | | long diff = endTime.getTime() - startTime.getTime();
|
| | | long day = diff / nd;// 计算差多少天
|
| | | long hour = diff % nd / nh;// 计算差多少小时
|
| | | long min = diff % nd % nh / nm;// 计算差多少分钟
|
| | |
|
| | | if (day > 0) {
|
| | | datatime = day + "天" + hour + "时" + min + "分";
|
| | | } else {
|
| | | if (hour > 0 ) {
|
| | | datatime = hour + "时" + min + "分";
|
| | | } else {
|
| | | if (min < 0)
|
| | | min = 0;
|
| | | datatime = min + "分";
|
| | | }
|
| | | }
|
| | | return datatime;
|
| | | }
|
| | |
|
| | |
|
| | | |
| | | public static String dateDiff2(Date startTime, Date endTime) throws Exception{
|
| | | |
| | | String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟" ;
|
| | | |
| | | public static DateInfo dateDiff3(long startTime, long endTime) throws Exception {
|
| | | long nm = 1000 * 60;// 一分钟的毫秒数
|
| | | long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
| | | long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
| | |
|
| | | // 获得两个时间的毫秒时间差异
|
| | | long diff = endTime.getTime() - startTime.getTime();
|
| | | |
| | | long day = diff / nd;// 计算差多少天
|
| | | long diff = endTime - startTime;
|
| | | |
| | | int day = (int) (diff / nd);// 计算差多少天
|
| | | long hour = diff % nd / nh;// 计算差多少小时
|
| | | long min = diff % nd % nh / nm;// 计算差多少分钟
|
| | | |
| | | |
| | | datatime = day + "天" + hour + "小时" + min + "分钟" ;
|
| | | |
| | | |
| | | return datatime;
|
| | | long minute = diff % nd % nh / nm;// 计算差多少分钟
|
| | | long second =(diff/1000-day*24*60*60-hour*60*60-minute*60); // 计算差多少秒
|
| | |
|
| | | DateInfo dateInfo = new DateInfo();
|
| | | dateInfo.setDay(day);
|
| | | dateInfo.setHour(hour);
|
| | | dateInfo.setMinute(minute);
|
| | | dateInfo.setSecond(second);
|
| | | return dateInfo;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 通过时间秒毫秒数判断两个时间的间隔
|
| | | * |
| | | * @param date1
|
| | | * @param date2
|
| | | * @return
|
| | | */
|
| | | public static int differentDaysByMillisecond(Date start, Date end) {
|
| | | return (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
|
| | | }
|
| | |
|
| | | public String transferLongToDate(String dateFormat, Long millSec) {
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | Date date = new Date(millSec);
|
| | | return sdf.format(date);
|
| | | }
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | Date date = new Date(millSec);
|
| | | return sdf.format(date);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 验证是否属于同一天
|
| | |
| | | }
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 判断是否同一个月
|
| | | * @param date1
|
| | | * @param date2
|
| | | * @return
|
| | | */
|
| | | public static boolean isSameMonth(Date date1, Date date2) {
|
| | | Calendar calendar1 = Calendar.getInstance();
|
| | | calendar1.setTime(date1);
|
| | | Calendar calendar2 = Calendar.getInstance();
|
| | | calendar2.setTime(date2);
|
| | | int year1 = calendar1.get(Calendar.YEAR);
|
| | | int year2 = calendar2.get(Calendar.YEAR);
|
| | | int month1 = calendar1.get(Calendar.MONTH);
|
| | | int month2 = calendar2.get(Calendar.MONTH);
|
| | | System.out.println(year1 + " " + month1);
|
| | | System.out.println(year2 + " " + month2);
|
| | | return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);
|
| | | } |
| | | |
| | | |
| | | |
| | | /**
|
| | | * 指定日期加上天数后的日期
|
| | | * |
| | | * @param num 增加的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String plusDay(int num, String nowDate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | Date currdate = format.parse(nowDate);
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, num);
|
| | |
|
| | | return format.format(ca.getTime());
|
| | | }
|
| | |
|
| | | /**
|
| | | * 指定日期加上天数后的日期
|
| | | * |
| | | * @param num 增加的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String plusDay(int num, Date currdate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, num);
|
| | |
|
| | | return format.format(ca.getTime());
|
| | | }
|
| | | |
| | | /**
|
| | | * 指定日期加上天数后的日期
|
| | | * |
| | | * @param num 增加的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static Date plusDayDate(int num, Date currdate) {
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, num);
|
| | | return ca.getTime();
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * 指定日期减去天数后的日期
|
| | | * |
| | | * @param num 减去的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String reduceDay(int num, String nowDate) throws ParseException {
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | Date currdate = format.parse(nowDate);
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
|
| | |
|
| | | return format.format(ca.getTime());
|
| | | }
|
| | |
|
| | | /**
|
| | | * 指定日期减去天数后的日期
|
| | | * |
| | | * @param num 减去的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static Date reduceDay(int num, Date date) throws ParseException {
|
| | | // 设置要获取到什么样的时间
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | // 获取String类型的时间
|
| | | String date_str = format.format(date);
|
| | | Date currdate = format.parse(date_str);
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
|
| | |
|
| | | return ca.getTime();
|
| | | }
|
| | | |
| | | /**
|
| | | * 指定日期减去天数后的日期
|
| | | * |
| | | * @param num 减去的天数
|
| | | * @param nowDate 创建时间
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static String reduceDayTostring(int num, Date date) throws ParseException {
|
| | | // 设置要获取到什么样的时间
|
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
| | | // 获取String类型的时间
|
| | | String date_str = format.format(date);
|
| | | Date currdate = format.parse(date_str);
|
| | |
|
| | | Calendar ca = Calendar.getInstance();
|
| | | ca.setTime(currdate);
|
| | | ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
|
| | | return format.format(ca.getTime());
|
| | | }
|
| | |
|
| | | /**
|
| | | * 计算两个日期之间相差的天数
|
| | | * |
| | | * @param smdate 较小的时间
|
| | | * @param bdate 较大的时间
|
| | | * @return 相差天数
|
| | | * @throws ParseException calendar 对日期进行时间操作 getTimeInMillis() 获取日期的毫秒显示形式
|
| | | */
|
| | | public static int daysBetween(Date minDate, Date maxDate) throws ParseException {
|
| | | Calendar cal = Calendar.getInstance();
|
| | | cal.setTime(minDate);
|
| | | long minTime = cal.getTimeInMillis();
|
| | | cal.setTime(maxDate);
|
| | | long maxTime = cal.getTimeInMillis();
|
| | | long between_days = (maxTime - minTime) / (1000 * 3600 * 24);
|
| | | return Integer.parseInt(String.valueOf(between_days));
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 字符串日期格式的计算
|
| | | * |
| | | * @param smdate
|
| | | * @param bdate
|
| | | * @return 单位天数
|
| | | * @throws ParseException
|
| | | */
|
| | | public static int daysBetween2(Date minDate, Date maxDate) throws ParseException {
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
| | | return daysBetween(sdf.format(minDate), sdf.format(maxDate));
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 字符串日期格式的计算
|
| | | * |
| | | * @param smdate
|
| | | * @param bdate
|
| | | * @return
|
| | | * @throws ParseException
|
| | | */
|
| | | public static int daysBetween(String minDate, String maxDate) throws ParseException {
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
| | | Calendar cal = Calendar.getInstance();
|
| | | cal.setTime(sdf.parse(minDate));
|
| | | long minTime = cal.getTimeInMillis();
|
| | | cal.setTime(sdf.parse(maxDate));
|
| | | long maxTime = cal.getTimeInMillis();
|
| | | long between_days = (maxTime - minTime) / (1000 * 3600 * 24);
|
| | | return Integer.parseInt(String.valueOf(between_days));
|
| | | }
|
| | |
|
| | | /**
|
| | | * 返回中间日期
|
| | | * |
| | | * @param startTime
|
| | | * @param endTime
|
| | | * @return
|
| | | * @throws Exception
|
| | | */
|
| | | public static List<String> dayFactory(String startTime, String endTime) throws Exception {
|
| | |
|
| | | List<String> listDate = new ArrayList<String>();
|
| | |
|
| | | String plusDay = "";
|
| | | for (int i = 0; i < 1000; i++) {
|
| | | if (i == 0) {
|
| | | plusDay = startTime;
|
| | | } else {
|
| | | plusDay = DateUtil.plusDay(i, startTime);
|
| | | }
|
| | |
|
| | | listDate.add(plusDay);
|
| | |
|
| | | if (plusDay.equals(endTime)) {
|
| | | break; // 时间结束
|
| | | }
|
| | | }
|
| | | return listDate;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 随机减去几分钟
|
| | | * @param date
|
| | | * @param min 随机添加的分钟数
|
| | | * @return
|
| | | */
|
| | | public static Date reduceRandomMinute(Date date , int min) {
|
| | | long rand = (long) (Math.random() * 1000 * 60 * min);
|
| | | long time = date.getTime() - rand;
|
| | | return new Date(time);
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * @param args
|
| | | * @throws ParseException format() 对日期进行格式化处理 parse() 将日期设置为date类型
|
| | | */
|
| | | public static void main(String[] args) throws ParseException {
|
| | | // TODO Auto-generated method stub
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | Date d1 = sdf.parse("2016-09-08 00:00:00");
|
| | | Date d2 = sdf.parse("2016-09-08 00:00:00");
|
| | |
|
| | | System.out.println(daysBetween(d1, d2));
|
| | | System.out.println(daysBetween("2016-09-08 10:10:10", "2016-09-29 00:00:00"));
|
| | | }
|
| | |
|
| | | } |