- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if array contains three consecutive dates in java?
To check to find whether a given array contains three consecutive dates:
- Convert the given array into a list of type LocalDate.
- Using the methods of the LocalDate class compare ith, i+1th and i+1th, i+2th elements of the list if equal the list contain 3 consecutive elements.
Example
import java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class ConsicutiveDate { public static void main(String args[]) { String[] dates = {"5/12/2017", "6/12/2017", "7/12/2017"}; List<LocalDate> localDateList = new ArrayList<>(); for (int i = 0; i<dates.length; i++) { String[] data = dates[i].split("/"); Month m = Month.of(Integer.parseInt(data[1])); LocalDate localDate = LocalDate.of(Integer.parseInt(data[2]),m,Integer.parseInt(data[0])); localDateList.add(localDate); Date date = java.sql.Date.valueOf(localDate); } System.out.println("Contents of the list are ::"+localDateList); Collections.sort(localDateList); for (int i = 0; i < localDateList.size() - 1; i++) { LocalDate date1 = localDateList.get(i); LocalDate date2 = localDateList.get(i + 1); if (date1.plusDays(1).equals(date2)) { System.out.println("Consecutive Dates are: " + date1 + " and " + date2); } } } }
Output
Contents of the list are ::[2017-12-05, 2017-12-06, 2017-12-07] Consecutive Dates are: 2017-12-05 and 2017-12-06 Consecutive Dates are: 2017-12-06 and 2017-12-07
- Related Articles
- Check if three consecutive elements in an array is identical in JavaScript
- Check if list contains consecutive numbers in Python
- How to check if two dates are equal in Java 8?
- Java Program to Check if An Array Contains a Given Value
- Java Program to Check if An Array Contains the Given Value
- How to check if ArrayList contains an item in Java?
- How to check if an array contains integer values in JavaScript ?
- Check if array elements are consecutive in Python
- Java Program to check if two dates are equal
- How To Check If Three Points are Collinear in Java?
- Check three consecutive numbers - JavaScript
- Check if a binary string contains consecutive same or not in C++
- How to check if array contains a duplicate number using C#?
- How to check if a Perl array contains a particular value?
- How to check if Java list contains an element or not?

Advertisements