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