0
Datetime start;
DateTime stop;  

public void datesIbtween(DateTime start, DateTime stop) {
         //compare start and stop, the calculate diffrence
         }

I do not want to parse the start and stop as String, I was wondering if is a there a way I can parse in Datetime variable into a method? if not? what are my alternatives?

Slim'odara
  • 17
  • 1
  • 4
  • I'm not quite sure what you mean by "is a there a way I can parse in Datetime variable into a method." Could you clarify that a bit? – Chris Sep 25 '19 at 02:42
  • What do you mean by parse into a method? – Martin'sRun Sep 25 '19 at 02:43
  • I wanted the method to calculate the number of day between 2 dates (start and stop). The start and stop variables are of type "Datetime" and I expect their values to be parsed when that method is called. The question is - is it doable? – Slim'odara Sep 25 '19 at 02:47
  • @Martin'sRun I wanted to say " the caller will supply the values of those DateTime variables when they use an object to call it. – Slim'odara Sep 25 '19 at 02:49

2 Answers2

1

If I understood your question correctly, then you can just use the time library:

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class DaysInBetween {
   public static void main(String[] args) {
    LocalDate before = LocalDate.of(2017, Month.MAY, 24);
    LocalDate after = LocalDate.of(2017, Month.JULY, 29);
    long daysBetween = ChronoUnit.DAYS.between(before, after);
    System.out.println(daysBetween);
   }    
}

NOTE

This means you can parse the date input from the caller as a LocalDate (i.e: days=int, months=string, and year=int) then pass it to the imported function to calculate the difference.

BAS
  • 145
  • 1
  • 11
0

You most definitely can use DateTime as your method parameter, if that's what you meant. To find the difference between your DateTimes use the Date.daysBetween method.

Check : Number of days between two dates in Joda-Time

Martin'sRun
  • 522
  • 3
  • 11