fork(1) download
  1. import java.util.*;
  2. import java.text.*;
  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. private Date data;
  36. private static 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 datadata){
  44. SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  45. try {
  46. data = form.parse(datadata);
  47. } catch (Exception e) {
  48. System.out.println("Data nu este in formatul cerut!");
  49. }
  50. Date aux = getDataInceput();
  51. Calendar cal = Calendar.getInstance();
  52. cal.setTime(aux);
  53. Calendar c1 = Calendar.getInstance();
  54. c1.setTime(data);
  55. long seconds = (c1.getTimeInMillis() - cal.getTimeInMillis()) / 1000;
  56. int hours = (int) (seconds / 3600);
  57. double hrs = (seconds/ 3600.0);
  58. int ev = hours / this.numarOre;
  59. cal.add(Calendar.HOUR, (ev * this.numarOre));
  60. while (hrs > (ev * this.numarOre)){
  61. cal.add(Calendar.HOUR, this.numarOre);
  62. ++ev;
  63. }
  64. return form.format(cal.getTime());
  65. }
  66. }
  67.  
  68. class prog {
  69. public static void main(String[] args) {
  70. EvenimentRecurent er = new EvenimentRecurent("2019-03-09 22:46:00",
  71. "2019-03-09 23:00:00", "Scris probleme", 24);
  72. System.out.println(er.urmatorulEveniment("2019-04-19 22:46:23"));
  73. // 2019-04-20 22:46:00
  74. }
  75. }
Success #stdin #stdout 0.13s 36084KB
stdin
Standard input is empty
stdout
2019-04-20 22:46:00