• Android中使用Java计算指定日期、时间戳等方法的合集


    1、获取当前年月日

    Calendar cal = Calendar.getInstance();
    //获取当前年
    int currentYear = cal.get(Calendar.YEAR);
    //获取当前月
    int currentMonth = cal.get(Calendar.MONTH) + 1;
    
    // 获取当前日
    int day = calenedar.get(Calendar.DATE);  
    // 获取当前小时
    int hour = calendar.get(Calendar.HOUR_OF_DAY);  
    // 获取当前分钟
    int minute = calendar.get(Calendar.MINUTE);  
    // 获取当前秒
    int second = calendar.get(Calendar.SECOND);  
    
    // 获取当前是本周第几天
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);  
    // 获取当前是本月第几天
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);  
    // 获取当前是本年第几天
    int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2、时间戳格式化

    //当前时间的时间戳
    long start = System.currentTimeMillis();
    
    //将时间戳转换为 yyyy-MM-dd HH:mm:ss 格式的字符串
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = format.format("时间戳");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、根据当前时间戳获取当前时间的时分值

    SimpleDateFormat formater_hour = new SimpleDateFormat("HH");
    SimpleDateFormat formater_minute = new SimpleDateFormat("mm");
    Date date = new Date(System.currentTimeMillis());
    int cur_hour = Integer.valueOf(formater_hour.format(date));
    int cur_minute = Integer.valueOf(formater_minute.format(date));
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、获取指定年月的月第一天和最后一天的时间戳

        /**
         * 获取指定年月的第一天和最后一天的时间戳
         * @param year
         * @param monthOfYear
         * @return
         */
        public static long getSupportBeginDayofMonth(int year, int monthOfYear) {
            Calendar cal = Calendar.getInstance();
            // 不加下面2行,就是取当前时间前一个月的第一天及最后一天
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
    
    		//月最后一天的时间戳
            cal.add(Calendar.DAY_OF_MONTH, -1);
            Date lastDate = cal.getTime();
    
    		//月第一天的时间戳
            cal.set(Calendar.DAY_OF_MONTH, 1);
            Date firstDate = cal.getTime();
            
            return firstDate.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

    5、获取当前时间的上个月同一时间的日期

    例子:
    当前时间是:2023-10-20 14:42:46 一个月前时间是:2023-09-20 14:42:46

    //计算最近一个月的起始时间
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String timeTo = format.format(new Date());
    String timeFrom = format.format(stepMonth(new Date(),-1));
    Log.d("mylog_finished_product","当前时间是:"+timeTo+" 一个月前时间是:"+timeFrom);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    /**
    * 给定日期加上或减去指定月份后的日期
    * @param sourceDate 原始时间
    * @param month 要调整的月份 时间向前为负数,向后为正数
    * @return
    */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    6、持续更新中…

  • 相关阅读:
    林业数据可视化新篇章:山海鲸软件看板设计心得
    C#WPF简单双精度动画应用实例
    写过的最蠢的代码
    【计算机网络】 集线器、网桥、交换机、路由器看这一篇就懂了。实验: 路由器的作用,以及没有路由器的情况下,如何用三层交换机实现路由器的功能
    Python面试宝典:Python中与爬虫基础以及数据抓取和解析相关的面试笔试题(1000加面试笔试题助你轻松捕获大厂Offer)
    LC655+offer070+offer069+offer072+662
    R数据分析:网络分析的做法,原理和复现方法
    mongodb数据迁移的方法
    TSP路径规划总结(常用解决方案 A*算法,蜂群算法,遗传算法,蚁群及其优化等)
    面试突击30:线程池是如何执行的?拒绝策略有哪些?
  • 原文地址:https://blog.csdn.net/qq_46269365/article/details/133945964