Java获取时间,将当前时间减一天、一月、一年,并加以格式化

  • 一、普遍例子
  • 1、代码
  • 2、效果
  • 二、自定义方法
  • 1、代码
  • 2、效果
  • 三、自定义工具类
  • 1、代码
  • 2、工具类
  • 3、效果
  • 四、补充
  • 1、Calendar.add()方法参数
  • 2、String转换成Date后格式化成自定义时间格式的String类型



一、普遍例子

1、代码

void contextLoads() {
        Date date = new Date();//获取当前时间
        System.out.println(date);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式

        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天,即一天前的时间
        System.out.println(simpleDateFormat.format(calendar.getTime()));

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.MONTH, -1);//当前时间减去一个月,即一个月前的时间
        System.out.println(simpleDateFormat.format(calendar2.getTime()));

        Calendar calendar3 = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);//当前时间减去一年,即一年前的时间
        System.out.println(simpleDateFormat.format(calendar3.getTime()));

        System.out.println(calendar.getTimeInMillis());//返回当前时间的毫秒数
    }

2、效果

java 日期减去一个月 java当前日期减去一月_System

二、自定义方法

1、代码

/**
     * 获取某天的时间,支持自定义时间格式
     *
     * @param simpleDateFormat 时间格式,yyyy-MM-dd HH:mm:ss
     * @param index            为正表示当前时间加天数,为负表示当前时间减天数
     * @return String
     */
    public static String getTimeDay(String simpleDateFormat, int index) {
        //设置时区
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat(simpleDateFormat);
        calendar.add(Calendar.DAY_OF_MONTH, index);
        String date = fmt.format(calendar.getTime());
        return date;
    }

    /**
     * 第二种使用自定义方法
     */
    void contextLoads2() {
        System.out.println(getTimeDay("yyyy-MM-dd HH:mm:ss", -1));
    }

2、效果

java 日期减去一个月 java当前日期减去一月_后端_02

三、自定义工具类

1、代码

void contextLoads3() {
        Date now = DateUtil.now();
        System.out.println("今天时间: " + DateUtil.formatDateTime(now));
        System.out.println("昨天时间: " + DateUtil.formatDateTime(DateUtil.addDate(now, -1)));
    }

2、工具类

详细可私聊

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

/**
 * @author lichangyuan
 * @create 2021-12-15 11:30
 */
public class DateUtil {

    public DateUtil() {
    }
    
    public static Date now() {
        return new Date();
    }
    public static Date add(Date date, Integer field, Integer amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    public static Date addDate(Date date, Integer days) {
        return add(date, 5, days);
    }

    public static String format(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            if (isEmpty(format)) {
                format = "yyyy-MM-dd";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

    public static String formatDateTime(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }
    
    /**
     * 计算两个日期相差天数
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) {
        long betweenDays = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return Integer.parseInt(String.valueOf(betweenDays));
    }
}

3、效果

java 日期减去一个月 java当前日期减去一月_System_03

四、补充

1、Calendar.add()方法参数

数字

对应操作

1

年份

2

月份

3

星期

5

日期

11

小时

12

分钟

13


14

毫秒

2、String转换成Date后格式化成自定义时间格式的String类型

void test12() {
        //SimpleDateFormat中的parse方法可以把特定格式的String型的字符串转换成date类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date newDate = sdf.parse("2022-12-01 13:00:00");
            System.out.println(newDate);
            
            
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy:MM:dd HH-mm-ss");
            String format = sdf2.format(newDate);
            System.out.println(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
    }

java 日期减去一个月 java当前日期减去一月_后端_04

  1. 我们用人生最好的年华抵押,去担保一个说出来都会被嘲笑的梦想。
  2. 当黄昏落在身上,没有烟火,没有杂尘,只有光和希望。
  3. 大路走尽还有数不尽的小路,只要不停地走就有数不尽的风光。
  4. “和你—起努力”胜过—万句“我喜欢你”。
  5. 当你觉得为时已晚的时候,恰恰是最早重振旗鼓的时候。
  6. 当你眼前—片漆黑不要害怕,因为你就是那个发光体。
  7. 谁能─开始对爱就擅长,谁能才刚启程就有方向。
  8. 无论今天多么浑浊不堪,明天依l日如约而至。
  9. 在为了摘到最亮的星星的努力着,也为当下的每一份美好心情。
  10. 我不再要求任何人为我的痛苦做出改变,我直接去为我的幸福做事情。