<button id="mgqzr"></button>
<noscript id="mgqzr"></noscript>
<button id="mgqzr"></button>
<pre id="mgqzr"><sup id="mgqzr"><sub id="mgqzr"></sub></sup></pre>
<noscript id="mgqzr"><b id="mgqzr"></b></noscript><button id="mgqzr"></button>

<noscript id="mgqzr"></noscript><button id="mgqzr"><b id="mgqzr"><th id="mgqzr"></th></b></button>
<pre id="mgqzr"><b id="mgqzr"></b></pre><button id="mgqzr"><menuitem id="mgqzr"><table id="mgqzr"></table></menuitem></button>
<pre id="mgqzr"><input id="mgqzr"></input></pre>

如何獲取java.util.Date中當前季度的第一個日期和最后一個日期 [英] How to get the first date and last date of current quarter in java.util.Date

查看:850
本文介紹了如何獲取java.util.Date中當前季度的第一個日期和最后一個日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要將當前季度的第一個日期作為java.util.Date對象,并將當前季度的最后日期作為java.util獲取。日期對象。

I need to get the first date of the current quarter as a java.util.Date object and the last date of the current quarter as a java.util.Date object.

我正在使用以下方法來獲取本月初次和本月最后一個日期。

I'm using following methods to get this month first date and this month last date.

private   Date getThisMonthFirstDate(){
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
}

private   Date getThisMonthLastDate(){
    Calendar calandar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.DAY_OF_MONTH,1);
    calendar.add(Calendar.MONTH, 1);
    calendar.add(Calendar.DATE, -1);
   return calendar.getTime();
}

有沒有辦法修改該函數來實現這個或任何人都可以指出一個更好的方法?

Is there a way to modify that function to achieve this or could anyone point out a better way?

假設Q1 = Jan Feb Mar,Q2 = Apr,May,Jun等。

Assume that Q1 = Jan Feb Mar, Q2 = Apr, May, Jun, etc.

推薦答案

private static Date getFirstDayOfQuarter(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)/3 * 3);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    return cal.getTime();
}

private static Date getLastDayOfQuarter(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)/3 * 3 + 2);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    return cal.getTime();
}

這應該工作,但我建議你比我做的更仔細地測試:)
注意:Java中的int值的分割返回底層值。

This should work, but I suggest you to test it more carefully than I did :) Note: Division of int values in Java returns the floor value.

這篇關于如何獲取java.util.Date中當前季度的第一個日期和最后一個日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
相關文章
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
亚洲AV无码国产精品
<button id="mgqzr"></button>
<noscript id="mgqzr"></noscript>
<button id="mgqzr"></button>
<pre id="mgqzr"><sup id="mgqzr"><sub id="mgqzr"></sub></sup></pre>
<noscript id="mgqzr"><b id="mgqzr"></b></noscript><button id="mgqzr"></button>

<noscript id="mgqzr"></noscript><button id="mgqzr"><b id="mgqzr"><th id="mgqzr"></th></b></button>
<pre id="mgqzr"><b id="mgqzr"></b></pre><button id="mgqzr"><menuitem id="mgqzr"><table id="mgqzr"></table></menuitem></button>
<pre id="mgqzr"><input id="mgqzr"></input></pre>