-1

First i set month and year then i want previous month and year not current month. now i getting which month set on calender that month only

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

public class PrevMonth 
{
    private static String prev_day_str;
    private static String prev_month_str;

    public static void main(String[] args) 
    {
        SimpleDateFormat prev_day = new SimpleDateFormat("dd");
        Calendar calendar   = Calendar.getInstance();
        calendar.set(Calendar.MONTH, 1);
        calendar.set(Calendar.YEAR, 2015);

        calendar .add(Calendar.MONTH, -1);      //subtracting a month
        SimpleDateFormat prev_month = new SimpleDateFormat("MM");
        SimpleDateFormat prev_year = new SimpleDateFormat("YYYY");
        prev_day_str = prev_month.format(new Date(calendar .getTimeInMillis()));
        prev_month_str = prev_year.format(new Date(calendar .getTimeInMillis()));

        System.out.println(prev_day_str+" "+prev_month_str);
    }

}

Result: 01 2015 need for 12 2014

soorapadman
  • 4,451
  • 7
  • 35
  • 47
Jayakumar
  • 91
  • 1
  • 14

2 Answers2

3

Your code:

calendar.set(Calendar.MONTH, 1);

actually sets the month to February.

For January you need to use 0, ie.

calendar.set(Calendar.MONTH, 0);
dave
  • 11,641
  • 5
  • 47
  • 65
Akshai
  • 219
  • 3
  • 6
0

You start by initialize the calendar to February instead of January :

    public class PrevMonth  {
    private static String prev_day_str;
    private static String prev_month_str;

    public static void main(String[] args) 
    {
        SimpleDateFormat prev_day = new SimpleDateFormat("dd");
        Calendar calendar   = Calendar.getInstance();
        // 0 for January
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.YEAR, 2015);
        // -1 for substruct
        calendar .add(Calendar.MONTH, -1);      //subtracting a month
        SimpleDateFormat prev_month = new SimpleDateFormat("MM");
        SimpleDateFormat prev_year = new SimpleDateFormat("YYYY");
        prev_day_str = prev_month.format(new Date(calendar .getTimeInMillis()));
        prev_month_str = prev_year.format(new Date(calendar .getTimeInMillis()));

        System.out.println(prev_day_str+" "+prev_month_str);
    }

}
chenchuk
  • 5,324
  • 4
  • 34
  • 41
  • calendar.set(Calendar.MONTH, 2); Output: 2015 02 but need 2015 01 – Jayakumar Nov 25 '15 at 06:15
  • remember that 2 means 3/2015 , so u get 2/2015 after substruct 1 month - look at the comment inside the code – chenchuk Nov 25 '15 at 06:20
  • so java calender start with 0 end with 11 for months – Jayakumar Nov 25 '15 at 06:21
  • almost true ... actually its circular ... ( 12 means 1/2016, 24 means 1/2017 ... etc. ) – chenchuk Nov 25 '15 at 06:23
  • but i using month is 12 but getting 2014 12 – Jayakumar Nov 25 '15 at 06:25
  • make sure you save your code before running it ... i just try now and if i put 12 : the end-result ( after substruction ) is 12/2015. if i put 24 : then the end-result (after substruction ) is 12/2016. consider it that way ( when Calendar.YEAR is 2015, then Calendar.MONTH is the offset from January/2015. means that 0 is January itself, 1 is the next (Feb) 12 is 12 monthes ahead ( January/2016 ) etc. – chenchuk Nov 25 '15 at 06:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96114/discussion-between-jayakumar-and-chenchuk). – Jayakumar Nov 25 '15 at 07:13