Java commonly used time and calendar processing classes

Java commonly used time and calendar processing classes

public class DateUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);
public static final long ONE_MINUTE = 60;
public static final long ONE_HOUR = 3600;
public static final long ONE_DAY = 86400;
public static final long ONE_MONTH = 2592000;
public static final long ONE_YEAR = 31104000;

/**
 *
 * @param day
 * @return
 */
public static String getXDays( int day ){
    Calendar calendar2 = Calendar.getInstance();
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    calendar2.add( Calendar.DATE, day );
    String two_days_last = sdf2.format(calendar2.getTime());
    LOGGER.info( "前天日期:{}", two_days_last );
    return two_days_last;
}


public static String formatDate(Date date) {
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    return sdf2.format(date);
}

public  static Date parseDate(String dateStr) {
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    try {
        return sdf2.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
        throw new ServiceException("日期转换出错");
    }
}

/**
 *
 * @param day
 * @return
 */
public static Date addDay(Date date, int day ){
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(date);
    calendar2.add( Calendar.DATE, day );
    return calendar2.getTime();
}


/**
 * 获取一天中剩余的时间(秒数)
 */
public static Integer getDayRemainingTime(Date currentDate) {
    LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
            ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
            .withSecond(0).withNano(0);
    LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
            ZoneId.systemDefault());
    long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
    return (int) seconds;
}

public static Integer getDayNumOfWeek(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    //一周第一天是否为星期天
    boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
    //获取周几
    int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
    //若一周第一天为星期天,则-1
    if(isFirstSunday){
        weekDay = weekDay - 1;
        if(weekDay == 0){
            weekDay = 7;
        }
    }
    return weekDay;
}



public static void main(String[] args) throws ParseException {
    String time = "2020-08-09";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = format.parse(time);
    System.out.println(getLastSunday(date));

// System.out.println( DateUtils.getBeforeDay(2));

}

/**
 * 获取某年某月的最后一天日期
 * @param year
 * @param month
 * @return
 */
public static String getLastDayOfMonth(int year,int month) {
    Calendar cal = Calendar.getInstance();
    //设置年份
    cal.set(Calendar.YEAR,year);
    //设置月份
    cal.set(Calendar.MONTH, month-1);
    //获取某月最大天数
    int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    //设置日历中月份的最大天数
    cal.set(Calendar.DAY_OF_MONTH, lastDay);
    //格式化日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(cal.getTime());
}

/**
 * 获取某日是否在某月内
 * @param year
 * @param month
 * @param date
 * @return
 */
public static boolean isThisMonth(int year,int month,Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int month1 = cal.get(Calendar.MONTH) + 1;
    int year1 = cal.get(Calendar.YEAR);
    return year == year1 && month == month1;
}

/**
 * 获取前day天的日期
 * @param day
 * @return
 */
public static String getBeforeDay(int day){
    DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,-24 * day);
    return dateFormat.format(calendar.getTime());
}

/**
 * 获取前year年日期
 * @param year
 * @param date
 * @return
 */
public static String getLastYearDay(int year,Date date){
    DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.YEAR, year);
    return dateFormat.format(calendar.getTime());
}

/**
 * 获取前后month个月日期
 * @param month
 * @param date
 * @return
 */
public static String getLastMonthDay(int month,Date date){
    DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, month);
    return dateFormat.format(calendar.getTime());
}

/**
 * 日期转换
 * @param time
 * @return
 */
public static Date parseTime(String time){
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date dateTime = simpleDateFormat.parse(time);
        return dateTime;
    } catch (ParseException e) {
        e.printStackTrace();
        throw new ServiceException("日期转换错误");
    }
}

/**
 * 获取上一个周日
 * @param date
 * @return
 */
public static String getLastSunday(Date date){
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    c.add(Calendar.WEEK_OF_MONTH, -1);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return simpleDateFormat.format(c.getTime());
}

public static Date getLastSundayDate(Date date){
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    c.add(Calendar.WEEK_OF_MONTH, -1);
    return c.getTime();
}

/**
 * 获取某天的前几天的日期
 * @param day
 * @param date
 * @return
 */
public static String getDayBeforeDay(int day,Date date){
    DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY,-24 * day);
    return dateFormat.format(calendar.getTime());
}
/**
 * 获取某天的前几天的日期
 * @param day
 * @param date
 * @return
 */
public static String getDayBeforeDay2(int day,Date date){
    DateFormat dateFormat=new SimpleDateFormat("yyyyMMdd");
    Calendar calendar=Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY,-24 * day);
    return dateFormat.format(calendar.getTime());
}


public static String fromToday(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    long time = date.getTime() / 1000;
    long now = System.currentTimeMillis() / 1000;
    long ago = now - time;
    if(ago <= ONE_MINUTE){
        return "刚刚";
    }
    else  if (ago <= ONE_HOUR){
        return ago / ONE_MINUTE + "分钟前";
    }

    else if (ago <= ONE_DAY) {
        return ago / ONE_HOUR + "小时前";
    } else if (ago <= ONE_DAY * 2) {
        return "昨天 " + DateUtils.formatDateToHAndM(date);
    } else if (ago <= ONE_DAY * 3) {
        return "前天 " + DateUtils.formatDateToHAndM(date);
    } else {
        return formatDate(date);
    }

}

/**
 * date成转换时分
 * @param date
 * @return
 */
public static String formatDateToHAndM(Date date){
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
    return formatter.format(date);
}
public static String LocatDateToSting(LocalDate date){
    DateTimeFormatter  formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    return date.format(formatter);
}

public static Date formatStringToHAndM(String date) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
    return formatter.parse(date);
}

}

Guess you like

Origin blog.csdn.net/xxaann/article/details/113244360