cancel
Showing results for 
Search instead for 
Did you mean: 

Conversion of Date to week format

sahithi_moparthi
Contributor
0 Kudos

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.

Accepted Solutions (0)

Answers (2)

Answers (2)

anupam_ghosh2
Active Contributor
0 Kudos

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

anupam_ghosh2
Active Contributor
0 Kudos

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

ernesto_cruz
Participant
0 Kudos

Good contribution Anupam, I proved the UDF and works ok. SAP Netweaver 7.1

Greets..

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ernesto,

Thanks a lot for your comments. It feels great to be able to serve SCN in some way.

SCN has given me a lot, I think I can never repay it back to the same amount but at least I will try my level best.

Regards

Anupam