Java如何獲取當(dāng)前年份、月份和日期字符串
Java獲取當(dāng)前年份、月份和日期是通過(guò)Calendar類的實(shí)例對(duì)象來(lái)獲取的。
首先創(chuàng)建一個(gè)Calendar類的實(shí)例對(duì)象,Calendar類屬于java.util包。
Calendar calendar = Calendar.getInstance();
獲取當(dāng)前年份、月份和日期等。
// 獲取當(dāng)前年 int year = calendar.get(Calendar.YEAR); // 獲取當(dāng)前月 int month = calendar.get(Calendar.MONTH) + 1; // 獲取當(dāng)前日 int day = calenedar.get(Calendar.DATE); // 獲取當(dāng)前小時(shí) int hour = calendar.get(Calendar.HOUR_OF_DAY); // 獲取當(dāng)前分鐘 int minute = calendar.get(Calendar.MINUTE); // 獲取當(dāng)前秒 int second = calendar.get(Calendar.SECOND); // 獲取當(dāng)前是本周第幾天 int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 獲取當(dāng)前是本月第幾天 int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // 獲取當(dāng)前是本年第幾天 int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
獲取當(dāng)月的第一天和最后一天的字符串。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 獲取當(dāng)月第一天 calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); String firstday = format.format(calendar.getTime()); // 獲取當(dāng)月最后一天 calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 0); String lastday = format.format(calendar.getTime()); // 打印結(jié)果字符串 System.out.println("本月第一天和最后一天分別是:" + firstday + " 和 " + lastday + "。");
另外也可以使用Date類的實(shí)例對(duì)象配合SimpleDateFormat類的實(shí)例對(duì)象來(lái)獲取當(dāng)前日期字符串。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); System.out.println("當(dāng)前日期字符串:" + format.format(date) + "。");
獲取上個(gè)星期第一天的某個(gè)小時(shí)的時(shí)間戳
public static Long getPreviousWeekday(int hour) { int week = -1; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(Calendar.DATE, mondayPlus + 7 * week); Date monday = currentDate.getTime(); TimeZone curTimeZone = TimeZone.getTimeZone("GMT+8"); Calendar c = Calendar.getInstance(curTimeZone); c.setTime(monday); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); // Date z = c.getTime(); // SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd); // System.out.println(sdf.format(z)); return c.getTime().getTime(); } public static int getMondayPlus() { Calendar cd = Calendar.getInstance(); int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; if (dayOfWeek == 1) { return 0; } return (1 - dayOfWeek); }
到此這篇關(guān)于Java獲取當(dāng)前年份、月份和日期字符串等的文章就介紹到這了,更多相關(guān)java獲取當(dāng)前年月日期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security 實(shí)現(xiàn)用戶名密碼登錄流程源碼詳解
在服務(wù)端的安全管理使用了Spring Security,用戶登錄成功之后,Spring Security幫你把用戶信息保存在Session里,但是具體保存在哪里,要是不深究你可能就不知道,今天小編就帶大家具體了解一下Spring Security實(shí)現(xiàn)用戶名密碼登錄的流程2021-11-11基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word
這篇文章主要介紹了基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java客戶端通過(guò)HTTPS連接到Easysearch實(shí)現(xiàn)過(guò)程
這篇文章主要為大家介紹了Java客戶端通過(guò)HTTPS連接到Easysearch實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11springboot3集成mybatis-plus報(bào)sqlSession異常的問(wèn)題解決
springboot3已經(jīng)發(fā)布正式版,但是在集成mybatis-plus最新版3.5.2的時(shí)候發(fā)現(xiàn)提示異常,本文就來(lái)介紹一下報(bào)sqlSession異常的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Spring boot AOP通過(guò)XML配置文件聲明的方法
這篇文章主要介紹了Spring boot AOP通過(guò)XML配置文件聲明,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06基于Spring MVC的文件上傳和下載實(shí)現(xiàn)方法
在Web應(yīng)用程序中,文件上傳和下載是常見(jiàn)的功能,Spring MVC框架提供了方便的方式來(lái)實(shí)現(xiàn)這些功能,本文將介紹如何使用Spring MVC實(shí)現(xiàn)文件上傳和下載,需要的朋友可以參考下2023-05-05