Java:常用日期类操作

郎家岭伯爵 2023年07月06日 250次浏览

前言

Java 中常用的日期类操作。

实现

获取前XX时间

例如常用的“获取前一年时间”、“获取前一月时间”等操作。

package com.langjialing.helloworld.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/6 13:59
 */
@RestController
@RequestMapping("/date")
@Slf4j
public class DateController {
    @GetMapping("/date")
    public void getDate(){
        // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date0 = sdf.format(new Date());
        log.info("当前时间为:{}",date0);

        // 获取前一年时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        String date1 = sdf.format(calendar.getTime());
        log.info("前一年时间为:{}",date1);

        // 获取前一月时间
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        String date2 = sdf.format(calendar1.getTime());
        log.info("前一月时间为:{}",date2);

        // 获取前一周时间
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.WEEK_OF_YEAR, -1);
        String date3 = sdf.format(calendar2.getTime());
        log.info("前一周时间为:{}",date3);

        // 获取前一天时间
        Calendar calendar3 = Calendar.getInstance();
        calendar3.add(Calendar.DAY_OF_YEAR, -1);
        String date4 = sdf.format(calendar3.getTime());
        log.info("前一天时间为:{}",date4);

        // 获取前一小时时间
        Calendar calendar4 = Calendar.getInstance();
        calendar4.add(Calendar.HOUR_OF_DAY, -1);
        String date5 = sdf.format(calendar4.getTime());
        log.info("前一小时时间为:{}",date5);
    }
}

输出:

当前时间为:2023年07月06日 14:07:31
前一年时间为:2022年07月06日 14:07:31
前一月时间为:2023年06月06日 14:07:31
前一周时间为:2023年06月29日 14:07:31
前一天时间为:2023年07月05日 14:07:31
前一小时时间为:2023年07月06日 13:07:31

获取前两年(月、日)时间

例如常用的“获取前两年时间”、“获取前两月时间”等操作。

package com.langjialing.helloworld.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/6 13:59
 */
@RestController
@RequestMapping("/date")
@Slf4j
public class DateController {
    @GetMapping("/date")
    public void getDate(){
        // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date0 = sdf.format(new Date());
        log.info("当前时间为:{}",date0);

        // 获取前两年的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -2);
        String date1 = sdf.format(calendar.getTime());
        log.info("前两年的时间为:{}",date1);
        
        // 获取前一年的时间
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.YEAR, -1);
        String date2 = sdf.format(calendar1.getTime());
        log.info("前一年的时间为:{}",date2);
        
        // 获取前三个月的时间
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.MONTH, -3);
        String date3 = sdf.format(calendar2.getTime());
        log.info("前三个月的时间为:{}",date3);
        
        // 获取前一周的时间
        Calendar calendar3 = Calendar.getInstance();
        calendar3.add(Calendar.WEEK_OF_YEAR, -1);
        String date4 = sdf.format(calendar3.getTime());
        log.info("前一周的时间为:{}",date4);
        
        // 获取前一天的时间
        Calendar calendar4 = Calendar.getInstance();
        calendar4.add(Calendar.DAY_OF_YEAR, -1);
        String date5 = sdf.format(calendar4.getTime());
        log.info("前一天的时间为:{}",date5);
    }
}

输出:

当前时间为:2023年07月06日 14:32:47
前两年的时间为:2021年07月06日 14:32:47
前一年的时间为:2022年07月06日 14:32:47
前三个月的时间为:2023年04月06日 14:32:47
前一周的时间为:2023年06月29日 14:32:47
前一天的时间为:2023年07月05日 14:32:47

获取最后一天

例如常用的“获取上年最后一天”、“获取上月最后一天”等操作。

package com.langjialing.helloworld.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/6 13:59
 */
@RestController
@RequestMapping("/date")
@Slf4j
public class DateController {
    @GetMapping("/date")
    public void getDate(){
        // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date0 = sdf.format(new Date());
        log.info("当前时间为:{}",date0);

        // 获取上年最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        Date date1 = calendar.getTime();
        String date2 = sdf.format(date1);
        log.info("上年最后一天为:{}",date2);

        // 获取上年第一天
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.YEAR, -1);
        calendar1.set(Calendar.DAY_OF_YEAR, calendar1.getActualMinimum(Calendar.DAY_OF_YEAR));
        Date date3 = calendar1.getTime();
        String date4 = sdf.format(date3);
        log.info("上年第一天为:{}",date4);

