cancel
Showing results for 
Search instead for 
Did you mean: 

calculate the days between two dates

tomi87
Champ in-the-making
Champ in-the-making
Hello,
I want to calculate the days between two dates.
But I don't know how to do this.
Right now I tried it with:
.bpmn20.xml
<activiti:formProperty id="dateFrom" name="Date from (dd-MM-yyyy)" datePattern="dd-MM-yyyy hh:mm" type="date"            required="true" />
<activiti:formProperty id="dateToo" name="Date too (dd-MM-yyyy)" datePattern="dd-MM-yyyy hh:mm" type="date"         required="true" />
(he say datePattern  is not allowed…) (I'm using the newest version)


public void execute(DelegateExecution execution) {
StAppInfo sai = new StAppInfo ();
sai.setDateFrom((DateFormType) execution.getVariable("dateFrom"));
sai.setDateToo((DateFormType) execution.getVariable("dateToo"));

public class StAppInfo implements Serializable {
        private DateFormType dateFrom;
   private DateFormType dateToo;
But there I can't find a possibility to calculate the days between this two dates, or to convert this into days or minutes.

Do you have some suggestions for me ?
2 REPLIES 2

frederikherema1
Star Contributor
Star Contributor

(DateFormType) execution.getVariable("dateFrom")

This does not work… When submitting a form-property of type "date", a java.util.Date instance is stored as variable. You can compare them as you would compare any other date…

tomi87
Champ in-the-making
Champ in-the-making
Ok I got it now.
Thank you!

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

import org.activiti.engine.impl.form.DateFormType;

public class StapIn implements Serializable {

   DateFormType dft = new DateFormType("dd-MM-yyyy");


   private String dateFrom;
   private String dateTo;
   private long days;

   public void setDateFrom(Date dateType) {

      this.dateFrom = dft.convertModelValueToFormValue(dateType);
   }

   // for testing change DateFormType into Date
   public void setDateTo(Date dateType) {
      this.dateTo = dft.convertModelValueToFormValue(dateType);
   }

   public void setDays(long days) {
      this.days = days;
   }

   public long getDays() {

      Calendar gcFrom = new GregorianCalendar();
      Calendar gcTo = new GregorianCalendar();

      String dateFrom = getDateFrom();
      String dateTo = getDateTo();

      Date ddFrom = (Date) dft.convertFormValueToModelValue(dateFrom);
      Date ddto = (Date) dft.convertFormValueToModelValue(dateTo);

      gcFrom.setTime(ddFrom);
      gcTo.setTime(ddto);

      long time = gcTo.getTime().getTime() - gcFrom.getTime().getTime();
      long day = Math.round((double) time / (24. * 60. * 60. * 1000.));

      setDays(day);
      return days;
   }



   public String getDateFrom() {

      return dateFrom;
   }

   public String getDateTo() {
      return dateTo;
   }
}