fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.SimpleDateFormat;
  7.  
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. public class Main {
  11.  
  12. public static void main(String[] args) throws Exception {
  13. Booking[] bookings = {
  14. new Booking(new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-12"),new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-18") ),
  15. new Booking(new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-11"),new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-15") ),
  16. new Booking(new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-13"),new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-14") ),
  17. new Booking(new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-12"),new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH).parse("2016-10-13") ),
  18. };
  19.  
  20. System.out.println(getDaysBetweenDates(bookings));
  21.  
  22. }
  23.  
  24. public static Date getMaxOccurence(Booking[] booking) {
  25. List<Date> dates = new ArrayList<Date>();
  26. Date max = new Date();
  27. int freq = 0;
  28. for (Booking b : booking) {
  29. Calendar calendar = new GregorianCalendar();
  30. calendar.setTime(b.getStartDate());
  31.  
  32. while (calendar.getTime().before(b.getEndDate())) {
  33. Date result = calendar.getTime();
  34. dates.add(result);
  35. int curr = Collections.frequency(dates, result);
  36. if (curr > freq) {
  37. freq = curr;
  38. max = result;
  39. }
  40. calendar.add(Calendar.DATE, 1);
  41. }
  42. }
  43. return max;
  44. }
  45.  
  46. static class Booking {
  47. Date startDate;
  48. Date endDate;
  49.  
  50. public Booking(Date startDate, Date endDate) {
  51. this.startDate = startDate;
  52. this.endDate = endDate;
  53. }
  54.  
  55. public Date getStartDate() {
  56. return startDate;
  57. }
  58.  
  59. public void setStartDate(Date startDate) {
  60. this.startDate = startDate;
  61. }
  62.  
  63. public Date getEndDate() {
  64. return endDate;
  65. }
  66.  
  67. public void setEndDate(Date endDate) {
  68. this.endDate = endDate;
  69. }
  70. }
  71. }
Success #stdin #stdout 0.07s 4386816KB
stdin
Standard input is empty
stdout
Thu Oct 13 00:00:00 GMT 2016