INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Increase date by given int

Increase date by given int

(OP)
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
http://www.primrose.org.uk

RE: Increase date by given int

(OP)
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

(OP)
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());

 System.out.println(strDate);




}

}


Thanks for the help :)


RE: Increase date by given int

CODE

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 ...
cal.setTime(myDate);
int myMonth = cal.get(Calendar.MONTH);
//will get it

Tim

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!

Resources

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