http://www.technicalpage.net/search/label/SQL

>> TodayPlusDays

Add days to today or current date in Java:

Using below code, you can add days to today's date or to current date and find out the new date in any date format.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FromToday{


       public static void main(String[] args) throws Exception {            

                     SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); // "dd/MM/yyyy" is the date format , you can use any date format , month should be using capitol "M" only , not small "m".

                     Calendar calendar = Calendar.getInstance();
                                   
String today = dateFormat.format(calendar.getTime());

System.out.println("Today's date = "+today);

                     calendar.add(Calendar.DAY_OF_MONTH, 2); // add 2 days on today.

                     String newDateFromToday = dateFormat.format(calendar.getTime());

                     System.out.println("new Date after adding 2 days = "+newDateFromToday);
             
       }
}


Output:
new Date after adding 2 days = 03/01/2019 if today = 01/01/2019


Let's give a different date format:


public class FromToday{


       public static void main(String[] args) throws Exception {            

                     SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy"); // "dd/MM/yyyy" is the date format , you can use any date format , month should be using capitol "M" only , not small "m".

                     Calendar calendar = Calendar.getInstance();

                       String today = dateFormat.format(calendar.getTime());


     System.out.println("Today's date = "+today);                                   

                     calendar.add(Calendar.DAY_OF_MONTH, 2); // add 2 days on today.

                     String newDateFromToday = dateFormat.format(calendar.getTime());

                     System.out.println("new Date after adding 2 days = "+newDateFromToday);
             
       }
}


Output:
new Date after adding 2 days = 1/3/2019 if today = 01/01/2019


Let's make a reusable function in which we supply the date, date format and number of days as input and get the output.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class FromToday{

       public static void main(String[] args) throws Exception {            
             
              String newDate = plusDaysFromToday("dd-MM-yyyy",2);//calling the method/function
             
              System.out.println("The date as returned by the function = "+newDate);
       }
      
       public static String plusDaysFromToday(String givenDateFormat, int numOFDays) throws Exception{
             
              SimpleDateFormat dateFormat = new SimpleDateFormat(givenDateFormat);
             
              Calendar calendar = Calendar.getInstance();


          String today = dateFormat.format(calendar.getTime());

          System.out.println("Today's date = "+today);
             
              calendar.add(Calendar.DAY_OF_MONTH, numOFDays);
             
              String newDateFromToday = dateFormat.format(calendar.getTime());
             
              System.out.println("newDateFromToday = "+newDateFromToday );
             
              return newDateFromToday ;
             
       }
}

Output:
newDateFromToday = 03-01-2019 if today is 01-01-2019
The date as returned by the function = 03-01-2019







No comments:

Post a Comment