Java获取时间如何将当前时间减一天、一月、一年、并格式化
作者:李长渊哦
这篇文章主要介绍了Java获取时间,将当前时间减一天、一月、一年,并加以格式化,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Java获取时间,将当前时间减一天、一月、一年,并加以格式化
一、普遍例子
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、效果

二、自定义方法
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、效果

三、自定义工具类
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、效果

四、补充
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当前时间减一天内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
