Results 1 to 3 of 3

Thread: Incrementing the month in a date

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    18

    Incrementing the month in a date

    Could anyone tell me how to increment or decrement only the month in a date?


  2. #2
    Junior Member
    Join Date
    Sep 2007
    Answers
    5

    Re: Incrementing the month in a date

    Quote Originally Posted by 3roses View Post
    Could anyone tell me how to increment or decrement only the month in a date?
    Try this
    say u have object cal of type Calender.

    Calendar cal= Calendar.getInstance();
    cal.add(cal.MONTH,1); // to increment month by one
    cal.add(cal.MONTH,-1); // to decrement month by one


  3. #3
    Expert Member
    Join Date
    Mar 2012
    Answers
    208

    Re: Incrementing the month in a date

    Increment and decrement months can be done using the Calendar Class as:

    import java.util.Calendar;

    public class Main {

    public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    System.out.println("Now : " + cal.getTime());

    int monthsToDecrement = -1;
    cal.add(Calendar.MONTH, monthsToDecrement);
    System.out.println("Date after decrement: " + cal.getTime());
    }


  4.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact