Java examples for java.util:Month
get Last Month End
//package com.java2s; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { int year = 2; int month = 2; System.out.println(getLastMonthEnd(year, month)); }//from w w w . jav a 2 s.c o m public static Date getLastMonthEnd(int year, int month) { Calendar temp = getMonthEndCalendar(year, month); temp.add(Calendar.MONTH, -1); return temp.getTime(); } private static Calendar getMonthEndCalendar(int year, int month) { Calendar result = Calendar.getInstance(); result.setTime(getMonthStart(year, month)); result.set(Calendar.DAY_OF_MONTH, result.getActualMaximum(Calendar.DAY_OF_MONTH)); return result; } public static Date getMonthStart(int year, int month) { return parseDate(String.valueOf(year) + month, "yyyyM"); } public static Date parseDate(String dateString, String format) { SimpleDateFormat formater = new SimpleDateFormat(); formater.applyPattern(format); formater.setLenient(false); try { return formater.parse(dateString); } catch (ParseException e) { return null; } } }