Example usage for java.util Calendar SUNDAY

List of usage examples for java.util Calendar SUNDAY

Introduction

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

Prototype

int SUNDAY

To view the source code for java.util Calendar SUNDAY.

Click Source Link

Document

Value of the #DAY_OF_WEEK field indicating Sunday.

Usage

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static Object dateDiff(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 3) {
        try {//  w ww.j  a v  a2 s. com
            if (isNull(ArgList, new int[] { 0, 1, 2 })) {
                return new Double(Double.NaN);
            } else if (isUndefined(ArgList, new int[] { 0, 1, 2 })) {
                return undefinedValue;
            } else {
                java.util.Date dIn1 = (java.util.Date) ArgList[0];
                java.util.Date dIn2 = (java.util.Date) ArgList[1];
                String strType = ((String) ArgList[2]).toLowerCase();
                int iRC = 0;

                Calendar startDate = Calendar.getInstance();
                Calendar endDate = Calendar.getInstance();
                startDate.setTime(dIn1);
                endDate.setTime(dIn2);

                /*
                 * Changed by: Ingo Klose, SHS VIVEON AG, Date: 27.04.2007
                 *
                 * Calculating time differences using getTimeInMillis() leads to false results when crossing Daylight
                 * Savingstime borders. In order to get correct results the time zone offsets have to be added.
                 *
                 * Fix: 1. calculate correct milli seconds for start and end date 2. replace endDate.getTimeInMillis() with
                 * endL and startDate.getTimeInMillis() with startL
                 */
                long endL = endDate.getTimeInMillis()
                        + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                long startL = startDate.getTimeInMillis()
                        + startDate.getTimeZone().getOffset(startDate.getTimeInMillis());

                if (strType.equals("y")) {
                    return new Double(endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
                } else if (strType.equals("m")) {
                    int iMonthsToAdd = (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR)) * 12;
                    return new Double(
                            (endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH)) + iMonthsToAdd);
                } else if (strType.equals("d")) {
                    return new Double(((endL - startL) / 86400000));
                } else if (strType.equals("wd")) {
                    int iOffset = -1;
                    if (endDate.before(startDate)) {
                        iOffset = 1;
                    }
                    while ((iOffset == 1 && endL < startL) || (iOffset == -1 && endL > startL)) {
                        int day = endDate.get(Calendar.DAY_OF_WEEK);
                        if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY)) {
                            iRC++;
                        }
                        endDate.add(Calendar.DATE, iOffset);
                        endL = endDate.getTimeInMillis()
                                + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                    }
                    return new Double(iRC);
                } else if (strType.equals("w")) {
                    int iDays = (int) ((endL - startL) / 86400000);
                    return new Double(iDays / 7);
                } else if (strType.equals("ss")) {
                    return new Double(((endL - startL) / 1000));
                } else if (strType.equals("mi")) {
                    return new Double(((endL - startL) / 60000));
                } else if (strType.equals("hh")) {
                    return new Double(((endL - startL) / 3600000));
                } else {
                    return new Double(((endL - startL) / 86400000));
                }
                /*
                 * End Bugfix
                 */
            }
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call dateDiff requires 3 arguments.");
    }
}

From source file:gov.opm.scrd.batchprocessing.jobs.BatchProcessingJob.java

/**
 * Checks whether the current day is a holiday.
 *
 * @param now The current day.// www  .ja  v  a  2  s. c  o  m
 * @return True if the current date is a holiday, false otherwise.
 * @throws BatchProcessingException If major error occurred.
 */
