zoukankan      html  css  js  c++  java
  • java获取当月第一天时间、当月最后一天时间、上月第一天时间、上月最后一天时间

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    public class test {
        public static void main(String[] args) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            // 本月起始
            Calendar thisMonthFirstDateCal = Calendar.getInstance();
            thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
            String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00";
            System.out.println("本月起始:" + thisMonthFirstTime);
    
            // 本月末尾
            Calendar thisMonthEndDateCal = Calendar.getInstance();
            thisMonthEndDateCal.set(Calendar.DAY_OF_MONTH, thisMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
            String thisMonthEndTime = format.format(thisMonthEndDateCal.getTime()) + " 23:59:59";
            System.out.println("本月末尾:" + thisMonthEndTime);
    
            // 上月起始
            Calendar lastMonthFirstDateCal = Calendar.getInstance();
            lastMonthFirstDateCal.add(Calendar.MONTH,-1);
            lastMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
            String lastMonthFirstTime = format.format(lastMonthFirstDateCal.getTime()) + " 00:00:00";
            System.out.println("上月起始:" + lastMonthFirstTime);
    
            // 上月末尾
            Calendar lastMonthEndDateCal = Calendar.getInstance();
            lastMonthEndDateCal.add(Calendar.MONTH,-1);
            lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
            String lastMonthEndTime = format.format(lastMonthEndDateCal.getTime()) + " 23:59:59";
            System.out.println("上月末尾:" + lastMonthEndTime);
        }
    }
  • 相关阅读:
    Authentication for the REST APIs
    Authentication for the REST APIs
    泛型转Datatable
    Web API 返回json文件的2中不用方式
    Robotframework自定义关键字库
    python通过接口上传图片造测试数据
    robot framework(2) 环境搭建
    RobotFrameWork(1) 关键字驱动测试框架
    python发送带附件的邮件
    解决adb连接海马玩模拟器
  • 原文地址:https://www.cnblogs.com/daihu/p/13224829.html
Copyright © 2011-2022 走看看