cancel
Showing results for 
Search instead for 
Did you mean: 

how to calculate week

Former Member
0 Kudos

Hi Experts,

I have two field StartDate and EndDate and based on that I have to calculate the number of weeks.

Please help.

Regards

Accepted Solutions (0)

Answers (3)

Answers (3)

vinaymittal
Contributor
0 Kudos

Hi Rakhi,

You can close the thread if its solved.

vinaymittal
Contributor
0 Kudos

use this code

SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");

long d1=formater.parse("date1").getTime();
long d2=formater.parse("date2").getTime();

int days = (int)Math.abs((d1-d2)/(1000*60*60*24))); // suppose 9 days


int number_of_weeks = (int)Math.ceil(days/7);




pass the date in following format

2001-1-2


import the class from


  • java.text.SimpleDateFormat
  • java.util.Date
Former Member
0 Kudos

This is not working. Please Check.

And one more thing can you please explain this (1000*24*60*60)Logic.Why 1000?

vinaymittal
Contributor
0 Kudos

Time is in milliseconds so we need to multiply by 1000.

what is the error you are facing while implementing it....

replace "date1" with the variable you need to pass without quotes.

vinaymittal
Contributor
0 Kudos

use this

import java.text.SimpleDateFormat;

import java.util.Date;

public class HelloWorld {

  public static void main(String[] args) throws Exception {

    SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");

long d1=formater.parse("2015-08-01").getTime();

long d2=formater.parse("2015-08-19").getTime();

int days = (int)Math.abs((d1-d2)/(1000*60*60*24)); // suppose 9 days

int number_of_weeks = (int)Math.ceil(days/7);

System.out.println(number_of_weeks); // use result.addValue() here

  }

}

gives the week number as 2 if it has 14 to 20 days

if you want the celing value just give days/7.0 it will give the rounded up value.

for SAP PI

try{

SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");

long d1=formater.parse("2015-08-01").getTime();

long d2=formater.parse("2015-08-19").getTime();

int days = (int)Math.abs((d1-d2)/(1000*60*60*24)); // suppose 9 days

int number_of_weeks = (int)Math.ceil(days/7);

result.addValue(number_of_weeks);

}

catch(Exception e)

{

result.addValue("Date Parse Exception");

}

Former Member
0 Kudos

Display queue is null.It is not giving the number of weeks.

vinaymittal
Contributor
0 Kudos

can you share the screenshot of the UDF implementation i mean the parameters you have passed and the udf code you have exactly used and the mode of passing values by queues or by context and also the screenshot of import parameters on the left side

Former Member
0 Kudos
vinaymittal
Contributor
0 Kudos

u need to give like this

long d1=formater.parse(var1[0]).getTime();

long d2=formater.parse(var2[0]).getTime();


make the sure the mode of function is all values of a context


also add the import statement in the functions tab in the left side like this

for

  • java.text.SimpleDateFormat
  • java.util.Date


Former Member
0 Kudos

Hello Rakhi ,

Have you tried the approach mentioned by me?

The date format to pass is : "ddMMyyyy"; 

This can be changed based on your requirement.

Thanks

vinaymittal
Contributor
0 Kudos

also change

result.addValue(""+number_of_weeks);

Former Member
0 Kudos

Hello Rakhi ,

Try to use the below UDF :

public String week(String a, Container container) throws StreamTransformationException{

        String dateFormat="ddMMyyyy";  

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(dateFormat); 

        java.util.Date date=null; 

          try 

          { 

                          date = sdf.parse(a); 

          } 

          catch(java.text.ParseException e) 

          { 

                         throw new StreamTransformationException("UNABLE TO PARSE INPUT DATE"); 

          } 

          java.util.GregorianCalendar greg = new java.util.GregorianCalendar();   

          greg.setTime(date);   

          String year="."+greg.get(Calendar.YEAR); 

          if(greg.get(Calendar.WEEK_OF_YEAR)<=2 && greg.get(Calendar.DAY_OF_YEAR)>350) 

          { 

                         year="."+(greg.get(Calendar.YEAR)+1); 

          } 

          a=""+greg.get(Calendar.WEEK_OF_YEAR); 

          return a; 

  } 

}

Use this for both your dates. This will give you the week number for that date , then you can subtract the two to get the difference to find out the weeks.

thanks