Java logic in Cloverleaf

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Java logic in Cloverleaf

  • Creator
    Topic
  • #53944
    Hina Siddiqui
    Participant

      Could you please help me with the following code. Its a date function. Basically setting 2 different dates.

      java.util.Calendar cal1 = java.util.Calendar.getInstance();cal1.setTime(date1);  

      java.util.Calendar cal2 = java.util.Calendar.getInstance();cal2.setTime(date2);

      java.util.Calendar date = (java.util.Calendar) cal1.clone();  

      long daysBetween = 0;  

      while (date.before(cal2)) {  

        date.add(java.util.Calendar.DAY_OF_MONTH, 1);  

        daysBetween++;  

        }  

      int awardIntVal = (int) (daysBetween/365.25);

      awardVal = (double) (daysBetween/365.25);

      String award = new Double(awardVal).toString();

      if(award.indexOf(“.999”) > -1) {

               awardVal = Double.parseDouble(award.substring(0,award.indexOf(‘.’))) + 1;

      }

      else

               awardVal = awardIntVal;

      How can i do this using tcl in cloverleaf.

    Viewing 2 reply threads
    • Author
      Replies
      • #79627
        bill bearden
        Participant

          Greetings Hina,

          It looks like you are trying to calculate the number of full years between 2 dates. If that isn’t it, all bets are off.

          This is much like calculating the age of a person. There are lots of posts on the forum for that. Here are some I found by searching “calculate age”.

          https://usspvlclovertch2.infor.com/viewtopic.php?t=5946

          https://usspvlclovertch2.infor.com/viewtopic.php?t=890

          https://usspvlclovertch2.infor.com/viewtopic.php?t=1768

          https://usspvlclovertch2.infor.com/viewtopic.php?t=1444

          https://usspvlclovertch2.infor.com/viewtopic.php?t=515

          A number of these posts say it is more complicated than you might think. One of the posts mentions a function that comes with (came with?) Cloverleaf. And Tcl dates only work back to 1902. And there are probably other reasons the little bit of code included below won’t handle all your date differencing needs. But, here goes anyway.

          One way to do this would be with Tcl date arithmetic. If you have 2 Tcl date/times, you can just find the difference between them with a Tcl expr. Then, you can get just the number of years from clock format. But clock format is time since 1/1/1970 so you have to subtract 1970 from the number of years returned by clock format. So…

          Code:

          set day1 “1988-08-08”
          set day2 “2013-12-05”
          set dt1 [clock scan $day1 -format “%Y-%m-%d”]
          set dt2 [clock scan $day2 -format “%Y-%m-%d”]
          set dt3 [expr $dt2 – $dt1]
          set years [clock format $dt3 -format “%Y”]
          set years [expr $years – 1970]
          echo $years

        • #79628
          Hina Siddiqui
          Participant

            Hi

            The code definitely helped me understand the approach. But i am trying to do it in xlate. Is there a way i could do there. Like using the already declared variables by cloverleaf

          • #79629
            bill bearden
            Participant

              To get today’s date, you could substitute this near the beginning of that code.

              Code:

              set day2 [clock format [clock seconds] -format “%Y-%m-%d”]

          Viewing 2 reply threads
          • The forum ‘Cloverleaf’ is closed to new topics and replies.