이 기사의 예에서는 매일 두 날짜 사이를 순회하는 데 주로 사용되는 JAVA 시간 및 날짜 처리 클래스에 대해 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부사항은 다음과 같습니다:
/*** * 파일 이름: AccountDate.java* * 생성 시간: 2008-11-18** 이메일: **@163.com*/import java.text.DecimalFormat;import java.text.ParseException;import java. text.SimpleDateFormat;가져오기 java.util.ArrayList;가져오기 java.util.Date;가져오기 java.util.List;공용 클래스 AccountDate { 개인 static temporary int gregorianCutoverYear = 1582; /** 윤년의 월별 일수*/ private static final int[] DAYS_P_MONTH_LY= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31 , 30, 31} ; /** 윤년이 아닌 해의 월별 일수*/ private static final int[] DAYS_P_MONTH_CY= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} /** 배열의 연도, 월, 일을 나타냅니다.*/ private static final int Y = 0, M = 1, D = 2; /** * 날짜를 나타내는 문자열을 연도, 월, 일을 나타내는 정수 배열로 분할합니다* @param 날짜 * @return */ public static int[] SplitYMD(String date){ date = date.replace("-", "") int[] ymd = {0, 0, 0}; = Integer.parseInt(date.substring(0, 4)); ymd[M] = Integer.parseInt(date.substring(4, 6)); ymd[D] = Integer.parseInt(date.substring(6, 8)); return ymd; } /** * 전달된 매개변수가 나타내는 연도가 윤년인지 확인* @param year * @return */ public static boolean isLeapYear(int year) { 반환 연도 >= gregorianCutoverYear ? ((연도%4 == 0) && ((연도%100 != 0) || (연도%400 == 0))) : (연도%4 == 0) } /** * 날짜 더하기 1일* @param 연도 * @param 월 * @param 일 * @return */ private static int[] addOneDay(int 연도, int 월, int 일){ if(isLeapYear( 연도 )){ day++; if( day > DAYS_P_MONTH_LY[month -1 ] ){ 월++; 월 = 1; } }else{ 일++; if( 월 > 12){ 월++ = 1; } 일 = 1; } } int[] ymd = {연도, 월, 일} } /** * 2자리 미만의 월 또는 일을 2자리로 보완합니다. * @param 십진수 * @return */ public static String formatMonthDay(intdecimal){ DecimalFormat df = new DecimalFormat("00") return df.format(decimal ); } /** * 연도를 4자리 미만에서 4자리로 보완 * @paramdecimal * @return */ public static String formatYear(intdecimal){ DecimalFormat df = new DecimalFormat("0000"); return df.format( 소수 ); } /** * 두 날짜 사이의 일 수 계산 * @param start * @param end * @throws ParseException */ public static long countDay ( String start,String end){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 날짜 시작 날짜, endDate; = 0; 시도 { BeginDate= format.parse(begin); endDate= format.parse(end); day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000) catch (ParseException e) { e.printStackTrace(); } return day } /** * 루프에서 날짜 계산* @param startDate endDate * @param days * @return */ public static List<String> getEveryday(String BeginDate, String endDate){ long days = countDay(beginDate, endDate) int[] ymd = SplitYMD(beginDate) = new ArrayList<String>(); EveryDays.add(beginDate); for(int i = 0; i < days; i++) ymd = addOneDay(ymd[Y], ymd[M], ymd[D]); EveryDays.add(formatYear(ymd[Y])+"-"+formatMonthDay(ymd[M])+"-"+formatMonthDay( ymd[D])); } return EveryDays } public static void main(String[] args) { List<String> 목록 = AccountDate.getEveryday("2008-08-29", "2008-09-02") for (String result : list) { System.out.println(result) } }
이 글이 모든 사람의 Java 프로그래밍에 도움이 되기를 바랍니다.