Java獲取時間如何將當(dāng)前時間減一天、一月、一年、并格式化
更新時間:2023年09月08日 14:32:01 作者:李長淵哦
這篇文章主要介紹了Java獲取時間,將當(dāng)前時間減一天、一月、一年,并加以格式化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
Java獲取時間,將當(dāng)前時間減一天、一月、一年,并加以格式化
一、普遍例子
1、代碼
void contextLoads() { Date date = new Date();//獲取當(dāng)前時間 System.out.println(date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置格式 Calendar calendar = Calendar.getInstance(); //創(chuàng)建Calendar 的實(shí)例 calendar.add(Calendar.DAY_OF_MONTH, -1); //當(dāng)前時間減去一天,即一天前的時間 System.out.println(simpleDateFormat.format(calendar.getTime())); Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.MONTH, -1);//當(dāng)前時間減去一個月,即一個月前的時間 System.out.println(simpleDateFormat.format(calendar2.getTime())); Calendar calendar3 = Calendar.getInstance(); calendar.add(Calendar.YEAR, -1);//當(dāng)前時間減去一年,即一年前的時間 System.out.println(simpleDateFormat.format(calendar3.getTime())); System.out.println(calendar.getTimeInMillis());//返回當(dāng)前時間的毫秒數(shù) }
2、效果
二、自定義方法
1、代碼
/** * 獲取某天的時間,支持自定義時間格式 * * @param simpleDateFormat 時間格式,yyyy-MM-dd HH:mm:ss * @param index 為正表示當(dāng)前時間加天數(shù),為負(fù)表示當(dāng)前時間減天數(shù) * @return String */ public static String getTimeDay(String simpleDateFormat, int index) { //設(shè)置時區(qū) 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、工具類
詳細(xì)可私聊
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"); } /** * 計算兩個日期相差天數(shù) * * @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、效果
四、補(bǔ)充
1、Calendar.add()方法參數(shù)
數(shù)字 | 對應(yīng)操作 |
---|---|
1 | 年份 |
2 | 月份 |
3 | 星期 |
5 | 日期 |
11 | 小時 |
12 | 分鐘 |
13 | 秒 |
14 | 毫秒 |
2、String轉(zhuǎn)換成Date后格式化成自定義時間格式的String類型
void test12() { //SimpleDateFormat中的parse方法可以把特定格式的String型的字符串轉(zhuǎn)換成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(); } }
到此這篇關(guān)于Java獲取時間,將當(dāng)前時間減一天、一月、一年,并加以格式化的文章就介紹到這了,更多相關(guān)java當(dāng)前時間減一天內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security使用單點(diǎn)登錄的權(quán)限功能
本文主要介紹了Spring Security使用單點(diǎn)登錄的權(quán)限功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Springboot訪問templates html頁面過程詳解
這篇文章主要介紹了Springboot訪問templates html頁面過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05java實(shí)現(xiàn)查找PDF關(guān)鍵字所在頁碼及其坐標(biāo)
這篇文章主要介紹了java實(shí)現(xiàn)查找PDF關(guān)鍵字所在頁碼及其坐標(biāo)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09java ArrayBlockingQueue的方法及缺點(diǎn)分析
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java ArrayBlockingQueue的方法及缺點(diǎn)分析,對此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-01-01Spring?Boot整合log4j2日志配置的詳細(xì)教程
這篇文章主要介紹了SpringBoot項目中整合Log4j2日志框架的步驟和配置,包括常用日志框架的比較、配置參數(shù)介紹、Log4j2配置詳解以及使用步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02