欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java如何獲取當前年份、月份和日期字符串

 更新時間:2024年03月02日 12:01:10   作者: Mr. Kang  
Java獲取當前年份、月份和日期是通過Calendar類的實例對象來獲取的,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧

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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺談Mybatis版本升級踩坑及背后原理分析

    淺談Mybatis版本升級踩坑及背后原理分析

    這篇文章主要介紹了淺談Mybatis版本升級踩坑及背后原理分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • java實現(xiàn)基因序列比較的示例代碼

    java實現(xiàn)基因序列比較的示例代碼

    這篇文章主要介紹了java實現(xiàn)基因序列比較的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • Spring Security 實現(xiàn)用戶名密碼登錄流程源碼詳解

    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

    這篇文章主要介紹了基于Freemarker和xml實現(xiàn)Java導出word,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程

    Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程

    這篇文章主要為大家介紹了Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • springboot3集成mybatis-plus報sqlSession異常的問題解決

    springboot3集成mybatis-plus報sqlSession異常的問題解決

    springboot3已經(jīng)發(fā)布正式版,但是在集成mybatis-plus最新版3.5.2的時候發(fā)現(xiàn)提示異常,本文就來介紹一下報sqlSession異常的問題解決,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Java線程等待用法實例分析

    Java線程等待用法實例分析

    這篇文章主要介紹了Java線程等待用法,結(jié)合實例形式分析了obj.wait()實現(xiàn)線程等待相關原理與操作技巧,需要的朋友可以參考下
    2018-09-09
  • Kafka中消息隊列的兩種模式講解

    Kafka中消息隊列的兩種模式講解

    這篇文章主要介紹了Kafka中消息隊列的兩種模式講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Spring boot AOP通過XML配置文件聲明的方法

    Spring boot AOP通過XML配置文件聲明的方法

    這篇文章主要介紹了Spring boot AOP通過XML配置文件聲明,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • 基于Spring MVC的文件上傳和下載實現(xiàn)方法

    基于Spring MVC的文件上傳和下載實現(xiàn)方法

    在Web應用程序中,文件上傳和下載是常見的功能,Spring MVC框架提供了方便的方式來實現(xiàn)這些功能,本文將介紹如何使用Spring MVC實現(xiàn)文件上傳和下載,需要的朋友可以參考下
    2023-05-05

最新評論