java时间格式处理

1、每月1号取前一个月所在月份

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        return LocalDate.now().getDayOfMonth() == 1 ?
                //获取当前月份-1
                formatter.format(LocalDate.now().minusMonths(1)) :
                formatter.format(LocalDate.now());

2、获取当前日期所在月

Calendar now = Calendar.getInstance();
        String year = now.get(Calendar.YEAR) + "";
        int mon = (now.get(Calendar.MONTH) + 1);
        String month = mon + "";
        if (mon < 10) {
            month = "0" + mon;
        }
        return year + month;

3、获取指定日之前所在日(yyyy-MM-dd)

Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String mDateTime = formatter.format(c.getTime());
        return mDateTime;

4、查询T-1日是周几

public Map<String, String> getTimeByLatitude() {
        Date date = new Date();
        SimpleDateFormat simdf = new SimpleDateFormat("YYYY-MM-dd");
        Map<String, String> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        //获取要查询的T-1日
        map.put("lastDate", simdf.format(calendar.getTime()));
        //获取要查询的T-1日对应的月份
        map.put("month", simdf.format(calendar.getTime()).substring(0, 7));
        int day = calendar.get(Calendar.DATE);
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }
        calendar.add(Calendar.DATE, -day_of_week + 1);
        //获取要查询的T-1日对应的周一
        map.put("weekHand", simdf.format(calendar.getTime()));
        return map;
    }

5、获取指定月份以及格式

private String getLastMonth(int i, int style) {
        SimpleDateFormat format;
        if (style == 1) {
            format = new SimpleDateFormat("yyyyM");
        } else {
            format = new SimpleDateFormat("yyyyMM");
        }
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - i);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值