上个月,上一年时间

Java与数据结构相关探讨
public static void main(String [] args){
    //当年
    String year = Year.now().toString();
    //去年
    String jyear = Year.now().plusYears(-1).toString();
    //上个月
    String a = DateUtil.lastMonth();
    //上个月第一天
    String b  = DateUtil.lastMonthFirstDay();
    //上个月最后一天
    String c = DateUtil.lastMonthLastDay();
    //上个年第一天
    String d = DateUtil.yearAndOneTime(new Date());
    //上个年最后一天
    String e = DateUtil.yearAndLastTime(new Date());
    String nowMonth = DateUtil.lastMonth();
    String range = nowMonth.substring(0,4);
    //上三月第一天
    String startTime  = range+"-01-01 00:00:00";
    System.out.println(nowMonth);
    System.out.println(range);
    System.out.println(startTime);

}

/**
 * 获取上个月份
 * yyyy-MM
 *
 * @return
 */
public static String lastMonth() {
    return YearMonth.now().plusMonths(-1).toString();
}

/**
 * 获取上个月份第一天
 * yyyy-MM-dd
 *
 * @return
 */
public static String lastMonthFirstDay() {
    LocalDate lastMonthDate = LocalDate.now().plusMonths(-1);
    return lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()).toString();
}

/**
 * 获取三个月份第一天
 * yyyy-MM-dd
 *
 * @return
 */
public static String lastThreeMonthFirstDay() {
    LocalDate lastMonthDate = LocalDate.now().plusMonths(-3);
    return lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()).toString();
}

/**
 * 获取六个月份第一天
 * yyyy-MM-dd
 *
 * @return
 */
public static String lastSixMonthFirstDay() {
    LocalDate lastMonthDate = LocalDate.now().plusMonths(-6);
    return lastMonthDate.with(TemporalAdjusters.firstDayOfMonth()).toString();
}


/**
 * 获取上个月份最后一天
 * yyyy-MM-dd
 *
 * @return
 */
public static String lastMonthLastDay() {
    LocalDate lastMonthDate = LocalDate.now().plusMonths(-1);
    return lastMonthDate.with(TemporalAdjusters.lastDayOfMonth()).toString();
}


/**
 * 年加一年的第一天
 * 格式pattern
 *
 * @param
 * @return
 */
public static String yearAndOneTime(Date fixedTime) {
    //时间格式化
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-01-01");
    //使用Calendar来实现加减1小时,1天,1个月和1年
    Calendar calendar = Calendar.getInstance();
    //指定时间加1年
    calendar.setTime(fixedTime);
    calendar.add(Calendar.YEAR, -1);
    System.out.println(String.format("1年之后的时间:%s", dateFormat.format(calendar.getTime())));
    return dateFormat.format(calendar.getTime());
}



/**
 * 年加一年的最后一天
 * 格式pattern
 *
 * @param
 * @return
 */
public static String yearAndLastTime(Date fixedTime) {
    //时间格式化
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-12-31");
    //使用Calendar来实现加减1小时,1天,1个月和1年
    Calendar calendar = Calendar.getInstance();
    //指定时间加1年
    calendar.setTime(fixedTime);
    calendar.add(Calendar.YEAR, -1);
    System.out.println(String.format("1年之后的时间:%s", dateFormat.format(calendar.getTime())));
    return dateFormat.format(calendar.getTime());
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值