cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for Last Day of Week

Former Member
0 Kudos

Hi All

Can someone provide me with a code to get the last day of week.

My input can be any random date. And I need to find the last day of that particular week.

I tried using the CALENDAR class & its methods but it takes the data from current week.

Regards

Soumen Das

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

Here I have a excerpt of a UDF for the last day in month:

String yearS = var1.substring(start+7,start+11);
int year =  Integer.parseInt(yearS);
// get month (note: January = 0)
String monthS = var1.substring(start+4,start+6);
int month = Integer.parseInt(monthS) - 1;
// determine last day of month
Calendar calendar = Calendar.getInstance(); 
calendar.set(year,month,1);
int lastDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
String lastDateS = Integer.toString(lastDate);

For the last day in week you have to replace Calendar.DAY_OF_MONTH with another constant.

Search also with google, this helped me to create my UDF.

Former Member
0 Kudos

Last day of months is available everywhere.

My issue was with last day of week as the last day of week could be in the next month & at times next year too.

stefan_grube
Active Contributor
0 Kudos

did you try Calendar.DAY_OF_WEEK ?

Former Member
0 Kudos
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");    
Date date = simpleDateFormat.parse(var1);
Calendar calendar = Calendar.getInstance();    
        calendar.setTime(date);
         int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.set(Calendar.DAY_OF_WEEK, (calendar.get(Calendar.DAY_OF_WEEK)%7)+(7 - dayOfWeek));
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
        String now = new String( dateformat.format( calendar.getTime()));
				return now;

This is the code I tried. But it fails at the 2nd line where I parse my input Date string. var1 is the input string.

Answers (1)

Answers (1)

Former Member
0 Kudos

May be this link can provide you some help..

/thread/5154036 [original link is broken]

Former Member
0 Kudos

I wrote the following code to get this done :

import java.util.*;
import java.text.*;
public class test {
   public static void main(String[] args) {
 try {    
	String str_date="20000103";
        
	DateFormat formatter ; 
     Date date ; 
     formatter = new SimpleDateFormat("yyyyMMdd");
    date = (Date)formatter.parse(str_date); 
     Calendar cal=Calendar.getInstance();
     cal.setTime(date);
       
       int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
       cal.set(Calendar.DAY_OF_WEEK,(cal.get(Calendar.DAY_OF_WEEK)%7)+(7 - dayOfWeek));
       cal.add(cal.DAY_OF_WEEK,1);
       String strdate = null;

       SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

       if (cal != null) {
       strdate = sdf.format(cal.getTime());
       }
       
               System.out.println(strdate);
    
 }  
 
 catch (ParseException e)
    {System.out.println("Exception :"+e);    }   
     
   }
}

Thanks Everyone!!!

Soumen...