on 06-30-2007 4:37 PM
Hi Experts,
I am using Web Dynpro program. The BAPI returns first day of week (i.e. Monday). I have to calculate dates of Tuesdays, Wednesdays, Thursdays, Fridays, Saturdays and Sundays based on BAPI output. For example, suppose BAPI has recturned Monday Feb 25, 2007. Based on this, I have to calculate what will be the dates Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. How can we calculate this quickly in Java?
Regards,
Brian
Here is a very comprehensive article about date and date manipulation:
<a href="http://www.onjava.com/pub/a/onjava/2003/06/05/java_calendar.html">See article (O'Reilly)</a>
Otherwise lookup the add method of the java.util.Calendar class....
Have you tried:
Calendar mycal = new Calendar();
mycal.setDate(sapdate);
mycal.add(Calendar.DATE, 1);
Enjoy and attribute points
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
U can use the following code to serve your purpose :
public class DateUtility { /* Add Day/Month/Year to a Dateadd() is used to add values to a Calendar object. You specify which Calendar field is to be affected by the operation (Calendar.YEAR, Calendar.MONTH, Calendar.DATE). */ public static void addToDate(){ System.out.println("In the ADD Operation"); // String DATE_FORMAT = "yyyy-MM-dd"; String DATE_FORMAT = "dd-MM-yyyy"; //Refer Java DOCS for formats java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT); Calendar c1 = Calendar.getInstance(); Date d1 = new Date(); System.out.println("Todays date in Calendar Format : "+c1); System.out.println("c1.getTime() : "+c1.getTime()); System.out.println("c1.get(Calendar.YEAR): " + c1.get(Calendar.YEAR)); System.out.println("Todays date in Date Format : "+d1); c1.set(1999,0 ,20); //(year,month,date) System.out.println("c1.set(1999,0 ,20) : "+c1.getTime()); c1.add(Calendar.DATE,40); System.out.println("Date + 20 days is : " + sdf.format(c1.getTime())); System.out.println(); System.out.println(); }
public static void main(String args[]){
addToDate();
}
If it is helpful please reward with points.
Thanks
ritu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Brian,
Here is the piece of code you are looking for.
<b>Assumption:</b>
The return type of the BAPI is a String. Example: <b>"Monday May 31, 2007"</b>
String day = "Monday May 31, 2007";
String year = day.substring(day.lastIndexOf(" ")+1, day.length());
String month = day.substring(day.indexOf(" ")1, day.indexOf(" ")4);
String date = day.substring(day.indexOf(" ")5, day.indexOf(" ")7);
Calendar cal = Calendar.getInstance();
int current_month = 0;
if(month.equals("Jan")){
current_month = 0;
}else if(month.equals("Feb")){
current_month = 1;
}else if(month.equals("Mar")){
current_month = 2;
}else if(month.equals("Apr")){
current_month = 3;
}else if(month.equals("May")){
current_month = 4;
}else if(month.equals("Jun")){
current_month = 5;
}else if(month.equals("Jul")){
current_month = 6;
}else if(month.equals("Aug")){
current_month = 7;
}else if(month.equals("Sep")){
current_month = 8;
}else if(month.equals("Oct")){
current_month = 9;
}else if(month.equals("Nov")){
current_month = 10;
}else if(month.equals("Dec")){
current_month = 11;
}
cal.set(Integer.parseInt(year), current_month, Integer.parseInt(date));
cal.add(Calendar.DATE, 1); //Tuesday
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.YEAR));
cal.add(Calendar.DATE, 1); //Wednesday
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.YEAR));
cal.add(Calendar.DATE, 1); //Thrusday
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.YEAR));
cal.add(Calendar.DATE, 1); //Friday
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.YEAR));
cal.add(Calendar.DATE, 1); //Saturday
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.YEAR));
cal.add(Calendar.DATE, 1); //Sunday
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.YEAR));
You need to append the output according to your convenience.
Hope this helps.
Regards,
Rekha Malavathu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
9 | |
8 | |
6 | |
6 | |
6 | |
5 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.