on 06-02-2010 2:59 PM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
May be this link can provide you some help..
/thread/5154036 [original link is broken]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
User | Count |
---|---|
89 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.