I have a date from the request, say 15-aug-2014, How do I calculate the previous month end date (It will be 31-Jul-2014) in java using java,lang.Calendar. We dont use JODA time.
May be this can help,
Calendar cal = Calendar.getInstance();
//Set the Calendar date to 1st of the current month
cal.set(Calendar.DATE, 1);
// Subtract a day from it
cal.set(Calendar.DATE,cal.get(Calendar.DATE)-1);
// Prints the previous month end date
System.out.println(cal.get(Calendar.DATE));
From the Calendar
instance, you can get the Date
by
Date date = cal.getTime();
Finally, you can use SimpledateFormat
to format your date as needed
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
System.out.println( "The date is: "+ sdf.format(date));
Hope, it helps!
尝试
c.add(Calendar.DAY_OF_MONTH, -(c.get(Calendar.DAY_OF_MONTH)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.