on 11-16-2012 10:09 AM
Hi,
Could any one please help me in converting the date format to week format.
From IDOC the Date is coming as 10102012..we need to convert this as Week.2012( 38.2012 ) .
I hope we need to write UDF.
Hi Santhoshi M,
Java has method to provide us the day of the week. Thus I do not need to write the logic for determination of week of the year from day of the year. Thus I have modified the code as shown below
public static String weekOfYear (String a) 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)+year;
return a;
}
I have followed this calender as reference http://www.timeanddate.com/calendar/
This 53rd week of current year will shown as 1st week of next year.
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Santhosi,
Please try this UDF
The date 10102012 comes to 41st week of 2012 and not 38. Please check this link
http://disc.gsfc.nasa.gov/julian_calendar.shtml
public static String weekOfYear (String a) throws StreamTransformationException
{
String dateFormat="ddMMyyyy";
int dayOfYear=0;
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);
dayOfYear=greg.get(Calendar.DAY_OF_YEAR);
int weekOfYear=dayOfYear/7;
if((dayOfYear-weekOfYear*7)>0)
{
weekOfYear++;
}
if(weekOfYear>52)
{
weekOfYear=52;
}
a=""+weekOfYear+"."+greg.get(Calendar.YEAR);
return a;
}
Finally if the last day of the year falls on 53 rd week of the year the code makes it 52 weeks as per this link
http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php
A year can have only up to 52 weeks. If you do not support this logic you need to remove lines 24-27.
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
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.