fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class RangeData implements Iterable<Data>{
  8. private Data inicio;
  9. private Data fim;
  10.  
  11. public RangeData(Data inicio, Data fim) {
  12. this.inicio = inicio;
  13. this.fim = fim;
  14. }
  15.  
  16. @Override
  17. public Iterator<Data> iterator() {
  18. return new Iterator<Data>() {
  19. Data corrente = inicio;
  20. @Override
  21. public boolean hasNext() {
  22. return corrente.menorOuIgual(fim);
  23. }
  24.  
  25. @Override
  26. public Data next() {
  27. Data atual = corrente;
  28. corrente = corrente.proximoDia();
  29. return atual;
  30. }
  31. };
  32. }
  33. }
  34.  
  35. class Data {
  36. private int dia;
  37. private int mes;
  38. private int ano;
  39.  
  40. public Data(){
  41.  
  42. }
  43.  
  44. public Data(int dia, int mes, int ano){
  45. setData(dia, mes, ano);
  46. }
  47.  
  48. public void setData(int dia, int mes, int ano){
  49. this.dia = dia;
  50. this.mes = mes;
  51. this.ano = ano;
  52. }
  53.  
  54. public boolean menorOuIgual(Data data) {
  55. return ano < data.ano || (ano == data.ano && mes < data.mes) ||
  56. (ano == data.ano && mes == data.mes && dia <= data.dia);
  57. }
  58.  
  59. public Data proximoDia(){
  60. return new Data(dia + 1, mes, ano);
  61. }
  62.  
  63. @Override
  64. public String toString() {
  65. return String.format("%02d/%02d/%d",dia, mes, ano);
  66. }
  67. }
  68.  
  69. /* Name of the class has to be "Main" only if the class is public. */
  70. class Ideone
  71. {
  72. public static void main (String[] args) throws java.lang.Exception
  73. {
  74. Data d[] = { new Data(), new Data(), new Data(), new Data() };
  75.  
  76. d[0].setData( 2, 4, 2016 );
  77. d[1].setData( 12, 3, 2006 );
  78. d[2].setData( 3, 3, 2006 );
  79. d[3].setData( 7, 9, 2015 );
  80.  
  81. for( Data x : (new RangeData( d[2], d[1] )) )
  82. System.out.println( x );
  83. }
  84. }
Success #stdin #stdout 0.1s 28052KB
stdin
Standard input is empty
stdout
03/03/2006
04/03/2006
05/03/2006
06/03/2006
07/03/2006
08/03/2006
09/03/2006
10/03/2006
11/03/2006
12/03/2006