java 获取当月的第一天和最后一天,及一段时间内的工作日天数,按指定年月获取每一天,分钟转小时

直接上代码

//获取当月第一天和最后一天
Calendar cale = null;
cale = Calendar.getInstance();
// 获取当月第一天和最后一天
SimpleDateFormat formatTemp = new SimpleDateFormat("yyyy-MM-dd");
String firstday, lastday;
// 获取当前月的第一天
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = formatTemp.format(cale.getTime());
// 获取当前月的最后一天
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = formatTemp.format(cale.getTime());
System.out.println("firstday:"+firstday+" lastday:"+lastday);
String newDataUrl= dataUrl+"startDate="+firstday+"&"+"endDate"+lastday;

firstday:2020-04-01 lastday:2020-04-30

单独的算当前年和月,同样可以利用Calendar类

Calendar calendar = Calendar.getInstance();
System.out.println("开始时间是" + DateUtil.currentShortTime());
//获取当前年
int year = calendar.get(Calendar.YEAR);
//获取当前月份
int month = calendar.get(Calendar.MONTH +1);

补充:

根据开始时间和结束时间计算有效的工作日天数

public int getDutyDays(String strStartDate,String strEndDate) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate=null;
    Date endDate = null;

    try {
        startDate=df.parse(strStartDate);
        endDate = df.parse(strEndDate);
    } catch (ParseException e) {
        System.out.println("非法的日期格式,无法进行转换");
        e.printStackTrace();
    }
    int result = 0;
    while (startDate.compareTo(endDate) <= 0) {
        if (startDate.getDay() != 6 && startDate.getDay() != 0) {
            result++;
        }
        startDate.setDate(startDate.getDate() + 1);
    }

    return result;
}

获取某年指定月份的每一天

public List<String> getMonthFullDay(int year , int month){
    SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
    List<String> fullDayList = new ArrayList<>(32);
    // 获得当前日期对象
    Calendar cal = Calendar.getInstance();
    cal.clear();// 清除信息
    cal.set(Calendar.YEAR, year);
    // 1月从0开始
    cal.set(Calendar.MONTH, month-1 );
    // 当月1号
    cal.set(Calendar.DAY_OF_MONTH,1);
    int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    for (int j = 1; j <= count ; j++) {
        fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
        cal.add(Calendar.DAY_OF_MONTH,1);
    }
    return fullDayList;
}

[结果如下
        "2021-04-01",
        "2021-04-02",
        "2021-04-03",
        "2021-04-04",
        "2021-04-05",
        "2021-04-06",
        "2021-04-07",
        "2021-04-08",
        "2021-04-09",
        "2021-04-10",
        "2021-04-11",
        "2021-04-12",
        "2021-04-13",
        "2021-04-14",
        "2021-04-15",
        "2021-04-16",
        "2021-04-17",
        "2021-04-18",
        "2021-04-19",
        "2021-04-20",
        "2021-04-21",
        "2021-04-22",
        "2021-04-23",
        "2021-04-24",
        "2021-04-25",
        "2021-04-26",
        "2021-04-27",
        "2021-04-28",
        "2021-04-29",
        "2021-04-30"
    ]

将分转为小时,四舍五入保留一位小数

public static void main(String[] args) {
        double hour = new BigDecimal(260).divide(new BigDecimal(60), 1, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(hour);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值