日期处理工具类DateUtil

日期处理工具类DateUtil,date时间处理类,java 时间工具类,java date uitl
java处理时间的工具类,废话不多说,直接上代码!!可直接拷贝过去使用

package com.fis.sanx.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.apache.commons.lang.time.DateFormatUtils;
/**
 * 日期处理工具类
 *
 * @author shenglf
 *
 */
public class DateUtil {
    public static final String ISO_DATE_FORMAT = "yyyyMMdd";
    public static final String ISO_TIME_FORMAT = "HHmmss";
    public static final String ISO_DATETIME_FORMAT = "yyyyMMddHHmmss";
    public static final String CHN_DATE_FORMAT = "yyyy-MM-dd";
    public static final String CHN_TIME_FORMAT = "HH:mm:ss";
    public static final String CHN_TIME_FORMAT_SHORT = "MM-dd HH:mm";
    public static final String CHN_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String CHN_DATETIME_FORMAT_LONG = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String CHN_DATETIME_FORMAT_SHORT = "yyyy-MM-dd HH:mm";
    public static final String DB_DATETIME_FORMAT = "yyyy-mm-dd hh24:mi:ss";
    public static final String DB_ISO_DATE_FORMAT = "yyyyMMdd";
    public static final String DB_ISO_DATETIME_FORMAT = "yyyyMMddHH24miss";
    public static final String DB_CHN_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DB_CHN_DATETIME_FORMAT = "yyyy-MM-dd HH24:mi:ss";
    // public static final String DATE_FORMAT = "yyyy-MM-dd";
    // public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String getSysdateString() {
        return toString(new Date());
    }
    public static String getSysDatetime() {
        return toString(new Date(), CHN_DATETIME_FORMAT);
    }
    /**
     * 比较一个日期/时间是否在指定范围内
     *
     * @param curDay
     *            等比较对象
     * @param from
     *            指定范围开始时间
     * @param to
     *            指定范围结束时间
     * @return 如果在就是true否则为false
     */
    public static boolean isBetween(Date curDay, Date from, Date to) {
        if (curDay == null || from == null || to == null) return false;
        if (curDay.compareTo(from) >= 0 && curDay.compareTo(to) <= 0) return true;
        return false;
    }
    /**
     * 比较二个日期/时间
     *
     * @param curDay
     *            等比较对象1
     * @param oldDate
     *            等比较对象2
     * @return 整数
     */
    public static int compare(Date curDay, Date oldDate) {
        if (curDay == null || oldDate == null) return -1;
        return curDay.compareTo(oldDate);
    }
    /**
     * 计算两个时间之间的间隔
     *
     * @param date1
     *            时间一
     * @param date2
     *            时间二
     * @param unit
     *            间隔单位
     * @return 返回指定间隔单位而计算出的时间间隔
     */
    public static double dateDiff(Date date1, Date date2, int unit) {
        long diff = date1.getTime() - date2.getTime();
        if (unit == Calendar.SECOND) {
            return diff / 1000.0;
        } else if (unit == Calendar.MINUTE) {
            return diff / 1000.0 / 60.0;
        } else if (unit == Calendar.HOUR) {
            return diff / 1000.0 / 60.0 / 60.0;
        } else if (unit == Calendar.DATE) {
            return diff / 1000.0 / 60.0 / 60.0 / 24.0;
        } else {
            return 0;
        }
    }
    /**
     * 字符串按格式转换为SQL日期
     *
     * @param sdate
     *            源字符串
     * @param format
     *            转换的格式
     * @return
     */
    public static java.sql.Date toSqlDate(final String sdate, final String format) {
        Date date = toDate(sdate, format);
        if (date == null) {
            return null;
        } else {
            return new java.sql.Date(date.getTime());
        }
    }
    /**
     * 字符串按格式转换为SQL日期
     *
     * @param sdate
     *            源字符串
     * @param format
     *            转换的格式
     * @return
     */
    public static java.sql.Date toSqlDate(final String sdate) {
        Date date = toDate(sdate, CHN_DATE_FORMAT);
        if (date == null) {
            return null;
        } else {
            return new java.sql.Date(date.getTime());
        }
    }
    /**
     * 字符串按格式转换为SQL长日期
     *
     * @param sdate
     *            源字符串
     * @param format
     *            转换的格式
     * @return
     */
    public static java.sql.Timestamp toSqlDatetime(final String sdate, final String format) {
        Date date = toDate(sdate, format);
        if (date == null) {
            return null;
        } else {
            return new java.sql.Timestamp(date.getTime());
        }
    }
    /**
     * 将timestamp 毫秒数时间转化成相应的日期格式
     *
     * @param timestamp
     *            毫秒数时间
     * @param format
     *            转换的格式
     * @return
     */
    public static String toStrDateByTimestamp(final String timestamp, final String format) {
        Long s = Long.valueOf(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(s));
    }
    /**
     * 字符串按格式转换为特定时区的日期
     *
     * @param sdate
     *            源字符串
     * @param format
     *            转换的格式
     * @param timeZone
     *            时区
     * @return
     */
    public static Date toDate(final String sdate, final String format, final String timeZone) {
        if (sdate == null || sdate.trim().equals("")) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(format); // "yyyy-MM-dd'T'HH:mm:ss.SSS+08:00"
        df.setTimeZone(TimeZone.getTimeZone(timeZone)); // GMT/EDT/EST
        Date ret = null;
        try {
            ret = (Date) df.parseObject(sdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ret;
    }
    /**
     * 日期换成星期
     *
     * @param args
     */
    public static int dateToWeek(final Date sdate) {
        int weekint = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(sdate);
        if (calendar.get(calendar.DAY_OF_WEEK) == 1) {
            weekint = 6;
        }
        if (calendar.get(calendar.DAY_OF_WEEK) == 2) {
            weekint = 0;
        }
        if (calendar.get(calendar.DAY_OF_WEEK) == 3) {
            weekint = 1;
        }
        if (calendar.get(calendar.DAY_OF_WEEK) == 4) {
            weekint = 2;
        }
        if (calendar.get(calendar.DAY_OF_WEEK) == 5) {
            weekint = 3;
        }
        if (calendar.get(calendar.DAY_OF_WEEK) == 6) {
            weekint = 4;
        }
        if (calendar.get(calendar.DAY_OF_WEEK) == 7) {
            weekint = 5;
        }
        return weekint;
    }
    /**
     * 获得系统的当前时间,毫秒.
     *
     * @return
     */
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }
    /**
     * 获得系统的当前时间
     *
     * @return e.g.Thu Oct 12 10:25:14 CST 2006
     */
    public static Date getCurrentDate() {
        // return new Date(System.currentTimeMillis());
        return new Date(getCurrentTimeMillis());
    }
    /**
     * 获得系统当前日期时间,以默认格式显示
     *
     * @return e.g.2006-10-12 10:55:06
     */
    public static String getCurrentFormatDateTime1() {
        Date currentDate = getCurrentDate();
        SimpleDateFormat dateFormator = new SimpleDateFormat(DateUtil.CHN_DATE_FORMAT);
        return dateFormator.format(currentDate);
    }
    public static String getCurrentFormatDateTime(String dateFormat) {
        Date currentDate = getCurrentDate();
        SimpleDateFormat dateFormator = new SimpleDateFormat(dateFormat);
        return dateFormator.format(currentDate);
    }
    /**
     * 根据传入的时间比如月得到月份的英文缩写值
     */
    public static String getEnMonth(String stringDates) {
        String monthEn = "jan";
        if ("01".equalsIgnoreCase(stringDates)) {
            monthEn = "jan";
        }
        if ("02".equalsIgnoreCase(stringDates)) {
            monthEn = "feb";
        }
        if ("03".equalsIgnoreCase(stringDates)) {
            monthEn = "mar";
        }
        if ("04".equalsIgnoreCase(stringDates)) {
            monthEn = "apr";
        }
        if ("05".equalsIgnoreCase(stringDates)) {
            monthEn = "may";
        }
        if ("06".equalsIgnoreCase(stringDates)) {
            monthEn = "jun";
        }
        if ("07".equalsIgnoreCase(stringDates)) {
            monthEn = "jul";
        }
        if ("08".equalsIgnoreCase(stringDates)) {
            monthEn = "aug";
        }
        if ("09".equalsIgnoreCase(stringDates)) {
            monthEn = "sep";
        }
        if ("10".equalsIgnoreCase(stringDates)) {
            monthEn = "oct";
        }
        if ("11".equalsIgnoreCase(stringDates)) {
            monthEn = "nov";
        }
        if ("12".equalsIgnoreCase(stringDates)) {
            monthEn = "dec";
        }
        return monthEn;
    }
    // 某个时间的减去几个小时
    public static String getSubDate(Date dateTime, Integer num) {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(CHN_DATETIME_FORMAT);
        java.util.Calendar Cal = java.util.Calendar.getInstance();
        Cal.setTime(dateTime);
        Cal.add(java.util.Calendar.HOUR_OF_DAY, num);
        String returnDate = format.format(Cal.getTime());
        return returnDate;
    }
    /**
     * 字符串转换为日期java.util.Date
     *
     * @param dateText
     *            字符串
     * @param format
     *            日期格式
     * @return
     */
    public static Date toDate(String dateText, String format) {
        if (dateText == null) {
            return null;
        }
        DateFormat df = null;
        try {
            if (format == null) {
                df = new SimpleDateFormat();
            } else {
                df = new SimpleDateFormat(format);
            }
            df.setLenient(false);
            return df.parse(dateText);
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 日期增加
     *
     * @param isoString
     *            日期字符串
     * @param fmt
     *            格式
     * @param field
     *            年/月/日 Calendar.YEAR/Calendar.MONTH/Calendar.DATE
     * @param amount
     *            增加数量
     * @return
     * @throws ParseException
     */
    public static final String dateIncrease(String isoString, String fmt, int field, int amount) {
        try {
            Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
            cal.setTime(toDate(isoString, fmt));
            cal.add(field, amount);
            return toString(cal.getTime(), fmt);
        } catch (Exception ex) {
            return null;
        }
    }
    /**
     * 日期增加-按日增加
     *
     * @param date
     * @param days
     * @return java.util.Date
     */
    public static Date dateIncreaseByDay(Date date, int days) {
        Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }
    /**
     * 日期增加-按月增加
     *
     * @param date
     * @param months
     * @return java.util.Date
     */
    public static Date dateIncreaseByMonth(Date date, int months) {
        Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }
    /**
     * 日期增加
     *
     * @param date
     *            日期字符串 yyyy-MM-dd
     * @param days
     * @return 日期字符串 yyyy-MM-dd
     */
    public static String dateIncreaseByDay(String date, int days) {
        return dateIncreaseByDay(date, ISO_DATE_FORMAT, days);
    }
    /**
     * 日期增加
     *
     * @param date
     *            日期字符串
     * @param fmt
     *            日期格式
     * @param days
     * @return
     */
    public static String dateIncreaseByDay(String date, String fmt, int days) {
        return dateIncrease(date, fmt, Calendar.DATE, days);
    }
    /**
     * 根据时间变量返回时间字符串
     *
     * @return 返回时间字符串
     * @param pattern
     *            时间字符串样式
     * @param date
     *            时间变量
     */
    public static String toString(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
            sfDate.setLenient(false);
            return sfDate.format(date);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 根据时间变量返回时间字符串 yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String toString(Date date) {
        return toString(date, CHN_DATE_FORMAT);
    }
    public static String datetimeToString(Date date) {
        return toString(date, CHN_DATETIME_FORMAT);
    }
    /**
     * transform the formatted string "YYYY-MM-DD" into "YYYYMMDD"
     *
     * @param str
     * @return
     */
    public static String dateToStr(String date) {
        if (date == null) return "";
        String[] str = date.split("-");
        if (str.length < 3) return date;
        return str[0] + (str[1].length() > 1 ? str[1] : "0" + str[1]) + (str[2].length() > 1 ? str[2] : "0" + str[2]);
    }
    /**
     * Returns the days between two dates. Positive values indicate that the
     * second date is after the first, and negative values indicate, well, the
     * opposite. Relying on specific times is problematic.
     *
     * @param early
     *            the "first date"
     * @param late
     *            the "second date"
     * @return the days between the two dates
     */
    public static final int daysBetween(Date early, Date late) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(early);
        c2.setTime(late);
        return daysBetween(c1, c2);
    }
    /**
     * Returns the days between two dates. Positive values indicate that the
     * second date is after the first, and negative values indicate, well, the
     * opposite.
     *
     * @param early
     * @param late
     * @return the days between two dates.
     */
    public static final int daysBetween(Calendar early, Calendar late) {
        int betweenDays = 0;
        betweenDays = late.get(Calendar.DAY_OF_YEAR) - early.get(Calendar.DAY_OF_YEAR);
        int betweenYears = late.get(Calendar.YEAR) - early.get(Calendar.YEAR);
        betweenDays = late.get(Calendar.DAY_OF_YEAR) - early.get(Calendar.DAY_OF_YEAR);
        for (int i = 0; i < betweenYears; i++) {
            early.set(Calendar.YEAR, (early.get(Calendar.YEAR) + 1));
            betweenDays = betweenDays + early.getActualMaximum(Calendar.DAY_OF_YEAR);
        }
        return betweenDays;
    }
    /**
     * Return a Julian date based on the input parameter. This is based from
     * calculations found at <a
     * href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day
     * Calculations (Gregorian Calendar)</a>, provided by Bill Jeffrys.
     *
     * @param c
     *            a calendar instance
     * @return the julian day number
     */
    public static final float toJulian(Calendar c) {
        int Y = c.get(Calendar.YEAR);
        int M = c.get(Calendar.MONTH);
        int D = c.get(Calendar.DATE);
        int A = Y / 100;
        int B = A / 4;
        int C = 2 - A + B;
        float E = (int)(365.25f * (Y + 4716));
        float F = (int)(30.6001f * (M + 1));
        float JD = C + D + E + F - 1524.5f;
        return JD;
    }
    public static String getFormatDate(String dateTime, int kind) {
        Date date = null;
        String format = getDateFormat(1);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (!(dateTime == null || dateTime.trim().equals(""))) {
            try {
                date = sdf.parse(dateTime);
            } catch (ParseException e) {}
        }
        return getDate(date, kind);
    }
    public static String getDate(Date date, int kind) {
        String res = "";
        if (date != null) {
            String format = getDateFormat(kind);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            res = sdf.format(date);
        }
        return res;
    }
    private static String getDateFormat(int kind) {
        String[] format = {
            "yyyy-MM-dd", // 0
            "yyyy-MM-dd HH:mm:ss", // 1
            "yyyy", // 2
            "M", // 3
            "dd", // 4
            "yyyy年M月d日H点m分", // 5
            "yyyy年M月d日", // 6
            "H点m分", // 7
            "yyyy/MM/dd HH:mm", // 8
            "HH", // 9
            "mm", // 10
            "yyyyMMdd", // 11
            "yyyyMMddHHmmss", // 12
            "yyyy-MM-dd 23:59:59" // 13
        };
        return format[kind];
    }
    /**
     * 时间转换
     *
     * @param datetime
     * @param kind
     * @return
     */
    public static Date parseDate(String datetime, int kind) {
        Date date = null;
        String format = getDateFormat(kind);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (!(datetime == null || datetime.trim().equals(""))) {
            try {
                date = sdf.parse(datetime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }
    /**
     * 时间字符串转换为yyyy-MM-dd格式日期
     *
     * @param datetime
     * @return
     */
    public static Date parseShortDate(String datetime) {
        return parseDate(datetime, 0);
    }
    public static Date parseDate(String datetime) {
        if (datetime.length() < 11) {
            return parseDate(datetime, 0);
        }
        return parseDate(datetime, 1);
    }
    /**
     * 日期格式专函
     *
     * @param date
     *            日期
     * @param kind
     *            转换格式 "yyyy-MM-dd", //0 "yyyy-MM-dd HH:mm:ss", //1 "yyyy",//2
     *            "M",//3 "dd", //4 "yyyy年M月d日H点m分", //5 "yyyy年M月d日", //6
     *            "H点m分", //7 "yyyy/MM/dd HH:mm", //8 "HH",//9 "mm",//10
     *            "yyyyMMdd", //11 "yyyyMMddHHmmss", //12 "yyyy-MM-dd 23:59:59"
     *            //13
     * @return
     */
    public static Date formatDateTime(Date date, int kind) {
        if (date != null) {
            String format = getDateFormat(kind);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return parseDate(sdf.format(date));
        } else {
            return null;
        }
    }
    /**
     * 日期转字符串
     *
     * @param date
     *            日期
     * @return "yyyy-MM-dd HH:mm:ss" 格式时间字符串
     */
    public static String getDateTime(Date date) {
        return getDate(date, 1);
    }
    /**
     * 时间格式字符串转换 yyyy-MM-dd HH:mm:ss 转换为yyyyMMddHHmmss yyyy-MM-dd 转换为yyyyMMdd
     *
     * @param datetime
     * @return
     */
    public static String convertChnToIso(String datetime) {
        if (datetime == null) return "";
        if (datetime.length() > 19) {
            datetime = datetime.substring(0, 19);
        }
        if (datetime.length() == 10) {
            datetime = toString(DateUtil.toDate(datetime, CHN_DATE_FORMAT), ISO_DATE_FORMAT);
        } else {
            datetime = toString(DateUtil.toDate(datetime, CHN_DATETIME_FORMAT), ISO_DATETIME_FORMAT);
        }
        return datetime;
    }
    /**
     * 字符串转时间格式字符串 yyyyMMddHHmmss转换为yyyy-MM-dd HH:mm:ss yyyyMMdd转换为yyyy-MM-dd
     *
     * @param datetime
     * @return
     */
    public static String convertIsoToChn(String datetime) {
        if (datetime == null) return "";
        if (datetime.length() > 14) {
            datetime = datetime.substring(0, 14);
        }
        if (datetime.length() == 8) {
            datetime = toString(DateUtil.toDate(datetime, ISO_DATE_FORMAT), CHN_DATE_FORMAT);
        } else {
            datetime = toString(DateUtil.toDate(datetime, ISO_DATETIME_FORMAT), CHN_DATETIME_FORMAT);
        }
        return datetime;
    }
    /**
     * 将长时间格式转为短时间格式,如yyyy-mm-dd hh24:mi:ss.sss转为yyyy-mm-dd hh24:mi
     *
     * @param datetime
     * @return
     */
    public static String convertChnDatetimeToShort(String datetime) {
        if (datetime == null) return "";
        if (datetime.length() > 16) {
            datetime = datetime.substring(0, 16);
        }
        return datetime;
    }
    /**
     * 将长时间格式转为短时间格式,如yyyy-mm-dd hh24:mi:ss.sss转为yyyy-mm-dd hh24:mi:ss
     *
     * @param datetime
     * @return
     */
    public static String convertChnDatetimeToSs(String datetime) {
        if (datetime == null) return "";
        if (datetime.length() > 19) {
            datetime = datetime.substring(0, 19);
        }
        return datetime;
    }
    /**
     * 将长时间格式转为短时间格式,如yyyy-mm-dd hh24:mi:ss.sss转为yyyy-mm-dd
     *
     * @param datetime
     * @return
     */
    public static String convertChnDatetimeToDate(String datetime) {
        if (datetime == null) return "";
        if (datetime.length() > 10) {
            datetime = datetime.substring(0, 10);
        }
        return datetime;
    }
    public static String convertIsoDatetimeToShort(String datetime) {
        if (datetime == null) return "";
        if (datetime.length() > 14) {
            datetime = datetime.substring(0, 14);
        }
        if (datetime.length() == 8) {
            datetime = toString(DateUtil.toDate(datetime, ISO_DATE_FORMAT), CHN_DATE_FORMAT);
        } else {
            datetime = toString(DateUtil.toDate(datetime, ISO_DATETIME_FORMAT), CHN_DATETIME_FORMAT);
        }
        return datetime;
    }
    public static String getDateFormat(String dateText) {
        if (null == dateText || dateText.equals("")) return null;
        Date date = DateUtil.toDate(dateText, DateUtil.CHN_DATETIME_FORMAT);
        if (date != null) return DateUtil.DB_CHN_DATETIME_FORMAT;
        date = DateUtil.toDate(dateText, DateUtil.CHN_DATE_FORMAT);
        if (date != null) return DateUtil.DB_CHN_DATE_FORMAT;
        date = DateUtil.toDate(dateText, DateUtil.ISO_DATETIME_FORMAT);
        if (date != null) return DateUtil.DB_ISO_DATETIME_FORMAT;
        date = DateUtil.toDate(dateText, DateUtil.ISO_DATE_FORMAT);
        if (date != null) return DateUtil.DB_ISO_DATE_FORMAT;
        return null;
    }
    /**
     * 获取下一天
     *
     * @param date
     * @return
     */
    public static Date getNextDate(Date date) {
        Calendar stCl = Calendar.getInstance();
        stCl.setTime(date);
        stCl.add(Calendar.DATE, 1);
        return stCl.getTime();
    }
    /**
     * 获取前一天
     *
     * @param date
     * @return
     */
    public static Date getFrontDate(Date date) {
        Calendar stCl = Calendar.getInstance();
        stCl.setTime(date);
        stCl.add(Calendar.DATE, -1);
        return stCl.getTime();
    }
    /**
     * 获取下一天
     *
     * @param date
     * @return
     */
    public static String getNextDate(String date) {
        try {
            Date da = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            Calendar stCl = Calendar.getInstance();
            stCl.setTime(da);
            stCl.add(Calendar.DATE, 1);
            return new SimpleDateFormat("yyyy-MM-dd").format(stCl.getTime());
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 获取前一天
     *
     * @param date
     * @return
     */
    public static String getFrontDate(String date) {
        try {
            Date da = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            Calendar stCl = Calendar.getInstance();
            stCl.setTime(da);
            stCl.add(Calendar.DATE, -1);
            return new SimpleDateFormat("yyyy-MM-dd").format(stCl.getTime());
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 获取上周一日期字符串
     *
     * @return
     */
    public static String getBeforeMonday() {
        Calendar cal = Calendar.getInstance();
        int n = -1;
        cal.add(Calendar.DATE, n * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return monday;
    }
    /**
     * 获取上一周星期日的日期
     *
     * @return
     */
    public static String getBeforeSunday() {
        Calendar cal = Calendar.getInstance();
        int n = 0;
        cal.add(Calendar.DATE, n * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return sunday;
    }
    /**
     * 获取N个月后的日期
     *
     * @param date
     * @param n
     *            月数
     * @return
     */
    public static Date getMonthDateAfter(Date date, int n) {
        Calendar stCl = Calendar.getInstance();
        stCl.setTime(date);
        stCl.add(Calendar.MONTH, n);
        return stCl.getTime();
    }
    /**
     * 将长整型数字转换为日期
     *
     * @param time
     * @param format
     * @return
     */
    public static String convert2String(long time) {
        if (time > 0l) {
            String format = CHN_DATE_FORMAT;
            SimpleDateFormat sf = new SimpleDateFormat(format);
            Date date = new Date(time);
            return sf.format(date);
        }
        return "";
    }
    /**
     * 返回 年月日小时分秒 毫秒
     *
     * @return yyyyMMddHHmmssS
     */
    public static String getTodayChar17() {
        String dateString = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssS");
        int length = dateString.length();
        if (length < 17) {
            String endStr = dateString.substring(14, length);
            int len = endStr.length();
            for (int i = 0; i < 3 - len; i++) {
                endStr = "0" + endStr;
            }
            dateString = dateString.substring(0, 14) + endStr;
        }
        return dateString;
    }
    /**
     * 判断是否是当月最后一天
     *
     * @param date
     * @return
     */
    public static boolean isLastMonthDay(Date date) {
        String d = toString(date);
        // 获取下一天
        String d1 = getNextDate(d);
        // 获取两个日期的月份,比较是否一致来判断是否当月最后一天
        d = d.substring(5, 7);
        d1 = d1.substring(5, 7);
        if (d.equals(d1)) {
            return false;
        }
        return true;
    }
    /**
     * 时间差
     *
     * @param endDate
     * @param nowDate
     * @param scop
     *            4 输出天 3输出到时 2输出到分 1输出到秒
     * @param positive
     *            是否过滤 0
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return (day == 0 ? "" : day + "天") + (hour == 0 ? "" : hour + "小时") + min + "分钟";
    }
    /**
     * 根据秒数转为*天*小时*分*秒返回
     * @param mss
     * @return
     */
    public static String getDatePoor(long mss) {
        String DateTimes = null;
        long days = mss / (60 * 60 * 24);
        long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % (60 * 60)) / 60;
        long seconds = mss % 60;
        if (days > 0) {
            DateTimes = days + "天" + hours + "小时" + minutes + "分钟" + (seconds == 0 ? "" : seconds + "秒");
        } else if (hours > 0) {
            DateTimes = hours + "小时" + minutes + "分钟" + (seconds == 0 ? "" : seconds + "秒");
        }
        /* else if (minutes > 0) {
            DateTimes = minutes + "分钟" + (seconds==0?"":seconds + "秒");
        } */
        else {
            //DateTimes = seconds + "秒";
            DateTimes = minutes + "分钟";
        }
        return DateTimes;
    }
    public static void main(String[] args) {
        /*String beginDate = "2017-02-14 16:10:49";
        String endDate = "2017-02-14 13:23:06";
        Date d1 = DateUtil.toDate(beginDate, DateUtil.CHN_DATETIME_FORMAT);
        Date d2 = DateUtil.toDate(endDate, DateUtil.CHN_DATETIME_FORMAT);
        System.out.println(getDatePoor(d1, d2));
        System.out.println(getDatePoor(168*60));*/
        long minutes = (60 * 60) / 60;
        System.out.println(minutes + "分钟");
    }
}
  • 发表于 2017-11-17 17:03
  • 阅读 ( 1789 )
  • 分类:Java

0 条评论

请先 登录 后评论
不写代码的码农
HJ社区-肖峰

IT

29 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章