• Post Reply Bookmark Topic Watch Topic
  • New Topic

add one day to current date  RSS feed

 
Mehak Darti
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...
i have the current date in one string varaible ... how do i add one day to the current date so that it will show be the next day's date... !!!??
 
Mehak Darti
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please attach code if possible..!
 
Debojit Dey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mahek,
u can use the code below
============================================================================
import java.util.*;
public class TestCal
{
public static void main(String[] args)
{
System.out.println("Hello World!");

//If u have the current date as
String currDate = "12/10/2003";
//Parse this String to get the day, month and year.
// This code wud vary depending on the date format that u use
int day = Integer.parseInt(currDate.substring(0,2));
int month = Integer.parseInt(currDate.substring(3,5));
int year = Integer.parseInt(currDate.substring(6));
// Create a Date object for this particular date
Calendar calendar = new GregorianCalendar();
//set the year, month and day to the date object created
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,day);
//You can even check out if the object was created with the correct data.
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
//Now in order to add a day to u r current date, do the following
calendar.add(Calendar.DAY_OF_MONTH,1);
// print out again to see if the day got incremented by 1
System.out.println("------------------------------------------");
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
}

// the date is
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR));
}
=========================================================================
Hope this helps.
Regards,
Debojit
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!