• Calendar.getInstance()获取指定时间点(定时)


    使用Calendar.getInstance()不仅能获取当前的时间,还能指定需要获取的时间点,在项目应用中达到
    
    • 1

    定时的作用,下面是常用的一些指定时间点使用:

    package com.data.cockpit.util;
     
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
     
    /**
     * 日期工具类
     * @author sixmonth
     * @date 2019年6月5日
     *
     */
    public class DateUtils {
        public static void main(String[]args){
            System.out.println("时间为:\n"+getDate1()
            +"\n"+getDate2()
            +"\n"+getDate3()
            +"\n"+getDate4()
            +"\n"+getDate5()
            +"\n"+getMonthFirstDay(2019,-1)
            +"\n"+getNestLastMin()
            +"\n"+getLastDay("2019-05-01")
            +"\n"+getMonthLastDay(2019,-1));
        }
        
        /*    
         * Calendar.HOUR_OF_DAY     24小时制
         * Calendar.HOUR     12小时制
         */
        
        /**
         * 获取当天0点时间
         * @return
         * @author sixmonth
         * @date 2019年6月5日
         *
         */
        public static String getDate1(){
             Calendar cal = Calendar.getInstance();
             cal.set(Calendar.HOUR_OF_DAY, 0);//控制时
             cal.set(Calendar.MINUTE, 0);//控制分
             cal.set(Calendar.SECOND, 0);//控制秒
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             return sdf.format(cal.getTime());
        }
        
        /**
         * 获取当天12点时间
         * @return
         * @author sixmonth
         * @date 2019年6月5日
         *
         */
        public static String getDate2(){
             Calendar cal = Calendar.getInstance();
             cal.set(Calendar.HOUR_OF_DAY, 12);
             cal.set(Calendar.MINUTE, 0);
             cal.set(Calendar.SECOND, 0);
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             return sdf.format(cal.getTime());
        }    
        
        /**
         * 获取本周一0点时间
         * @return
         * @author sixmonth
         * @date 2019年6月5日
         *
         */
        public static String getDate3(){
             Calendar cal = Calendar.getInstance();
             // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题
             int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
             if (1 == dayWeek) {  
                cal.add(Calendar.DAY_OF_MONTH, -1);  
             } 
             cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
             cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             return sdf.format(cal.getTime());
        }    
        
        /**
         * 获取本月第一天0点时间
         * @return
         * @author sixmonth
         * @date 2019年6月5日
         *
         */
        public static String getDate4(){
             Calendar cal = Calendar.getInstance();
             cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
             cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             return sdf.format(cal.getTime());
        }
        
        /**
         * 获得本月最后一天24点时间
         * @return
         * @author sixmonth
         * @date 2019年6月5日
         *
         */
        public static String getDate5(){
             Calendar cal = Calendar.getInstance();
             cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
             cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
             cal.set(Calendar.HOUR_OF_DAY, 24);
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             return sdf.format(cal.getTime());
        }    
        
         /**
          * 获取指定年月的第一天,如果参数格式错误,则重置成当前月份的第一天
          * @param year
          * @param month
          * @return
          * @author sixmonth
          * @date 2019年6月5日
          *
          */
        public static String getMonthFirstDay(int year, int month) {     
            Calendar cal = Calendar.getInstance();   
            if(month>0&&month<=12&&year>0) {
                //设置年份
                cal.set(Calendar.YEAR, year);
                //设置月份 
                cal.set(Calendar.MONTH, month-1); 
                //设置日历中月份的最小天数 
                cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE));  
                //格式化日期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(cal.getTime());  
            }else {
                //设置当前年月日
                cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH));
                //将日时间设置成本月第一天
                cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DAY_OF_MONTH));
                //格式化日期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(cal.getTime());
            }
        }
        
        /**
         * 获取指定年月的最后一天,如果参数格式错误,则重置成当前月份的最后一天
         * @param year
         * @param month
         * @return
         * @author sixmonth
         * @date 2019年6月5日
         *
         */
         public static String getMonthLastDay(int year, int month) {
             Calendar cal = Calendar.getInstance();   
             if(month>0&&month<=12&&year>0) {  
                 //设置年份  
                 cal.set(Calendar.YEAR, year);  
                 //设置月份  
                 cal.set(Calendar.MONTH, month-1); 
                 //设置日历中月份的最大天数  
                 cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));  
                 //格式化日期
                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                 return sdf.format(cal.getTime());
             }else {
                 //设置当前年月日
                cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH));
                //将日时间设置成本月最后一天
                cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                //格式化日期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(cal.getTime());
             }
         }
         
         /**
          * 获取当前时间之:前1分钟的时间
          * @param min 自定义间隔分钟,比如:-1
          * @return
          * @author sixmonth
          * @date 2019年6月5日
          *
          */
         public static String getNestLastMin(int min) {
             Calendar cal = Calendar.getInstance();
             cal.setTime(new Date());
             cal.add(Calendar.MINUTE, min); //数字参数可修改,+表示n分钟之后,-表示n分钟之前的时间
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             System.out.println("前一分钟:"+sdf.format(cal.getTime()));
             return sdf.format(cal.getTime());
         }
         /**
          * 获取当前时间之:前一周的时间
          * @param pattern 自定义格式化输出时间,比如:"yyyy-MM-dd HH:mm:ss"
          * @param day 自定义间隔天数,比如:-7
          * @return
          * @author sixmonth
          * @date 2019年6月6日
          *
          */
         public static String getNestLastWeek(String pattern,int day) {
             Calendar cal = Calendar.getInstance();
             cal.setTime(new Date());
             cal.add(Calendar.DATE, day); //数字参数可修改,+表示n天之后,-表示n天之前的时间
             SimpleDateFormat sdf = new SimpleDateFormat(pattern);
             System.out.println("前七天时间:"+sdf.format(cal.getTime()));
             return sdf.format(cal.getTime());
         }
         
         /**
          *  获取当前时间之:前一个月的时间
          * @param pattern  自定义格式化输出时间,比如:"yyyy-MM-dd HH:mm:ss"
         * @param mon 时间间隔,比如:-1/+1
          * @return
          * @author sixmonth
          * @date 2019年6月6日
          *
          */
         public static String getNestLastMonth(String pattern,int mon) {
             Calendar cal = Calendar.getInstance();
             cal.setTime(new Date());
             cal.add(Calendar.MONTH, mon); //数字参数可修改,+表示n月之后,-表示n月之前的时间
             SimpleDateFormat sdf = new SimpleDateFormat(pattern);
             System.out.println("前一个月时间:"+sdf.format(cal.getTime()));
             return sdf.format(cal.getTime());
         }
         
         /**
          * 获取当前时间之:前一年的时间
          * @param pattern  自定义格式化输出时间,比如:"yyyy-MM-dd HH:mm:ss"
          * @param year 时间间隔,比如:-1/+1
          * @return
          * @author sixmonth
          * @date 2019年6月6日
          *
          */
         public static String getNestLastYear(String pattern,int year) {
             Calendar cal = Calendar.getInstance();
             cal.setTime(new Date());
             cal.add(Calendar.YEAR, -1); //数字参数可修改,+表示n年之后,-表示n年之前的时间
             SimpleDateFormat sdf = new SimpleDateFormat(pattern);
             System.out.println("前一年时间:"+sdf.format(cal.getTime()));
             return sdf.format(cal.getTime());
         }
         
         /**
          * 根据指定日期,获取前一天或者后一天时间
          * @param date
          * @param day 时间间隔,比如:-1/+1
          * @return
          * @throws ParseException
          * @author sixmonth
          * @date 2019年6月6日
          *
          */
         public static String getLastDay(String date,int day){
             Calendar cal = Calendar.getInstance();
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
             Date dateTem;
             try {
                dateTem = sdf.parse(date);
                cal.setTime(dateTem);
                cal.add(Calendar.DATE, day); //数字参数可修改,+表示n天之后,-表示n天之前的时间
                System.out.println("前一天时间:"+sdf.format(cal.getTime()));
             } catch (ParseException e) {
                e.printStackTrace();
             }
             return sdf.format(cal.getTime());
         }
     
    }
    
    • 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
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274

    控制台输出如下:
    在这里插入图片描述
    参考链接:http://blog.csdn.net/holee_bk/article/details/53456630

  • 相关阅读:
    知识蒸馏原理与PVKD论文阅读
    【AI语音】华为EC6110M、Q21AQ、Q21C部分EC6110T、EC6110U_海思3798MV310_通刷_卡刷固件
    ARP协议;DHCP协议;ICMP协议
    Dubbo 一些你不一定知道但是很好用的功能
    Linux5.x启动过程分析
    Springboot 之 Mybatis-plus 多数据源
    231022|推荐系统1-推荐框架注释
    力扣刷题记录53.1-----700. 二叉搜索树中的搜索
    PyTorch官方文档学习笔记(备忘)
    400电话怎么办理
  • 原文地址:https://blog.csdn.net/weixin_42164754/article/details/132831311