on 08-18-2015 1:35 PM
Hi Experts,
I have two field StartDate and EndDate and based on that I have to calculate the number of weeks.
Please help.
Regards
Hi Rakhi,
You can close the thread if its solved.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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");
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.