Example usage for java.util Calendar getActualMaximum

List of usage examples for java.util Calendar getActualMaximum

Introduction

In this page you can find the example usage for java.util Calendar getActualMaximum.

Prototype

public int getActualMaximum(int field) 

Source Link

Document

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.

Usage

From source file:com.hihframework.core.utils.DateUtils.java

/**
 * 23.59.59.999? param:2004-08-20,return:2004-08-31
 * 23.59.59.999/*  ww  w  . j a va  2  s  .c  o m*/
 *
 * @param date
 * @return
 */
public static Timestamp getMaxDayInMonth(java.sql.Date date) {

    Calendar cale = Calendar.getInstance();
    cale.setTime(date);
    cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));
    java.sql.Date newDate = new java.sql.Date(cale.getTimeInMillis());

    cale = null;
    return Timestamp.valueOf(newDate.toString() + " 23:59:59.999");
}

From source file:it.geosolutions.geobatch.nrl.ndvi.NDVIIngestAction.java

/**
 * @return start date and end date in the array
 *//*from  w ww  . ja va2 s. c  om*/
public Calendar[] parseDekDate(String filename) throws ActionException {
    Matcher matcher = TIF_PATTERN.matcher(filename);
    if (matcher.matches()) {
        int y = 0;
        int m = 0;
        int k = 0;
        try {
            y = Integer.parseInt(matcher.group(1));
            m = Integer.parseInt(matcher.group(2));
            k = Integer.parseInt(matcher.group(3));
        } catch (NumberFormatException ex) {
            throw new ActionException(this, "Can't parse filename " + filename);
        }

        // century adjustment
        if (y < 50)
            y += 2000;
        else
            y += 1900;

        LOGGER.debug("Found date y=" + y + " m=" + m + " k=" + k);

        int startDay = k == 1 ? 1 : k == 2 ? 11 : k == 3 ? 21 : 0;
        if (startDay == 0)
            throw new ActionException(this, "Bad dekad " + k);
        Calendar start = new GregorianCalendar(y, m - 1, startDay);

        int endDay = startDay + 9;
        if (k == 3) {
            endDay = start.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        Calendar end = new GregorianCalendar(y, m - 1, endDay);

        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
        LOGGER.info("Granule interval is " + dt.format(start.getTime()) + " - " + dt.format(end.getTime()));

        return new Calendar[] { start, end };
    } else {
        throw new ActionException(this, "Can't parse filename " + filename);
    }
}

From source file:com.xumpy.timesheets.services.implementations.JobsSrvImpl.java

@Override
@Transactional/*from w  w  w . j  a  v  a 2  s .  c  o  m*/
public List<JobsInJobsGroup> selectMonthJobsInJobGroup(String month, Overview overview) throws ParseException {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date startDate = df.parse("01/" + month);

    Calendar c = Calendar.getInstance();
    c.setTime(startDate);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    Date endDate = c.getTime();

    List<JobsInJobsGroup> periodeJobsInJobsGroup = selectPeriodeJobsInJobGroup(startDate, endDate);

    for (int i = 0; i < periodeJobsInJobsGroup.size(); i++) {
        JobsInJobsGroup jobsInJobsGroup = periodeJobsInJobsGroup.get(i);

        List<JobsCtrlPojo> jobsCtrlPojo = new ArrayList<JobsCtrlPojo>();
        for (Jobs job : fillMonth(jobsInJobsGroup.getJobs())) {
            jobsCtrlPojo.add(new JobsCtrlPojo(job));
        }

        jobsInJobsGroup.setJobs(jobsCtrlPojo);
        periodeJobsInJobsGroup.set(i, jobsInJobsGroup);
    }

    overview.setMonth(month);
    overview.setAllJobsInJobsGroup(periodeJobsInJobsGroup);

    return periodeJobsInJobsGroup;
}

From source file:com.linuxbox.enkive.imap.mongo.MongoImapAccountCreator.java

@Override
public void addImapMessages(String username, Date fromDate, Date toDate) throws MessageSearchException {

    Calendar startTime = Calendar.getInstance();
    startTime.setTime(fromDate);/*  w w w  .  ja v  a 2s.  c  o  m*/
    Calendar endTime = Calendar.getInstance();
    endTime.setTime(toDate);

    while (startTime.before(endTime)) {
        Calendar endOfMonth = (Calendar) startTime.clone();
        endOfMonth.set(Calendar.DAY_OF_MONTH, endOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));

        if (endOfMonth.after(endTime))
            endOfMonth = (Calendar) endTime.clone();

        // Need to get all messages to add
        Set<String> messageIdsToAdd = getMailboxMessageIds(username, startTime.getTime(), endOfMonth.getTime());
        // Need to add messages
        // Get top UID, add from there
        if (!messageIdsToAdd.isEmpty()) {
            // Need to check if folder exists, if not create it and add to
            // user
            // mailbox list
            DBObject mailboxObject = getMessagesFolder(username, startTime.getTime());

            @SuppressWarnings("unchecked")
            HashMap<String, String> mailboxMsgIds = (HashMap<String, String>) mailboxObject
                    .get(MongoEnkiveImapConstants.MESSAGEIDS);

            TreeMap<String, String> sortedMsgIds = new TreeMap<String, String>();
            sortedMsgIds.putAll(mailboxMsgIds);
            long i = 0;
            if (!sortedMsgIds.isEmpty())
                i = Long.valueOf(sortedMsgIds.lastKey());

            for (String msgId : messageIdsToAdd) {
                i++;
                mailboxMsgIds.put(((Long.toString(i))), msgId);
            }
            mailboxObject.put(MongoEnkiveImapConstants.MESSAGEIDS, mailboxMsgIds);

            imapCollection.findAndModify(new BasicDBObject("_id", mailboxObject.get("_id")), mailboxObject);
        }
        startTime.set(Calendar.DAY_OF_MONTH, 1);
        startTime.add(Calendar.MONTH, 1);
    }
}

