• DateUtils时间的封装


    import java.text.ParseException;

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    import org.apache.commons.lang.StringUtils;

    /**
    *
    * <p>
    * 日期时间处理工具类
    * </p>
    */
    public class DateUtils {

    private static final Class<DateUtils> CLAZZ = DateUtils.class;

    public static final long ONE_WEEK = 604800000L;

    public static final String FORMAT_DATE = "yyyy-MM-dd";

    public static final String FORMAT_TIMESTAMP = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm";

    // 1分钟60秒
    // private static final long SECOND_OF_MINUT = 60;

    // 1秒1000毫秒
    // private static final long MILLSECOND_OF_SECOND = 1000;

    // 一天多少毫秒 86400000L
    public static final long DAY_OF_MILLSECOND = 86400000L;

    // 一个小时多少毫秒数 3600000
    private static final long HOUR_OF_MILLSECOND = 3600000L;

    // 一分钟多少毫秒数 60000
    private static final long MINUTE_OF_MILLSECOND = 60000L;

    /**
    * get the begin of today,2006-09-15 00:00:00 000
    * @return
    */
    public static Date getBeginOfToday() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
    }

    /**
    * 将java.util.Date转换为java.sql.Date
    * @param date
    * @return
    */
    public static java.sql.Date getSQLDate(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    java.sql.Date sd = new java.sql.Date(cal.getTimeInMillis());
    return sd;
    }

    /**
    * 将java.sql.Timestamp 转换成java.util.Calendar
    * @param timestamp
    * @return
    */
    public static java.util.Calendar getCalendarBySqlTimestamp(java.sql.Timestamp timestamp) {
    if (timestamp != null) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new java.util.Date(timestamp.getTime()));
    return calendar;
    } else {
    return null;
    }
    }

    /**
    * 将java.sql.Timestamp 转换成java.util.Date
    * @param timestamp
    * @return
    */
    public static java.util.Date getUtilDateBySqlTimestamp(java.sql.Timestamp timestamp) {
    if (timestamp != null) {
    return new Date(timestamp.getTime());
    } else {
    return null;
    }
    }

    /**
    * 获取系统当前日期(不包括时间)
    * @return
    */
    public static String getCurrent() {
    SimpleDateFormat dateFormat = new SimpleDateFormat();
    dateFormat.applyPattern("yyyy-MM-dd");
    return dateFormat.format(new Date());
    }

    /**
    * 获取系统当前日期时间(包括时间)
    * @return
    */
    public static String getCurrentTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat();
    dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
    return dateFormat.format(new Date());
    }

    /**
    * get the begin of day,2006-09-15 00:00:00 000
    * @param date
    * @return
    */
    public static Date getBeginOfDay(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date ds = new Date(cal.getTimeInMillis());
    return ds;
    }

    /**
    * 得到当前日期的凌晨12点59分59秒
    * @return
    */
    public static Date getEndOfToday() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 999);
    return cal.getTime();
    }

    /**
    * 得到下一天
    * @param date
    * @return
    */
    public static Date getNextDay(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
    return cal.getTime();
    }

    /**
    * 得到上一天(00:00:00)
    * @return
    */
    public static Date getTomorrow() {
    Calendar cal = new GregorianCalendar();
    cal.setTime(getBeginOfToday());
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
    return cal.getTime();
    }

    /**
    * 得到明天的这个时刻
    * @return
    */
    public static String getTomorrowNow() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
    return formatTime(cal.getTime());
    }

    /**
    * 得到昨天的这个时刻
    * @return
    */
    public static String getYesterdayNow() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
    return formatTime(cal.getTime());
    }

    /**
    * 得到今天
    * @return
    */
    public static Date getToday() {
    return new Date();
    }

    /**
    * 得到指定日期的下一天
    * @param date
    * @return
    */
    public static Date getNextMonth(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    return cal.getTime();
    }

    /**
    * 得到指定日期的上一天
    * @param date
    * @return
    */
    public static Date getPreviousDay(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
    }

    /**
    * 得到当前月的第一天
    * @return
    */
    public static Date getFirstDayOfCurMonth() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    return getBeginOfDay(cal.getTime());
    }

    /**
    * 得到当前月的最后一天
    * @return
    */
    public static Date getEndDayOfCurMonth() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    return getBeginOfDay(cal.getTime());
    }

    /**
    * 格式化日期为"yyyy-MM-dd"形式
    * @param d
    * @return
    */
    public static String formatDate(Date d) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    return df.format(d);
    }

    /**
    * 将日期格式字符串解析为"yyyy-MM-dd"形式的日期
    * @param dateStr
    * @return
    */
    public static Date parseDate(String dateStr) {
    return parseDateWithStyle(dateStr, FORMAT_DATE);
    }

    /**
    * 将日期格式字符串解析为"yyyy-MM-dd HH:mm:ss"形式的日期
    * @param dateStr
    * @return
    */
    public static Date parseTimeStamp(String dateStr) {
    return parseDateWithStyle(dateStr, FORMAT_TIMESTAMP);
    }

    /**
    * 将日期格式的字符串解析为日期
    * @param dateStr
    * @param style
    * @return
    */
    public static Date parseDateWithStyle(String dateStr, String style) {
    SimpleDateFormat df = new SimpleDateFormat(style);
    Date returnDate = null;
    try {
    returnDate = df.parse(dateStr);
    } catch (ParseException e) {
    LogUtils.logError(CLAZZ, "analsys date error,analsys date str:", dateStr, " date formate:", style);
    throw new RuntimeException();
    }
    return returnDate;
    }

    /**
    * 格式化时间为"yyyy-MM-dd HH:mm:ss"形式
    * @param d
    * @return
    */
    public static String formatTime(Date d) {
    if(d == null){
    return null;
    }
    SimpleDateFormat df = new SimpleDateFormat(FORMAT_TIMESTAMP);
    return df.format(d);
    }

    /**
    * 将指定的日期格式按照指定的格式进行格式化,返回字符串形式
    * @param date 需要格式化的日期
    * @param format 自定义的格式,DateUtils辅助类中已经定义了三种格式 例如:DateUtils.FORMAT_DATE
    * @return
    */
    public static String formatDate(Date date, String format) {
    SimpleDateFormat dateFormat = new SimpleDateFormat();
    dateFormat.applyPattern(format);
    return dateFormat.format(date);
    }

    /**
    * 得到两个日期中较大者
    * @param date1
    * @param date2
    * @return
    */
    public static Date getMax(Date date1, Date date2) {
    if (date1 == null)
    return date2;
    if (date2 == null)
    return date1;
    if (date1.after(date2))
    return date1;
    return date2;
    }

    /**
    * 得到两个日期中较小者
    * @param date1
    * @param date2
    * @return
    */
    public static Date getMin(Date date1, Date date2) {
    if (date1 == null)
    return date2;
    if (date2 == null)
    return date1;
    if (date1.after(date2))
    return date2;
    return date1;
    }

    /**
    * 得到year年month月的天数
    * @param year
    * @param month
    * @return
    */
    public static int daysOfMonth(int year, int month) {
    switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    return 31;
    case 4:
    case 6:
    case 9:
    case 11:
    return 30;
    case 2:
    if (isLeap(year))
    return 29;
    return 28;
    default:
    return 0;
    }
    }

    /**
    * 在指定日期基础上加若干天
    * @param date
    * @param day
    * @return
    */
    public static Date addDay(Date date, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(5, day);
    Date d = calendar.getTime();
    return d;
    }

    /**
    * 在指定日期基础上加若干小时
    * @param date
    * @param l
    * @return
    */
    public static Date addHour(Date date, long l) {
    long oldD = date.getTime();
    long deltaD = l * 60L * 60L * 1000L;
    long newD = oldD + deltaD;
    Date d = new Date(newD);
    return d;
    }

    /**
    * 在指定日期基础上加若干分钟
    * @param date
    * @param l
    * @return
    */
    public static Date addMinute(Date date, long l) {
    long oldD = date.getTime();
    long deltaD = l * 60L * 1000L;
    long newD = oldD + deltaD;
    Date d = new Date(newD);
    return d;
    }

    /**
    * 在指定日期基础上加若干月
    * @param date
    * @param i
    * @return
    */
    public static Date addMonth(Date date, int i) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int month = calendar.get(Calendar.MONTH);
    month += i;
    int deltaY = month / 12;
    month %= 12;
    calendar.set(Calendar.MONTH, month);
    if (deltaY != 0) {
    int year = calendar.get(Calendar.YEAR);
    calendar.set(Calendar.YEAR, deltaY + year);
    }
    return calendar.getTime();
    }

    /**
    * 在指定日期基础上加若干秒
    * @param date
    * @param l
    * @return
    */
    public static Date addSecond(Date date, long l) {
    long oldD = date.getTime();
    long deltaD = l * 1000L;
    long newD = oldD + deltaD;
    Date d = new Date(newD);
    return d;
    }

    /**
    * 在指定日期基础上加若干年
    * @param date
    * @param i
    * @return
    */
    public static Date addYear(Date date, int i) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    calendar.set(Calendar.YEAR, i + year);
    return calendar.getTime();
    }

    /**
    * 判断一年是否是闰年
    * @param year
    * @return
    */
    public static boolean isLeap(int year) {
    boolean divBy4 = (year % 4 == 0);
    boolean divBy100 = (year % 100 == 0);
    boolean divBy400 = (year % 400 == 0);
    return divBy4 && (!divBy100 || divBy400);
    }

    /**
    * 计算两个日期之间的毫秒数
    * @param beginDate
    * @param endDate
    * @return
    */
    public static long countBetweenDays(String beginDate, String endDate) {
    java.util.Date beginsDate = DateUtils.parseTimeStamp(beginDate);
    java.util.Date endsDate = DateUtils.parseTimeStamp(endDate);
    // 两个日期间隔毫秒数
    return endsDate.getTime() - beginsDate.getTime();
    }

    /**
    * 计算毫秒数
    * @param day
    * @param hour
    * @param minute
    * @return
    */
    public static long countDay(int day, int hour, int minute) {
    long value = 0L;
    value = day * DAY_OF_MILLSECOND + hour * HOUR_OF_MILLSECOND + minute * MINUTE_OF_MILLSECOND;
    return value;
    }

    public static Date addDate999(Date date){
    if(null == date){
    return null;
    }
    return new Date(date.getTime()+999);
    }

    /**
    * 毫秒转为天,时,分,秒
    * @param ms 毫秒
    * @return
    */
    public static String formatTime(long ms) {
    int ss = 1000;
    int mi = ss * 60;
    int hh = mi * 60;
    int dd = hh * 24;

    long day = ms / dd;
    long hour = (ms - day * dd) / hh;
    long minute = (ms - day * dd - hour * hh) / mi;
    long second = (ms - day * dd - hour * hh - minute * mi) / ss;

    StringBuffer timeSb = new StringBuffer();

    if(day > 0){
    timeSb.append(day).append("天");
    }

    if(hour > 0){
    timeSb.append(hour).append("小时");
    }

    if(minute > 0){
    timeSb.append(minute).append("分");
    }

    if(second > 0){
    timeSb.append(second).append("秒");
    }

    return timeSb.toString();
    }

    public static Date toDateFull(String str){
    return toDate(str, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date toDate(String date,String pattern){
    if(StringUtils.isEmpty(date))
    return null;
    SimpleDateFormat f=new SimpleDateFormat(pattern);
    try {
    return f.parse(date);
    } catch (ParseException e) {
    throw new IllegalArgumentException("date format error.the str must be "+pattern);
    }
    }

    public static int getMonthsBetween(Calendar d1, Calendar d2){
    if (d1.after(d2)) {
    java.util.Calendar swap = d1;
    d1 = d2;
    d2 = swap;
    }
    int months = d2.get(Calendar.MONTH) - d1.get(Calendar.MONTH);//当前月也算
    int y2 = d2.get(Calendar.YEAR);
    if (d1.get(Calendar.YEAR) != y2) {
    d1 = (Calendar) d1.clone();
    do {
    months += d1.getActualMaximum(Calendar.MONTH)+1;//得到当年的实际月数;月数从0开始,所以 +1
    d1.add(Calendar.YEAR, 1);
    } while (d1.get(Calendar.YEAR) != y2);
    }
    return months;
    }

    public static String date2String(Date date,String pattern){
    SimpleDateFormat f=new SimpleDateFormat(pattern);
    return f.format(date);
    }

    public static String getOldStrTimeOnDay(Date date,int oldDay) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.setTime(date);

    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + oldDay);

    SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return f.format(calendar.getTime());

    }

    public static String getOldStrTimeOnMonth(Date date,int oldmonth,String patter) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.setTime(date);

    // calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + oldmonth);
    calendar.add(Calendar.MONTH, oldmonth);

    SimpleDateFormat f=new SimpleDateFormat(patter);
    return f.format(calendar.getTime());

    }

    public static void main(String[] args) {
    // System.out.println(formatTime(60000000));
    System.out.println(getOldStrTimeOnMonth(parseTimeStamp("2017-07-31 00:00:00"), 2, "yyyyMMdd"));
    }
    }

  • 相关阅读:
    《四世同堂》金句摘抄(一)
    Django打造在线教育平台_day_3: 搭建后台管理系统Xadmin之其他app的数据表注册
    Django打造在线教育平台_day_3: 搭建后台管理系统Xadmin
    Django打造在线教育平台_day_3: 搭建后台管理系统Django自带的admin
    Django打造在线教育平台_day_2:新建operation app 编写models
    Django打造在线教育平台_day_2:新建organization app 编写models
    Django打造在线教育平台_day_2:新建courses app 编写models
    Django打造在线教育平台_day_2:新建users app 编写models之完善
    Django打造在线教育平台_day_2:新建users app 编写models之扩展user表
    Django打造在线教育平台_day_1:开发环境与项目的搭建
  • 原文地址:https://www.cnblogs.com/xxj-bigshow/p/7601625.html
Copyright © 2020-2023  润新知