积累的操作时间的工具类dateutil

来源:互联网 发布:淘宝客服晚班在家上班 编辑:程序博客网 时间:2024/04/30 00:22

dateutil之获取两个时间相差多少天和指定时间的前后N天日期

获取两个字符串类型的日期相差多少天,第一个时间比第二个时间小N天

public static int daysBetween(String smdate,String bdate) throws ParseException, java.text.ParseException{    SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT);        Calendar cal = Calendar.getInstance();          cal.setTime(sdf.parse(smdate));          long time1 = cal.getTimeInMillis();                       cal.setTime(sdf.parse(bdate));          long time2 = cal.getTimeInMillis();               long between_days=(time2-time1)/(1000*3600*24);        return Integer.parseInt(String.valueOf(between_days));   }

获取两个date类型的日期相差多少天,第一个时间比第二个时间小N天

/**      * 计算两个日期之间相差的天数      * @param smdate 较小的时间     * @param bdate  较大的时间     * @return 相差天数 * @throws ParseException  * @throws java.text.ParseException      */      public static int daysBetween(Date smdate,Date bdate) throws ParseException, java.text.ParseException      {      SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT);    smdate=sdf.parse(sdf.format(smdate));    bdate=sdf.parse(sdf.format(bdate));        Calendar cal = Calendar.getInstance();          cal.setTime(smdate);          long time1 = cal.getTimeInMillis();                       cal.setTime(bdate);          long time2 = cal.getTimeInMillis();               long between_days=(time2-time1)/(1000*3600*24);        return Integer.parseInt(String.valueOf(between_days));             }  

得到指定日期的前N天或者后N天  前N个月或者后N个月  前N秒或者后N秒   前N小时或者后N小时的字符串日期

/** * 得到指定日期的前后几天日期 * @param format  格式 * @return  字符串日期 */public static String beforeAfterNDaysDate(Date dt,int type,Integer day,String format) {Date dBefore = new Date();Calendar calendar = Calendar.getInstance(); // 得到日历calendar.setTime(dt);// 把当前时间赋给日历calendar.add(type, day); // 设置为前后N天dBefore = calendar.getTime(); // 得到前一天的时间SimpleDateFormat sdf = new SimpleDateFormat(format); // 设置时间格式String timed = sdf.format(dBefore); // 格式化前一天return timed;}

得到指定日期的最后一天

//得到指定日期的最后一天public static Date getLastDayOfMonth(Date dt) {Calendar calendar = Calendar.getInstance();calendar.setTime(dt);// 把当前时间赋给日历calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);return calendar.getTime();}


字符串转日期

/** * 字符串转日期 * @return Date */public static Date parseStringToDate(String thedate, String format) throws java.text.ParseException {DateFormat sdf = new SimpleDateFormat(format);Date dd1 = null;    if(thedate != null && !thedate.equals("")){dd1 = sdf.parse(thedate);    }return dd1;}

日期转字符串

/** * 日期转字符串 *  * @return String */public static String parseDateToString(Date thedate, String format) {DateFormat df = new SimpleDateFormat(format);if (thedate != null) {return df.format(thedate.getTime());}return "";}

将时间戳转化为时间

/** * 将时间戳转化为时间 * @param seconds * @param format * @return * @throws java.text.ParseException  */public static String timeStamp2Date(String seconds, String format) throws Exception {        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {            return "";        }        if (format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";        SimpleDateFormat sdf = new SimpleDateFormat(format);        String sd = sdf.format(new Date(Long.parseLong(seconds)));        return sd;    }





1 0
原创粉丝点击