From source file:com.feilong.commons.core.date.DateUtil.java

/**
 * ? <code>23:59:59.999</code> <br>
 * ?,,2 2829/*from  ww  w  .j  a va 2  s  .com*/
 * 
 * <pre>
 *   2012-10-11 17:10:30.701,
 * 
 * return 2012-10-31 23:59:59.999
 * </pre>
 * 
 * @param date
 *            the date
 * @return Date
 * @see #toCalendar(Date)
 * @see Calendar#set(int, int)
 * @see #dayEnd(Calendar)
 * @see Calendar#getTime()
 * @since 1.0
 */
public static final Date getLastDateOfThisMonth(Date date) {
    Calendar calendar = toCalendar(date);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    dayEnd(calendar);
    return calendar.getTime();
}

From source file:eu.squadd.timesheets.eolas.TimeTemplate.java

public String prepareTimesheet(String[] args) {
    String response = null;// w w w .  j a  va 2s .c  om
    try {
        String[] ym = args[0].split("/");
        month = Integer.parseInt(ym[0]);
        year = Integer.parseInt(ym[1]);

        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthName = cal.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.ENGLISH);
        String periodName = monthName + "-" + year;
        cal.set(Calendar.DATE, 1);
        String dayOfWeek = new SimpleDateFormat("EE").format(cal.getTime());

        System.out.println("Month: " + periodName);
        System.out.println("Days in month: " + days);
        System.out.println("Month starts in: " + dayOfWeek);

        Map<String, String> bankHolidays = year == 2016 ? publicHolidays2016 : publicHolidays2017;
        Map<String, String> holidays = this.extractHolidays(args);

        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(template));
        HSSFSheet sheet = wb.getSheet("timesheet"); //getSheetAt(0);
        HSSFRow currentRow;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        sheet.getRow(4).getCell(1).setCellValue(periodName);
        int row = 7;
        int startRow = 0;
        int i = 1;
        while (i <= days) {
            currentRow = sheet.getRow(row);
            if (currentRow.getRowNum() > 47)
                break;
            String day = currentRow.getCell(0).getStringCellValue();

            if (day.startsWith("Total")) {
                evaluator.evaluateFormulaCell(currentRow.getCell(2));
                evaluator.evaluateFormulaCell(currentRow.getCell(4));
                row++;
                continue;
            }

            if (startRow == 0) {
                if (dayOfWeek.equals(day.substring(0, 3))) {
                    startRow = currentRow.getRowNum();
                    System.out.println("Starting row found: " + startRow + 1);
                } else {
                    row++;
                    continue;
                }
            }
            cal.set(Calendar.DATE, i);
            String date = sdf.format(cal.getTime());
            if (!day.equals("Saturday") && !day.equals("Sunday") && bankHolidays.get(date) == null
                    && holidays.get(date) == null) {
                currentRow.getCell(1).setCellValue(date);
                currentRow.getCell(2).setCellValue(defaultHours); // regular hours
                //currentRow.getCell(3).setCellValue(defaultHours);   // overtime hours
                currentRow.getCell(4).setCellValue(defaultHours); // total hours                    
            }
            i++;
            row++;
        }
        currentRow = sheet.getRow(46);
        evaluator.evaluateFormulaCell(currentRow.getCell(2));
        evaluator.evaluateFormulaCell(currentRow.getCell(4));
        currentRow = sheet.getRow(47);
        evaluator.evaluateFormulaCell(currentRow.getCell(2));
        evaluator.evaluateFormulaCell(currentRow.getCell(4));
        response = outFilePath.replace("#MONTH#", periodName);
        wb.write(new FileOutputStream(response));

    } catch (IOException ex) {
        Logger.getLogger(Timesheets.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Timesheet created.");
    return response;
}

From source file:mchs.neverforget.NeverForgetActivity.java

public static int daysBetween(Calendar day1, Calendar day2) {
    Calendar dayOne = (Calendar) day1.clone(), dayTwo = (Calendar) day2.clone();

    if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
        return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
    } else {//from w w  w .j av a 2  s  . c  om
        if (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
            //swap them
            Calendar temp = dayOne;
            dayOne = dayTwo;
            dayTwo = temp;
        }
        int extraDays = 0;

        int dayOneOriginalYearDays = dayOne.get(Calendar.DAY_OF_YEAR);

        while (dayOne.get(Calendar.YEAR) > dayTwo.get(Calendar.YEAR)) {
            dayOne.add(Calendar.YEAR, -1);
            // getActualMaximum() important for leap years
            extraDays += dayOne.getActualMaximum(Calendar.DAY_OF_YEAR);
        }

        return extraDays - dayTwo.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays;
    }
}

