Use the roll method of Calendar to increment the month generated correct dates regardless of the actual days in the moth. For example, use the roll method to increase the month by 1 on a Calendar instance of date January 31, 2012 will give February 28, 2012.
The following sample code shows how to use the roll method to increment or decrement the month and then get the last day of the month.
int year = 2012;
int month = Calendar.JANUARY;
int day = 31;
Calendar calendar = new GregorianCalendar(year, month, day);
//increments the month by 1
calendar.roll(Calendar.MONTH, true);
//or decrements the month by one
//calendar.roll(Calendar.MONTH, false);
//get the last day of the month
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
-------------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
The following sample code shows how to use the roll method to increment or decrement the month and then get the last day of the month.
int year = 2012;
int month = Calendar.JANUARY;
int day = 31;
Calendar calendar = new GregorianCalendar(year, month, day);
//increments the month by 1
calendar.roll(Calendar.MONTH, true);
//or decrements the month by one
//calendar.roll(Calendar.MONTH, false);
//get the last day of the month
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
-------------------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
No comments:
Post a Comment