fork download
  1. import java.text.*;
  2. import java.util.*;
  3.  
  4. class Eveniment {
  5. private Date dataInceput, dataSfarsit;
  6. private String nume;
  7. // Primeste 2 stringuri in format yyyy-MM-dd HH:mm:ss reprezentand data si ora
  8. // de inceput si de final a evenimentului si inca un string care contine numele
  9. // cu care apare evenimentul in calendar
  10. public Eveniment(String dataInceput, String dataSfarsit, String nume) {
  11. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  12. try {
  13. this.dataInceput = format.parse(dataInceput);
  14. this.dataSfarsit = format.parse(dataSfarsit);
  15. } catch (Exception e) {
  16. System.out.println("Data nu este in formatul cerut!");
  17. }
  18. this.nume = nume;
  19. }
  20.  
  21. public Date getDataInceput() {
  22. return dataInceput;
  23. }
  24.  
  25. public Date getDataSfarsit() {
  26. return dataSfarsit;
  27. }
  28.  
  29. public String getNume() {
  30. return nume;
  31. }
  32. }
  33.  
  34. class EvenimentRecurent extends Eveniment {
  35. public static final long HOUR_IN_MSEC = 3600000;
  36. private int numarOre;
  37.  
  38. public EvenimentRecurent(String dataInceput, String dataSfarsit, String nume, int numarOre) {
  39. super(dataInceput, dataSfarsit, nume);
  40. this.numarOre = numarOre;
  41. }
  42.  
  43. public String urmatorulEveniment(String dataEveniment) {
  44. SimpleDateFormat formatRecurent = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  45. Date timpCurent = getDataInceput();
  46. try {
  47. while (timpCurent.before(formatRecurent.parse(dataEveniment))) {
  48. timpCurent.setTime(timpCurent.getTime() + numarOre * HOUR_IN_MSEC);
  49. }
  50. } catch (Exception e) {
  51. System.out.println("Data nu este in formatul cerut!");
  52. return null;
  53. }
  54. return formatRecurent.format(timpCurent).toString();
  55. }
  56. }
  57.  
  58. public class Main {
  59. public static void main(String[] args) {
  60. EvenimentRecurent er = new EvenimentRecurent("2019-05-09 22:46:00",
  61. "2019-03-09 23:00:00", "Scris probleme", 24);
  62. System.out.println(er.urmatorulEveniment("2019-05-09 22:46:00"));
  63. // 2019-04-20 22:46:00
  64. }
  65. }
  66.  
Success #stdin #stdout 0.18s 59112KB
stdin
Standard input is empty
stdout
2019-05-09 22:46:00