Advertisement
Guest User

Untitled

a guest
May 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5.  
  6. class Eveniment {
  7. private Date dataInceput, dataSfarsit;
  8. private String nume;
  9.  
  10. public Eveniment() {
  11. }
  12.  
  13. public Eveniment(String dataInceput, String dataSfarsit, String nume) {
  14. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  15. try {
  16. this.dataInceput = format.parse(dataInceput);
  17. this.dataSfarsit = format.parse(dataSfarsit);
  18. } catch (Exception e) {
  19. System.out.println("Data nu este in formatul cerut!");
  20. }
  21.  
  22. this.nume = nume;
  23. }
  24.  
  25. public Date getDataInceput() {
  26. return dataInceput;
  27. }
  28.  
  29. public Date getDataSfarsit() {
  30. return dataSfarsit;
  31. }
  32.  
  33. public String getNume() {
  34. return nume;
  35. }
  36. }
  37.  
  38.  
  39. class EvenimentRecurent extends Eveniment {
  40. private Date dataP;
  41. public int numarOre;
  42.  
  43.  
  44. public Date addHoursToJavaUtilDate(Date date, int hours) {
  45. Calendar calendar = Calendar.getInstance();
  46. calendar.setTime(date);
  47. calendar.add(Calendar.HOUR_OF_DAY, hours);
  48. return calendar.getTime();
  49. }
  50.  
  51.  
  52. public EvenimentRecurent(String dataInceput, String dataSfarsit, String nume, int numarOre){
  53. super(dataInceput,dataSfarsit,nume);
  54. this.numarOre=numarOre;
  55.  
  56.  
  57. }
  58. private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:s");
  59. public String urmatorulEveniment(String dataP){
  60. Date neww;
  61. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  62. try {
  63. this.dataP = format.parse(dataP);
  64. } catch (Exception e) {
  65. System.out.println("Data nu este in formatul cerut!");
  66.  
  67. }
  68. Calendar c = Calendar.getInstance();
  69. c.setTime(this.dataP);
  70. c.add(Calendar.HOUR, numarOre);
  71. Date currentDatePlusOne = addHoursToJavaUtilDate(this.dataP, numarOre);
  72. String strDate = dateFormat.format(currentDatePlusOne);
  73. return strDate;
  74. }
  75. }
  76. public class Main{
  77.  
  78. public static void main(String[] args) {
  79. EvenimentRecurent er = new EvenimentRecurent("2019-03-09 22:46:00", "2019-03-09 23:00:00", "Scris probleme", 94000);
  80. System.out.println(er.urmatorulEveniment("2019-04-19 22:46:23"));
  81. // 2019-04-20 22:46:23
  82.  
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement