Java - Check if array contains 3 consecutive dates
嗨,我有一个字符串[]数组,其中包含格式为YYYY/MM/DD的日期。我想迭代此数组,看看数组中接下来的2个元素是否包含连续的日期。如果是这样,只需增加count变量。这是我到目前为止所拥有的。我基本上只需要有关检查是否有3个连续日期的if语句的帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int count = 0; String[] dates = { "2004/1/23","2004/1/24","2004/1/25", "2004/1/26","2004/1/29","2004/2/11", "2004/2/17","2004/2/18","2004/2/18","2004/3/7"}; for(int i = 0; i < dates.length-2; i++){ //Help needed here! If i, i+1 and i+2 are consecutive... if(...){ count++; } } |
我意识到在比较之前,可能需要将字符串日期转换为实际日期对象。如有进一步指导,将不胜感激。谢谢
将
现在,您可以使用如下方法检查连续日期:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Calendar c = Calendar.getInstance(); int numConsecutive = 0; Date last = null; for (int i = 0; i < dates.length; i++) { c.setTime(dates[i]); c.add(Calendar.DATE, -1); if (c.getTime().equals(last)) { numConsecutive++; } else { numConsecutive = 0; } if (numConsecutive == 2) { numConsecutive = 0; count++; } last = dates[i]; } |
号
DR
使用
1 | if ( z.minusDays ( 1 ).isEqual ( y ) && z.minusDays ( 2 ).isEqual ( x ) ) … |
。使用java.time
现代方法使用java.time类,而不是麻烦的旧遗留类
首先将数组转换为
1 2 3 4 5 6 7 8 9 10 11 | String[] dates = { "2004/1/23" ,"2004/1/24" ,"2004/1/25" , "2004/1/26" ,"2004/1/29" ,"2004/2/11" , "2004/2/17" ,"2004/2/18" ,"2004/2/19" ,"2004/3/7" }; DateTimeFormatter f = DateTimeFormatter.ofPattern ("uuuu/M/d" ); List < LocalDate > localdates = new ArrayList <> ( dates.length ); for ( String input : dates ) { LocalDate ld = LocalDate.parse ( input, f ); localdates.add ( ld ); } |
循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Integer tripletCount = 0; List< LocalDate > tripletDates = new ArrayList<>(); LocalDate x = null; LocalDate y = null; for ( LocalDate z : localdates ) { if ( null == x ) { x = z; continue; } if ( null == y ) { y = z; continue; } if ( z.minusDays ( 1 ).isEqual ( y ) && z.minusDays ( 2 ).isEqual ( x ) ) { tripletCount= ( tripletCount + 1 ); tripletDates.add( z ); } // Prepare for next loop. x = y ; y = z ; } |
。
转储到控制台。
1 2 3 |
。
localdates: [2004-01-23, 2004-01-24, 2004-01-25, 2004-01-26, 2004-01-29, 2004-02-11, 2004-02-17, 2004-02-18, 2004-02-19, 2004-03-07]
tripletCount: 3
tripletDates: [2004-01-25, 2004-01-26, 2004-02-19]
号关于java.time
JavaTimeFr框架是在Java 8和之后构建的。这些类取代了麻烦的旧遗留日期时间类,如
现在处于维护模式的joda time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle教程。以及搜索堆栈溢出以获得许多示例和解释。规格为JSR 310。
在哪里获取java.time类?
- Java SE 8、Java SE 9及以后
- 内置。
- 标准JAVA API的一部分与捆绑实现。
- Java 9增加了一些次要的特性和修复。
- Java SE 6和Java SE 7
- 大部分JavaTimeActudio都被移植到TealEnter后端的Java 6和7中。
- 安卓
- threetenabp项目专门为Android调整threeten backport(如上所述)。
- 看看如何使用三连珠……
threeten额外项目使用额外的类扩展java.time。这个项目是将来可能添加到java.time的一个试验场。您可以在这里找到一些有用的类,如
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 | @Test public void threeConsecutiveDates() throws ParseException { List<Date> consecutive = new ArrayList<>(); consecutive.add(new Date(0)); final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String[] dates = { "2004/1/23","2004/1/24","2004/1/25", "2004/1/26","2004/1/29","2004/2/11", "2004/2/17","2004/2/18","2004/2/18","2004/3/7"}; for (String s : dates) { Date previous = consecutive.get(consecutive.size()-1); Date current = format.parse(s); if(previous.before(current) && (current.getTime()-previous.getTime() == 1000 * 60 * 60 * 24)) { consecutive.add(current); } else { consecutive.clear(); consecutive.add(current); } if(consecutive.size() == 3) { break; } } System.out.println("consecutive =" + consecutive); } |
您好,您需要计算两个日期之间的秒数,然后转换为天数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.*; class DateDiff { public static void main(String [] args) { Calendar c1=Calendar.getInstance(); c1.set(2011,5, 29 ); Calendar c2=Calendar.getInstance(); c2.set(2012,5,30); Date d1=c1.getTime(); Date d2=c2.getTime(); long diff=d2.getTime()-d1.getTime(); int noofdays=(int)(diff/(1000*24*60*60)); System.out.println(noofdays); } } |