        // 获取上月最后一天
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.MONTH, -1);
        calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date date5 = calendar2.getTime();
        String date6 = sdf.format(date5);
        log.info("上月最后一天为:{}",date6);

        // 获取上月第一天
        Calendar calendar3 = Calendar.getInstance();
        calendar3.add(Calendar.MONTH, -1);
        calendar3.set(Calendar.DAY_OF_MONTH, calendar3.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date date7 = calendar3.getTime();
        String date8 = sdf.format(date7);
        log.info("上月第一天为:{}",date8);

        // 获取本月最后一天
        Calendar calendar4 = Calendar.getInstance();
        calendar4.set(Calendar.DAY_OF_MONTH, calendar4.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date date9 = calendar4.getTime();
        String date10 = sdf.format(date9);
        log.info("本月最后一天为:{}",date10);

        // 获取本月第一天
        Calendar calendar5 = Calendar.getInstance();
        calendar5.set(Calendar.DAY_OF_MONTH, calendar5.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date date11 = calendar5.getTime();
        String date12 = sdf.format(date11);
        log.info("本月第一天为:{}",date12);
    }
}

输出:

当前时间为:2023年07月06日 14:17:15
上年最后一天为:2022年12月31日 14:17:15
上年第一天为:2022年01月01日 14:17:15
上月最后一天为:2023年06月30日 14:17:15
上月第一天为:2023年06月01日 14:17:15
本月最后一天为:2023年07月31日 14:17:15
本月第一天为:2023年07月01日 14:17:15

获取当前年、月、日

获取当前时间的年、月、日、小时、分钟等操作。

package com.langjialing.helloworld.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/6 13:59
 */
@RestController
@RequestMapping("/date")
@Slf4j
public class DateController {
    @GetMapping("/date")
    public void getDate(){
        // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date0 = sdf.format(new Date());
        log.info("当前时间为:{}",date0);

        // 获取当前年份
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        String date1 = sdf1.format(new Date());
        log.info("当前年份为:{}",date1);

        // 获取当前月份
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
        String date2 = sdf2.format(new Date());
        log.info("当前月份为:{}",date2);

        // 获取当前日
        SimpleDateFormat sdf3 = new SimpleDateFormat("dd");
        String date3 = sdf3.format(new Date());
        log.info("当前日为:{}",date3);

        // 获取当前小时
        SimpleDateFormat sdf4 = new SimpleDateFormat("HH");
        String date4 = sdf4.format(new Date());
        log.info("当前小时为:{}",date4);

        // 获取当前分钟
        SimpleDateFormat sdf5 = new SimpleDateFormat("mm");
        String date5 = sdf5.format(new Date());
        log.info("当前分钟为:{}",date5);
    }
}

输出:

当前时间为:2023年07月06日 14:21:37
当前年份为:2023
当前月份为:07
当前日为:06
当前小时为:14
当前分钟为:21

获取指定年月的最后一天

例如”获取2023年3月的最后一天”。

package com.langjialing.helloworld.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 郎家岭伯爵
 * @time 2023/7/6 13:59
 */
@RestController
@RequestMapping("/date")
@Slf4j
public class DateController {
    @GetMapping("/date")
    public void getDate(){
        // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date0 = sdf.format(new Date());
        log.info("当前时间为:{}",date0);

        // 获取指定年月的最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,2023);
        calendar.set(Calendar.MONTH,3);
        calendar.set(Calendar.DAY_OF_MONTH,0);
        String date1 = sdf.format(calendar.getTime());
        log.info("2023年3月的最后一天为:{}",date1);
    }
}

输出:

当前时间为:2023年07月06日 14:35:27
2023年3月的最后一天为:2023年03月31日 14:35:27

总结

Java 中常用日期类的操作。

赞助页面示例