gpt4 book ai didi

JAVA时间日期处理类实例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章JAVA时间日期处理类实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了JAVA时间日期处理类,主要用来遍历两个日期之间的每一天。分享给大家供大家参考。具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
*
* 文 件 名: AccountDate.java
*
* 创建时间: 2008-11-18
*
* Email : **@163.com
*/
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AccountDate {
  private static transient 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 date
  * @return
  */
  public static int [] splitYMD(String date){
   date = date.replace( "-" , "" );
   int [] ymd = { 0 , 0 , 0 };
   ymd[Y] = 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) {
   return year >= gregorianCutoverYear ?
    ((year% 4 == 0 ) && ((year% 100 != 0 ) || (year% 400 == 0 ))) : (year% 4 == 0 );
  }
  /**
  * 日期加1天
  * @param year
  * @param month
  * @param day
  * @return
  */
  private static int [] addOneDay( int year, int month, int day){
   if (isLeapYear( year )){
    day++;
    if ( day > DAYS_P_MONTH_LY[month - 1 ] ){
     month++;
     if (month > 12 ){
      year++;
      month = 1 ;
     }
     day = 1 ;
    }
   } else {
    day++;
    if ( day > DAYS_P_MONTH_CY[month - 1 ] ){
     month++;
     if (month > 12 ){
      year++;
      month = 1 ;
     }
     day = 1 ;
    }
   }
   int [] ymd = {year, month, day};
   return ymd;
  }
  /**
  * 将不足两位的月份或日期补足为两位
  * @param decimal
  * @return
  */
  public static String formatMonthDay( int decimal){
   DecimalFormat df = new DecimalFormat( "00" );
   return df.format( decimal );
  }
  /**
  * 将不足四位的年份补足为四位
  * @param decimal
  * @return
  */
  public static String formatYear( int decimal){
   DecimalFormat df = new DecimalFormat( "0000" );
   return df.format( decimal );
  }
  /**
  * 计算两个日期之间相隔的天数
  * @param begin
  * @param end
  * @return
  * @throws ParseException
  */
  public static long countDay(String begin,String end){
    SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" );
    Date beginDate , endDate;
    long day = 0 ;
    try {
    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 beginDate endDate
  * @param days
  * @return
  */
  public static List<String> getEveryday(String beginDate , String endDate){
   long days = countDay(beginDate, endDate);
   int [] ymd = splitYMD( beginDate );
   List<String> everyDays = 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> list = AccountDate.getEveryday( "2008-08-29" , "2008-09-02" );
   for (String result : list) {
    System.out.println(result);
   }
  }
}

希望本文所述对大家的java程序设计有所帮助.

最后此篇关于JAVA时间日期处理类实例的文章就讲到这里了,如果你想了解更多关于JAVA时间日期处理类实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com