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