protected boolean isNowHoliday(Date now) throws BatchProcessingException {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(now);
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) {
        return true; // Sunday for 0 and Saturday for 6 are holidays
    }

    try {
        startTransaction();

        StoredProcedureQuery sp = entityManager.createNamedStoredProcedureQuery("IsThisHoliday");
        sp.setParameter("pDate2Test", now, TemporalType.DATE);

        Boolean result = (Boolean) sp.getSingleResult();

        commitTransaction();

        return result;
    } catch (PersistenceException pe) {
        throw new BatchProcessingException("Database error checking holiday.", pe);
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object dateDiff(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    if (ArgList.length == 3) {
        try {/*from w ww .  ja  v a2  s  .c o m*/
            if (isNull(ArgList, new int[] { 0, 1, 2 }))
                return new Double(Double.NaN);
            else if (isUndefined(ArgList, new int[] { 0, 1, 2 }))
                return undefinedValue;
            else {
                java.util.Date dIn1 = (java.util.Date) ArgList[0];
                java.util.Date dIn2 = (java.util.Date) ArgList[1];
                String strType = (String) ArgList[2];
                int iRC = 0;

                Calendar startDate = Calendar.getInstance();
                Calendar endDate = Calendar.getInstance();
                startDate.setTime(dIn1);
                endDate.setTime(dIn2);

                /* Changed by:    Ingo Klose, SHS VIVEON AG,
                 * Date:      27.04.2007
                 *
                 * Calculating time differences using getTimeInMillis() leads to false results
                 * when crossing Daylight Savingstime borders. In order to get correct results
                 * the time zone offsets have to be added.
                 *
                 * Fix:       1.    calculate correct milli seconds for start and end date
                 *             2.    replace endDate.getTimeInMillis() with endL
                 *                and startDate.getTimeInMillis() with startL
                 */
                long endL = endDate.getTimeInMillis()
                        + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                long startL = startDate.getTimeInMillis()
                        + startDate.getTimeZone().getOffset(startDate.getTimeInMillis());

                if (strType.toLowerCase().equals("y")) {
                    return new Double(endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
                } else if (strType.toLowerCase().equals("m")) {
                    int iMonthsToAdd = (int) (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR)) * 12;
                    return new Double(
                            (endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH)) + iMonthsToAdd);
                } else if (strType.toLowerCase().equals("d")) {
                    return new Double(((endL - startL) / 86400000));
                } else if (strType.toLowerCase().equals("wd")) {
                    int iOffset = -1;
                    if (endDate.before(startDate))
                        iOffset = 1;
                    while ((iOffset == 1 && endL < startL) || (iOffset == -1 && endL > startL)) {
                        int day = endDate.get(Calendar.DAY_OF_WEEK);
                        if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
                            iRC++;
                        endDate.add(Calendar.DATE, iOffset);
                        endL = endDate.getTimeInMillis()
                                + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                    }
                    return new Double(iRC);
                } else if (strType.toLowerCase().equals("w")) {
                    int iDays = (int) ((endL - startL) / 86400000);
                    return new Double(iDays / 7);
                } else if (strType.toLowerCase().equals("ss")) {
                    return new Double(((endL - startL) / 1000));
                } else if (strType.toLowerCase().equals("mi")) {
                    return new Double(((endL - startL) / 60000));
                } else if (strType.toLowerCase().equals("hh")) {
                    return new Double(((endL - startL) / 3600000));
                } else {
                    return new Double(((endL - startL) / 86400000));
                }
                /*
                   * End Bugfix
                 */
            }
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call dateDiff requires 3 arguments.");
    }
}

From source file:KitchenManagement.FoodDemandForecastingAndPlanning.FoodDemandForecastingAndPlanningBean.java

@Override
public Boolean generateMaterialRequirementPlan(Long storeId) {
    System.out.println("generateMaterialRequirementPlan is called.");
    try {/* www. j a  va2  s.co  m*/
        StoreEntity store = em.find(StoreEntity.class, storeId);
        Query q = em.createQuery("select s from MonthScheduleEntity s");
        List<MonthScheduleEntity> scheduleList = q.getResultList();
        MonthScheduleEntity schedule = scheduleList.get(scheduleList.size() - 1);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, schedule.getYear());
        calendar.set(Calendar.MONTH, schedule.getMonth() - 1);

        Query q1 = em.createQuery(
                "select mps from MasterProductionScheduleEntity mps where mps.store.id = ?1 and mps.schedule.id = ?2")
                .setParameter(1, storeId).setParameter(2, schedule.getId());
        List<MasterProductionScheduleEntity> mpsList = (List<MasterProductionScheduleEntity>) q1
                .getResultList();

        for (MasterProductionScheduleEntity mps : mpsList) {
            if (mps.getMenuItem().getRecipe() != null) {
                for (LineItemEntity lineItem : mps.getMenuItem().getRecipe().getListOfLineItems()) {
                    Query q2 = em.createQuery(
                            "select mr from MaterialRequirementEntity mr where mr.store.id = ?1 and mr.rawIngredient.SKU = ?2 and mr.schedule.id = ?3 and mr.mps.id = ?4")
                            .setParameter(1, storeId).setParameter(2, lineItem.getItem().getSKU())
                            .setParameter(3, schedule.getId()).setParameter(4, mps.getId());
                    List<MaterialRequirementEntity> mrList = (List<MaterialRequirementEntity>) q2
                            .getResultList();
                    System.out.println("mrList.getSize(): " + mrList.size());
                    for (MaterialRequirementEntity mr : mrList) {
                        em.remove(mr);
                    }
                    em.flush();
                }
            }
        }

        for (MasterProductionScheduleEntity mps : mpsList) {
            if (mps.getMenuItem().getRecipe() != null && mps.getAmount_month() != 0) {
                for (LineItemEntity lineItem : mps.getMenuItem().getRecipe().getListOfLineItems()) {

                    Query query1 = em.createQuery(
                            "select mr from MaterialRequirementEntity mr where mr.store.id = ?1 and mr.rawIngredient.SKU = ?2 and mr.schedule.id =?3 and mr.day = ?4 ")
                            .setParameter(1, storeId).setParameter(2, lineItem.getItem().getSKU())
                            .setParameter(3, schedule.getId()).setParameter(4, 1);
                    if (mps.getAmount_week1() != 0) {
                        if (query1.getResultList().isEmpty()) {
                            MaterialRequirementEntity MR1 = new MaterialRequirementEntity();
                            MR1.setStore(store);
                            MR1.setMps(mps);
                            MR1.setRawIngredient((RawIngredientEntity) lineItem.getItem());
                            MR1.setQuantity(mps.getAmount_week1() * lineItem.getQuantity()
                                    / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                            MR1.setSchedule(schedule);
                            MR1.setDay(1);
                            em.persist(MR1);
                        } else {
                            MaterialRequirementEntity MR1 = (MaterialRequirementEntity) query1.getResultList()
                                    .get(0);
                            MR1.setQuantity(MR1.getQuantity() + mps.getAmount_week1() * lineItem.getQuantity()
                                    / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                            em.merge(MR1);
                        }
                    }

                    calendar.set(Calendar.WEEK_OF_MONTH, 2);
                    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                    System.out.println("calendar.get(Calendar.DAY_OF_MONTH) week2 :"
                            + calendar.get(Calendar.DAY_OF_MONTH));
                    Query query2 = em.createQuery(
                            "select mr from MaterialRequirementEntity mr where mr.store.id = ?1 and mr.rawIngredient.SKU = ?2 and mr.schedule.id =?3 and mr.day = ?4 ")
                            .setParameter(1, storeId).setParameter(2, lineItem.getItem().getSKU())
                            .setParameter(3, schedule.getId())
                            .setParameter(4, calendar.get(Calendar.DAY_OF_MONTH));

                    if (query2.getResultList().isEmpty()) {
                        MaterialRequirementEntity MR2 = new MaterialRequirementEntity();
                        MR2.setStore(store);
                        MR2.setMps(mps);
                        MR2.setRawIngredient((RawIngredientEntity) lineItem.getItem());
                        System.out.println("persis-mps.getAmount_week2(): " + mps.getAmount_week2());
                        System.out.println("lineItem.getQuantity(): " + lineItem.getQuantity());
                        System.out.println("mps.getMenuItem().getRecipe().getBroadLotSize()"
                                + mps.getMenuItem().getRecipe().getBroadLotSize());
                        MR2.setQuantity(mps.getAmount_week2() * lineItem.getQuantity()
                                / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                        MR2.setSchedule(schedule);
                        MR2.setDay(calendar.get(Calendar.DAY_OF_MONTH));
                        em.persist(MR2);
                        System.out.println("persis-MR2.getQuantity(): " + MR2.getQuantity());
                    } else {
                        MaterialRequirementEntity MR2 = (MaterialRequirementEntity) query2.getResultList()
                                .get(0);
                        System.out.println("persis-mps.getAmount_week2(): " + mps.getAmount_week2());
                        System.out.println("lineItem.getQuantity(): " + lineItem.getQuantity());
                        System.out.println("mps.getMenuItem().getRecipe().getBroadLotSize()"
                                + mps.getMenuItem().getRecipe().getBroadLotSize());

                        MR2.setQuantity(MR2.getQuantity() + mps.getAmount_week2() * lineItem.getQuantity()
                                / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                        em.merge(MR2);
                        System.out.println("merge-MR2.getQuantity(): " + MR2.getQuantity());
                    }

                    calendar.set(Calendar.WEEK_OF_MONTH, 3);
                    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                    System.out.println("calendar.get(Calendar.DAY_OF_MONTH) week3 :"
                            + calendar.get(Calendar.DAY_OF_MONTH));
                    Query query3 = em.createQuery(
                            "select mr from MaterialRequirementEntity mr where mr.store.id = ?1 and mr.rawIngredient.SKU = ?2 and mr.schedule.id =?3 and mr.day = ?4 ")
                            .setParameter(1, storeId).setParameter(2, lineItem.getItem().getSKU())
                            .setParameter(3, schedule.getId())
                            .setParameter(4, calendar.get(Calendar.DAY_OF_MONTH));
                    if (query3.getResultList().isEmpty()) {
                        MaterialRequirementEntity MR3 = new MaterialRequirementEntity();
                        MR3.setStore(store);
                        MR3.setMps(mps);
                        MR3.setRawIngredient((RawIngredientEntity) lineItem.getItem());
                        MR3.setQuantity(mps.getAmount_week3() * lineItem.getQuantity()
                                / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                        MR3.setSchedule(schedule);
                        MR3.setDay(calendar.get(Calendar.DAY_OF_MONTH));
                        em.persist(MR3);
                    } else {
                        MaterialRequirementEntity MR3 = (MaterialRequirementEntity) query3.getResultList()
                                .get(0);
                        MR3.setQuantity(MR3.getQuantity() + mps.getAmount_week3() * lineItem.getQuantity()
                                / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                        em.merge(MR3);
                    }

                    calendar.set(Calendar.WEEK_OF_MONTH, 4);
                    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                    System.out.println("calendar.get(Calendar.DAY_OF_MONTH) week4 :"
                            + calendar.get(Calendar.DAY_OF_MONTH));
                    Query query4 = em.createQuery(
                            "select mr from MaterialRequirementEntity mr where mr.store.id = ?1 and mr.rawIngredient.SKU = ?2 and mr.schedule.id =?3 and mr.day = ?4 ")
                            .setParameter(1, storeId).setParameter(2, lineItem.getItem().getSKU())
                            .setParameter(3, schedule.getId())
                            .setParameter(4, calendar.get(Calendar.DAY_OF_MONTH));
                    if (query4.getResultList().isEmpty()) {
                        MaterialRequirementEntity MR4 = new MaterialRequirementEntity();
                        MR4.setStore(store);
                        MR4.setMps(mps);
                        MR4.setRawIngredient((RawIngredientEntity) lineItem.getItem());
                        MR4.setQuantity(mps.getAmount_week4() * lineItem.getQuantity()
                                / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                        MR4.setSchedule(schedule);
                        MR4.setDay(calendar.get(Calendar.DAY_OF_MONTH));
                        em.persist(MR4);
                    } else {
                        MaterialRequirementEntity MR4 = (MaterialRequirementEntity) query4.getResultList()
                                .get(0);
                        MR4.setQuantity(MR4.getQuantity() + mps.getAmount_week4() * lineItem.getQuantity()
                                / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                        em.merge(MR4);
                    }

                    if (mps.getAmount_week5() != 0) {
                        calendar.set(Calendar.WEEK_OF_MONTH, 5);
                        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                        System.out.println("calendar.get(Calendar.DAY_OF_MONTH) week5 :"
                                + calendar.get(Calendar.DAY_OF_MONTH));
                        Query query5 = em.createQuery(
                                "select mr from MaterialRequirementEntity mr where mr.store.id = ?1 and mr.rawIngredient.SKU = ?2 and mr.schedule.id =?3 and mr.day = ?4 ")
                                .setParameter(1, storeId).setParameter(2, lineItem.getItem().getSKU())
                                .setParameter(3, schedule.getId())
                                .setParameter(4, calendar.get(Calendar.DAY_OF_MONTH));
                        if (query5.getResultList().isEmpty()) {
                            MaterialRequirementEntity MR5 = new MaterialRequirementEntity();
                            MR5.setStore(store);
                            MR5.setMps(mps);
                            MR5.setRawIngredient((RawIngredientEntity) lineItem.getItem());
                            MR5.setQuantity(mps.getAmount_week5() * lineItem.getQuantity()
                                    / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                            MR5.setSchedule(schedule);
                            MR5.setDay(calendar.get(Calendar.DAY_OF_MONTH));
                            em.persist(MR5);
                        } else {
                            MaterialRequirementEntity MR5 = (MaterialRequirementEntity) query5.getResultList()
                                    .get(0);
                            MR5.setQuantity(MR5.getQuantity() + mps.getAmount_week5() * lineItem.getQuantity()
                                    / mps.getMenuItem().getRecipe().getBroadLotSize() + 1);
                            em.merge(MR5);
                        }
                    }
                }
            }
        }
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static Object dateDiff(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    if (ArgList.length == 3) {
        try {//from  ww w.  j  a  v  a  2  s .  c  om
            if (isNull(ArgList, new int[] { 0, 1, 2 }))
                return new Double(Double.NaN);
            else if (isUndefined(ArgList, new int[] { 0, 1, 2 }))
                return Context.getUndefinedValue();
            else {
                java.util.Date dIn1 = (java.util.Date) Context.jsToJava(ArgList[0], java.util.Date.class);
                java.util.Date dIn2 = (java.util.Date) Context.jsToJava(ArgList[1], java.util.Date.class);
                String strType = Context.toString(ArgList[2]);
                int iRC = 0;

                Calendar startDate = Calendar.getInstance();
                Calendar endDate = Calendar.getInstance();
                startDate.setTime(dIn1);
                endDate.setTime(dIn2);

                /* Changed by:    Ingo Klose, SHS VIVEON AG,
                 * Date:      27.04.2007
                 *
                 * Calculating time differences using getTimeInMillis() leads to false results
                 * when crossing Daylight Savingstime borders. In order to get correct results
                 * the time zone offsets have to be added.
                 *
                 * Fix:       1.    calculate correct milli seconds for start and end date
                 *             2.    replace endDate.getTimeInMillis() with endL
                 *                and startDate.getTimeInMillis() with startL
                 */
                long endL = endDate.getTimeInMillis()
                        + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                long startL = startDate.getTimeInMillis()
                        + startDate.getTimeZone().getOffset(startDate.getTimeInMillis());

                if (strType.toLowerCase().equals("y")) {
                    return new Double(endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
                } else if (strType.toLowerCase().equals("m")) {
                    int iMonthsToAdd = (int) (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR)) * 12;
                    return new Double(
                            (endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH)) + iMonthsToAdd);
                } else if (strType.toLowerCase().equals("d")) {
                    return new Double(((endL - startL) / 86400000));
                } else if (strType.toLowerCase().equals("wd")) {
                    int iOffset = -1;
                    if (endDate.before(startDate))
                        iOffset = 1;
                    while (endL < startL || endL > startL) {
                        int day = endDate.get(Calendar.DAY_OF_WEEK);
                        if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
                            iRC++;
                        endDate.add(Calendar.DATE, iOffset);
                        endL = endDate.getTimeInMillis()
                                + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                    }
                    return new Double(iRC);
                } else if (strType.toLowerCase().equals("w")) {
                    int iDays = (int) ((endL - startL) / 86400000);
                    return new Double(iDays / 7);
                } else if (strType.toLowerCase().equals("ss")) {
                    return new Double(((endL - startL) / 1000));
                } else if (strType.toLowerCase().equals("mi")) {
                    return new Double(((endL - startL) / 60000));
                } else if (strType.toLowerCase().equals("hh")) {
                    return new Double(((endL - startL) / 3600000));
                } else {
                    return new Double(((endL - startL) / 86400000));
                }
                /*
                   * End Bugfix
                 */
            }
        } catch (Exception e) {
            throw Context.reportRuntimeError(e.toString());
        }
    } else {
        throw Context.reportRuntimeError("The function call dateDiff requires 3 arguments.");
    }
}

From source file:com.commander4j.util.JUtility.java

/**
 * Method getDayOfWeekString1.//www . j av a 2 s .  co m
 * 
 * @param currentDate
 *            Calendar
 * @return int
 */
public static String getDayOfWeekString1(Calendar currentDate) {
    int temp = getDayOfWeek(currentDate);
    String result = "";

    switch (temp) {
    case Calendar.MONDAY:
        result = "A";
        break;
    case Calendar.TUESDAY:
        result = "B";
        break;
    case Calendar.WEDNESDAY:
        result = "C";
        break;
    case Calendar.THURSDAY:
        result = "D";
        break;
    case Calendar.FRIDAY:
        result = "E";
        break;
    case Calendar.SATURDAY:
        result = "F";
        break;
    case Calendar.SUNDAY:
        result = "G";
        break;
    }

    return result;
}

From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static Object dateDiff(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    if (ArgList.length == 3) {
        try {/*from   w ww  .j  av  a2s.  co  m*/
            if (isNull(ArgList, new int[] { 0, 1, 2 })) {
                return new Double(Double.NaN);
            } else if (isUndefined(ArgList, new int[] { 0, 1, 2 })) {
                return Context.getUndefinedValue();
            } else {
                java.util.Date dIn1 = (java.util.Date) Context.jsToJava(ArgList[0], java.util.Date.class);
                java.util.Date dIn2 = (java.util.Date) Context.jsToJava(ArgList[1], java.util.Date.class);
                String strType = Context.toString(ArgList[2]).toLowerCase();
                int iRC = 0;

                Calendar startDate = Calendar.getInstance();
                Calendar endDate = Calendar.getInstance();
                startDate.setTime(dIn1);
                endDate.setTime(dIn2);

                /*
                 * Changed by: Ingo Klose, SHS VIVEON AG, Date: 27.04.2007
                 *
                 * Calculating time differences using getTimeInMillis() leads to false results when crossing Daylight
                 * Savingstime borders. In order to get correct results the time zone offsets have to be added.
                 *
                 * Fix: 1. calculate correct milli seconds for start and end date 2. replace endDate.getTimeInMillis() with
                 * endL and startDate.getTimeInMillis() with startL
                 */
                long endL = endDate.getTimeInMillis()
                        + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                long startL = startDate.getTimeInMillis()
                        + startDate.getTimeZone().getOffset(startDate.getTimeInMillis());

                if (strType.equals("y")) {
                    return new Double(endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR));
                } else if (strType.equals("m")) {
                    int iMonthsToAdd = (endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR)) * 12;
                    return new Double(
                            (endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH)) + iMonthsToAdd);
                } else if (strType.equals("d")) {
                    return new Double(((endL - startL) / 86400000));
                } else if (strType.equals("wd")) {
                    int iOffset = -1;
                    if (endDate.before(startDate)) {
                        iOffset = 1;
                    }
                    while ((iOffset == 1 && endL < startL) || (iOffset == -1 && endL > startL)) {
                        int day = endDate.get(Calendar.DAY_OF_WEEK);
                        if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY)) {
                            iRC++;
                        }
                        endDate.add(Calendar.DATE, iOffset);
                        endL = endDate.getTimeInMillis()
                                + endDate.getTimeZone().getOffset(endDate.getTimeInMillis());
                    }
                    return new Double(iRC);
                } else if (strType.equals("w")) {
                    int iDays = (int) ((endL - startL) / 86400000);
                    return new Double(iDays / 7);
                } else if (strType.equals("ss")) {
                    return new Double(((endL - startL) / 1000));
                } else if (strType.equals("mi")) {
                    return new Double(((endL - startL) / 60000));
                } else if (strType.equals("hh")) {
                    return new Double(((endL - startL) / 3600000));
                } else {
                    return new Double(((endL - startL) / 86400000));
                }
                /*
                 * End Bugfix
                 */
            }
        } catch (Exception e) {
            throw Context.reportRuntimeError(e.toString());
        }
    } else {
        throw Context.reportRuntimeError("The function call dateDiff requires 3 arguments.");
    }
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static Object getNextWorkingDay(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    // (Date dIn){
    if (ArgList.length == 1) {
        try {/*from  ww  w  .j a  v  a2 s  . co m*/
            if (isNull(ArgList[0])) {
                return null;
            } else if (isUndefined(ArgList[0])) {
                return undefinedValue;
            }
            java.util.Date dIn = (java.util.Date) ArgList[0];
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(dIn);
            startDate.add(Calendar.DATE, 1);
            while (startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                    || startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                startDate.add(Calendar.DATE, 1);
            }
            return startDate.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call getNextWorkingDay requires 1 argument.");
    }
}

From source file:com.rogchen.common.xml.UtilDateTime.java

public static int weekNumber(Timestamp input, int startOfWeek) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(startOfWeek);

    if (startOfWeek == Calendar.MONDAY) {
        calendar.setMinimalDaysInFirstWeek(4);
    } else if (startOfWeek == Calendar.SUNDAY) {
        calendar.setMinimalDaysInFirstWeek(3);
    }/* ww  w .  j a  v  a 2 s.  co m*/

    calendar.setTime(new Date(input.getTime()));
    return calendar.get(Calendar.WEEK_OF_YEAR);
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object getNextWorkingDay(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    //(Date dIn){
    if (ArgList.length == 1) {
        try {//w w w  .  j  a  va 2  s. c  o m
            if (isNull(ArgList[0]))
                return null;
            else if (isUndefined(ArgList[0]))
                return undefinedValue;
            java.util.Date dIn = (java.util.Date) ArgList[0];
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(dIn);
            startDate.add(Calendar.DATE, 1);
            while (startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                    || startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                startDate.add(Calendar.DATE, 1);
            }
            return startDate.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call getNextWorkingDay requires 1 argument.");
    }
}