Here you can find the source of getLastNDays(int aInN)
public static Map<String, GregorianCalendar> getLastNDays(int aInN)
//package com.java2s; //License from project: Open Source License import java.text.SimpleDateFormat; import java.util.*; public class Main { public static Map<String, GregorianCalendar> getLastNDays(int aInN) { HashMap<String, GregorianCalendar> lDays = new HashMap<String, GregorianCalendar>(); GregorianCalendar lCal = (GregorianCalendar) GregorianCalendar.getInstance(); while (aInN > 0) { // Skip Holidays and weekends updateCalenderDate(lCal);//from w w w . ja v a 2s. c o m String lFormattedDate = getFormattedDate(lCal); lDays.put(lFormattedDate, lCal); aInN--; lCal = (GregorianCalendar) lCal.clone(); lCal.add(GregorianCalendar.DATE, -1); } return lDays; } public static void updateCalenderDate(GregorianCalendar aInCal) { String lDayName = aInCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()); if (Objects.equals(lDayName, new String("Sun"))) { aInCal.add(GregorianCalendar.DATE, -2); } else if (Objects.equals(lDayName, new String("Sat"))) { aInCal.add(GregorianCalendar.DATE, -1); } } public static String getFormattedDate(GregorianCalendar aInCal) { SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); String formatted = format1.format(aInCal.getTime()); return formatted; } }