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. Date timpSpecific = formatRecurent.parse(dataEveniment);
  48. timpCurent.setTime(timpCurent.getTime() + (timpSpecific.getTime() - timpCurent.getTime()) / (numarOre * HOUR_IN_MSEC) * numarOre * HOUR_IN_MSEC);
  49. if (timpCurent.before(timpSpecific)) {
  50. timpCurent.setTime(timpCurent.getTime() + numarOre * HOUR_IN_MSEC);
  51. }
  52. } catch (Exception e) {
  53. System.out.println("Data nu este in formatul cerut!");
  54. return null;
  55. }
  56. return formatRecurent.format(timpCurent).toString();
  57. }
  58. }
  59.  
  60. public class Main {
  61. public static void main(String[] args) {
  62. EvenimentRecurent er = new EvenimentRecurent("2019-03-09 22:46:00",
  63. "2019-03-09 23:00:00", "Scris probleme", 24);
  64. System.out.println(er.urmatorulEveniment("2019-04-19 22:46:23"));
  65. // 2019-04-20 22:46:00
  66. }
  67. }
  68.  
Success #stdin #stdout 0.14s 59108KB
stdin
Standard input is empty
stdout
2019-04-20 22:46:00