avatar

目录
Calendar/Date 工具类的常用方法

Calendar

  • 基本使用

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    Calendar calendar = Calendar.getInstance();
    // 显示年份
    int year = calendar.get(Calendar.YEAR);
    System.out.println("年:" + year);

    // 显示月份 -- 从 0 开始
    int month = calendar.get(Calendar.MONTH);
    System.out.println("月:" + (month + 1));

    // 星期
    int week = calendar.get(Calendar.DAY_OF_WEEK);
    System.out.println("星期:" + week);

    // 今年的第 N 天
    int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
    System.out.println("今年的第 " + DAY_OF_YEAR + " 天");

    // 本月第 N 天
    int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
    System.out.println("本月的第 " + DAY_OF_MONTH + " 天");

    // 3小时以后
    calendar.add(Calendar.HOUR_OF_DAY, 3);
    int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
    System.out.println("三小时以后时间: " + HOUR_OF_DAY);

    // 当前分钟数
    int MINUTE = calendar.get(Calendar.MINUTE);
    System.out.println("当前分钟数: " + MINUTE);

    // 15 分钟以后
    calendar.add(Calendar.MINUTE, 15);
    MINUTE = calendar.get(Calendar.MINUTE);
    System.out.println("15分钟后的时间:" + MINUTE);

    // 30分钟前
    calendar.add(Calendar.MINUTE, -30);
    MINUTE = calendar.get(Calendar.MINUTE);
    System.out.println("30分钟以前的时间: " + MINUTE);

    // 格式化显示
    String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(calendar.getTime());
    System.out.println(str);

    // 重置 Calendar 显示当前时间
    calendar.setTime(new Date());
    str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS").format(calendar.getTime());
    System.out.println(str);

    // 创建一个 Calendar 用于比较时间
    Calendar calendarNew = Calendar.getInstance();

    // 设定为 5 小时以前,后者大,显示 -1
    calendarNew.add(Calendar.HOUR, -5);
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));

    // 设定7小时以后,前者大,显示 1
    calendarNew.add(Calendar.HOUR, +7);
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));

    // 退回 2 小时,时间相同,显示 0
    calendarNew.add(Calendar.HOUR, -2);
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));


    //设置每月的最大天数
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    //设置每天的最大小时
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    //设置每小时最大分钟
    calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
    //设置每分钟最大秒
    calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));

    //设置每月的最小天数
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    //设置每天的最小小时
    calendar.set(Calendar.HOUR, calendar.getActualMinimum(Calendar.HOUR));
    //设置每小时最小分钟
    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
    //设置每分钟最小秒
    calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));

    // 设置当前月份第一天的00:00:00
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY),calendar.getActualMinimum(Calendar.DAY_OF_MONTH),00,00,00);

    // 设置当前月份最后一天的23:59:59
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY),calendar.getActualMaximum(Calendar.DAY_OF_MONTH),23,59,59);
  • 计算两时间相差天数与周数

    java
    1
    2
    3
    4
    5
    ComputeDisposeDateUtil dateUtil = new ComputeDisposeDateUtil();
    int days = dateUtil.dayCount("2017-08-27", new Date());
    int week = dateUtil.weekCount(days);
    System.out.println(days);
    System.out.println(week);
    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    package test;

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

    /**
    * Created on 2017/10/17
    *
    * @author muycode
    */
    public class ComputeDisposeDateUtil {

    /**
    * 根据起始日期返回相差天数
    * @param pattern yyy-MM-dd
    * @param nowDate new Date()
    * @return
    */
    public int dayCount(String pattern, Date nowDate){
    Date oldDate = disposeDate(pattern);

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();

    startDate.setTime(oldDate);
    endDate.setTime(nowDate);

    int days = ((int) (endDate.getTime().getTime() / 1000) - (int) (startDate.getTime().getTime() / 1000)) / 3600 / 24;

    return days;
    }

    /**
    * 根据天数返回相差周数
    * @param days
    * @return
    */
    public int weekCount(int days){
    int day = days % 7 == 0 ? days / 7 : days / 7 + 1;
    return day;
    }

    /**
    * 返回字符串格式
    * @param date
    * @return
    */
    private String disposeDate(Date date){
    return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    /**
    * 返回日期格式
    * @param date
    * @return
    */
    private Date disposeDate(String date){
    try {
    return new SimpleDateFormat("yyyy-MM-dd").parse(date);
    } catch (ParseException e){
    e.printStackTrace();
    }
    return null;
    }
    }

Date

  • 距离当天结束还有多少秒

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /**
    * 距离23:59:59分还有多少秒
    *
    * @return
    */
    public static int get24TimeInterval() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat endDateFormat = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    String startDateStr = dateFormat.format(new Date());
    String endDateStr = endDateFormat.format(new Date());
    long second = 0;
    try {
    long startDateLong = dateFormat.parse(startDateStr).getTime();
    long endDateLong = dateFormat.parse(endDateStr).getTime();
    second = (endDateLong - startDateLong) / 1000;
    } catch (ParseException e) {
    e.printStackTrace();
    } finally {
    return (int) second;
    }
    }
  • 字符串转换日期格式

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    /**
    * 字符串转换日期格式
    * @param propertyValue:yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
    * @return
    * @throws ParseException
    */
    public static Date disposeDate(String propertyValue) {
    try {
    if (StringUtils.isNotBlank(propertyValue)){
    String t = "T";
    if (propertyValue.contains(t)){
    propertyValue = propertyValue.replace("Z", " UTC");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
    return format.parse(propertyValue);
    } else {
    return new SimpleDateFormat("yyyy-MM-dd").parse(propertyValue);
    }
    }
    return null;
    } catch (ParseException e){
    return null;
    }
    }
文章作者: 颜不喜
文章链接: https://www.muycode.cn/article/calendar20200313.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 颜不喜
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论