Created
January 31, 2023 11:22
-
-
Save Takuchan/762ee6fc7e1889eb377865ab5e48c2fc to your computer and use it in GitHub Desktop.
JavaでCalendarを使って月の最初の日と最後の日を返すプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 月初日を返す | |
public static Date getFirstDate(Date date) { | |
if (date==null) return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
int first = calendar.getActualMinimum(Calendar.DATE); | |
calendar.set(Calendar.DATE, first); | |
calendar.set(Calendar.HOUR_OF_DAY, 00); | |
calendar.set(Calendar.MINUTE, 00); | |
calendar.set(Calendar.SECOND, 00); | |
calendar.set(Calendar.MILLISECOND, 000); | |
return calendar.getTime(); | |
} | |
// 月末日を返す | |
public static Date getLastDate(Date date) { | |
if (date==null) return null; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
int last = calendar.getActualMaximum(Calendar.DATE); | |
calendar.set(Calendar.DATE, last); | |
calendar.set(Calendar.HOUR_OF_DAY, 23); | |
calendar.set(Calendar.MINUTE, 59); | |
calendar.set(Calendar.SECOND, 59); | |
calendar.set(Calendar.MILLISECOND, 999); | |
return calendar.getTime(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment