Java Day getLastNDays(int aInN)

Here you can find the source of getLastNDays(int aInN)

Description

get Last N Days

License

Open Source License

Declaration

public static Map<String, GregorianCalendar> getLastNDays(int aInN) 

Method Source Code

//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;
    }
}

Related

  1. getLastDay(Date date)
  2. getLastDay(Date date)
  3. getLastDay(Date date)
  4. getLastDay(String nowdate, String inFormat, String outFormat)
  5. getLastDayOfQuarter(Date date)
  6. getLastTimeInDay(Date day)