0

I have to create a method called nextEvent, which receives and returns a String that represents a date in this pattern: "yyyy-MM-dd HH:mm:ss". I have to add an int called nrHours to this String to modify the date when next event will be. My problem is that I don't know haw can I concatenate an int onto a string...

The code:

 class Event {
 private Date dateStart, dateEnd;
 private String name;

 public Event(String dateStart, String dateEnd, String name) {
     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     try {
         this.dateStart = format.parse(dateStart);
         this.dateEnd = format.parse(dateEnd);
     } catch (Exception e) {
         System.out.println("Not valid date format");
     }
     this.name = name;
 }
 
 public Date getDateStart() {
     return dateStart;
 }

 public Date getDateEnd() {
     return dateEnd;
 }

 public String getNume() {
     return name;
 }
 }




 class EventRecurrent extends Event {
 private int nrHours;
 private Date next;

 EventRecurrent(String dateStart, String dateEnd, String name, int nrHours) {
     super(dateStart, dateEnd, name);
     this.nrHours = nrHours;
 }

 public String nextEvent(String next) {

     // I dont know how can I add the nrHours to the String next

     return next;
 }
 }


 public class Main {
 public static void main(String[] args) {
     EventRecurrent er = new EventRecurrent("2019-03-09 22:46:00",
             "2019-03-09 23:00:00", "walk the dog", 24);
     System.out.println(er.nextEvent("2019-04-19 22:46:23"));
     // 2019-04-20 22:46:00
 }

}

In the Main is an example: if the nextEvent receives "2019-04-19 22:46:23", and the nrHours is 24, then it should print out: 2019-04-20 22:46:00

java.text.* and java.util.* are imported

CATboardBETA
  • 418
  • 6
  • 29
CoderOne
  • 1
  • 2
  • The same way you handle dates in your Event class. Parse the String to a LocalDateTime, do the date arithmetic, and format the result as a String. – Gilbert Le Blanc Mar 17 '21 at 13:15
  • Just as you don’t pass your number of hours in a string but wisely use an `int`, also don’t pass the date and time in a string, but wisely use a `LocalDateTime`. – Ole V.V. Mar 17 '21 at 14:11
  • Similar: [How can I add an int (minutes) to LocalDateTime.now()?](https://stackoverflow.com/questions/30374796/how-can-i-add-an-int-minutes-to-localdatetime-now) – Ole V.V. Mar 17 '21 at 14:17
  • I also recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` or `ZonedDateTime`; both are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 17 '21 at 14:19

2 Answers2

0

This way you can add hours.

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = formatter.parse(next);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.HOUR, 2);
    System.out.println(calendar.getTime());
Sanjay
  • 2,481
  • 1
  • 13
  • 28
  • 2
    I recommend using the [date time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Abra Mar 17 '21 at 13:39
  • I recommend you don’t use `DateFormat`, `SimpleDateFormat`, `Date` and `Calendar`. Those classes are poorly designed and long outdated every one of them, the first two in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 17 '21 at 14:13
  • The problem is I have to add hours to a String not to a Date. My method should receive a String and return a String. – CoderOne Mar 17 '21 at 14:47
  • In String it is not possilbe.Forst you need to convert to date then add then again convert to string and return. – Sanjay Mar 18 '21 at 04:06
0

java.time

Passing date and time as string is a very bad design. Use LocalDateTime or ZonedDateTime, and your task will be trivial. Under all circumstances I recommend that you use java.time, the modern Java date and time API, for all your date and time work. The two mentioned classes are part of that API.

However, if you insist on a bad design, you will have to convert your string to one of the mentioned types, add the hours and convert back. For the conversion we need a formatter.

protected static final DateTimeFormatter FORMATTER
        = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

Now your method becomes:

public String nextEvent(String next) {
    LocalDateTime dateTime = LocalDateTime.parse(next, FORMATTER);
    dateTime = dateTime.plusHours(nrHours);
    return dateTime.format(FORMATTER);
}

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161