Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Increase date by given int

Increase date by given int

Increase date by given int

Hi there,

I have the following code:

    DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    System.out.println(sdf.format(new Date()));

that prints the date in a given format. In this class I have an intand based on that int I would want to increase/decrease the date by that value. E.g. if the date is 12/10/2006 and the given int was 2 I want to change the date to 14/10/2006. (Likewise for -ve numbers)

I have heard of calendar objects but have no idea how to implement them.

Any help would be greatly appreciated.

Thanks and Regards

RE: Increase date by given int

This code will roll the date forward by 14 days from today :

Calendar cal = GregorianCalendar.getInstance();
cal.roll(Calendar.DAY_OF_MONTH, 14);

Free Java/J2EE Database Connection Pooling Software

RE: Increase date by given int

Thanks for that but when I do a System.out.println(cal) I get all the date information in the world.

Is there a way to extract the Calendar.MONTH and assign it to a variable?

Thanks again

RE: Increase date by given int

For anyone who will be looking at this thread I patched this together:

import java.util.*;
import java.text.*;

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

  DateFormat myDate = new SimpleDateFormat("dd/MM/yyyy");
  Calendar cal = GregorianCalendar.getInstance();

  int year = cal.get(Calendar.YEAR);
  int month = cal.get(Calendar.MONTH)+ 1;
  int day = cal.get(Calendar.DAY_OF_MONTH);

  // prints current date:
  System.out.println(day + "" + month + "" + year);

  // add 15 days
  cal.add(Calendar.DAY_OF_MONTH, 25);

  year = cal.get(Calendar.YEAR);
  month = cal.get(Calendar.MONTH) + 1;
  day = cal.get(Calendar.DAY_OF_MONTH);

  System.out.println(day + "" + month + "" + year);

 String strDate = myDate.format(cal.getTime());




Thanks for the help :)

RE: Increase date by given int


Calendar cal = Calendar.getInstance();

//this month
int month = cal.get(Calendar.MONTH);

//if we've got some other date in the Date instance myDate
//then ...
int myMonth = cal.get(Calendar.MONTH);
//will get it


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close