From source file:org.kuali.kfs.module.endow.businessobject.lookup.CalculateProcessDateUsingFrequencyCodeService.java

/**
 * This method will check and return maximum days in a month.
 * @param monthNumber The number of the month to test for maximum days..
 * @return maxDays Maximum number of days in the month of February for calendar.
 *///from w w w  .ja  v  a  2 s  .c  om
protected int checkMaximumDaysInMonth(int monthNumber) {
    int maxDays;

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, monthNumber);
    maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    return maxDays;
}

From source file:com.joinsystem.goku.common.utils.DateUtil.java

/**
 * /*w w  w . ja  v  a  2s .c  o  m*/
 * 
 * @param d1
 * @param d2
 * @return
 */
public static int diffDays(Calendar d1, Calendar d2) {
    if (d1.after(d2)) {
        java.util.Calendar swap = d1;
        d1 = d2;
        d2 = swap;
    }
    int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
    int y2 = d2.get(Calendar.YEAR);
    if (d1.get(Calendar.YEAR) != y2) {
        d1 = (Calendar) d1.clone();
        do {
            days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 
            d1.add(Calendar.YEAR, 1);
        } while (d1.get(Calendar.YEAR) != y2);
    }
    return days;
}

From source file:com.hihframework.core.utils.DateUtils.java

/**
 * ?//from   w  w w. java  2s . co  m
 *
 * @param date java.sql.Date ??
 * @return Date[] java.sql.Date 
 */
public static java.sql.Date[] getMonthDays(java.sql.Date date) {

    Calendar cale = Calendar.getInstance();
    cale.setTime(date);

    int today = cale.get(Calendar.DAY_OF_MONTH);
    int days = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
    long millis = cale.getTimeInMillis();

    java.sql.Date dates[] = new java.sql.Date[days];
    for (int i = 1; i <= days; i++) {
        long sub = (today - i) * 24 * 60 * 60 * 1000L;
        dates[i - 1] = new java.sql.Date(millis - sub);
    }

    cale = null;
    return dates;
}