Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

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

Prototype

public GregorianCalendar() 

Source Link

Document

Constructs a default GregorianCalendar using the current time in the default time zone with the default Locale.Category#FORMAT FORMAT locale.

Usage

From source file:py.una.pol.karaku.services.util.ServiceUtil.java

/**
 * Converts a {@link Date} into an instance of XMLGregorianCalendar
 * /*from w  ww . j  a v a2s .  c o m*/
 * @param date
 *            Instance of {@link Date} or a null reference
 * @return {@link XMLGregorianCalendar} instance whose value is based upon
 *         the value in the date parameter. If the date parameter is null
 *         then this method will simply return null.
 */
public XMLGregorianCalendar asXMLGregorianCalendar(Date date) {

    if (date == null) {
        return null;
    }
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(date.getTime());
    return df.newXMLGregorianCalendar(gc);
}

From source file:com.esofthead.mycollab.module.user.service.mybatis.UserPreferenceServiceImpl.java

@Override
public UserPreference getPreferenceOfUser(String username, int accountId) {
    UserPreferenceExample ex = new UserPreferenceExample();
    ex.createCriteria().andUsernameEqualTo(username).andSaccountidEqualTo(accountId);
    List<UserPreference> userPreferences = userPreferenceMapper.selectByExample(ex);
    UserPreference pref = null;// www .jav a2s . co m

    if (CollectionUtils.isNotEmpty(userPreferences)) {
        pref = userPreferences.get(0);
    } else {
        // create default user preference then save to database
        pref = new UserPreference();
        pref.setLastaccessedtime(new GregorianCalendar().getTime());
        pref.setUsername(username);
        pref.setSaccountid(accountId);
        userPreferenceMapper.insert(pref);
    }

    return pref;
}

From source file:eu.cloudscale.showcase.generate.GenerateMongo.java

@Override
public void populateOrdersAndCC_XACTSTable() {
    GregorianCalendar cal;/*from  www.j a va  2s. c o m*/
    String[] credit_cards = { "VISA", "MASTERCARD", "DISCOVER", "AMEX", "DINERS" };
    int num_card_types = 5;
    String[] ship_types = { "AIR", "UPS", "FEDEX", "SHIP", "COURIER", "MAIL" };
    int num_ship_types = 6;

    String[] status_types = { "PROCESSING", "SHIPPED", "PENDING", "DENIED" };
    int num_status_types = 4;

    // Order variables
    int O_C_ID;
    java.sql.Timestamp O_DATE;
    double O_SUB_TOTAL;
    double O_TAX;
    double O_TOTAL;
    String O_SHIP_TYPE;
    java.sql.Timestamp O_SHIP_DATE;
    int O_BILL_ADDR_ID, O_SHIP_ADDR_ID;
    String O_STATUS;

    String CX_TYPE;
    int CX_NUM;
    String CX_NAME;
    java.sql.Date CX_EXPIRY;
    String CX_AUTH_ID;
    int CX_CO_ID;

    System.out.println("Populating ORDERS, ORDER_LINES, CC_XACTS with " + NUM_ORDERS + " orders");

    System.out.print("Complete (in 10,000's): ");

    for (int i = 1; i <= NUM_ORDERS; i++) {
        if (i % 10000 == 0)
            System.out.print(i / 10000 + " ");

        int num_items = getRandomInt(1, 5);
        O_C_ID = getRandomInt(1, NUM_CUSTOMERS);
        cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, -1 * getRandomInt(1, 60));
        O_DATE = new java.sql.Timestamp(cal.getTime().getTime());
        O_SUB_TOTAL = (double) getRandomInt(1000, 999999) / 100;
        O_TAX = O_SUB_TOTAL * 0.0825;
        O_TOTAL = O_SUB_TOTAL + O_TAX + 3.00 + num_items;
        O_SHIP_TYPE = ship_types[getRandomInt(0, num_ship_types - 1)];
        cal.add(Calendar.DAY_OF_YEAR, getRandomInt(0, 7));
        O_SHIP_DATE = new java.sql.Timestamp(cal.getTime().getTime());

        O_BILL_ADDR_ID = getRandomInt(1, 2 * NUM_CUSTOMERS);
        O_SHIP_ADDR_ID = getRandomInt(1, 2 * NUM_CUSTOMERS);
        O_STATUS = status_types[getRandomInt(0, num_status_types - 1)];

        Orders order = new Orders();

        // Set parameter
        order.setOId(i);
        order.setCustomer(customerDao.findById(O_C_ID));
        order.setODate(new Date(O_DATE.getTime()));
        order.setOSubTotal(O_SUB_TOTAL);
        order.setOTax(O_TAX);
        order.setOTotal(O_TOTAL);
        order.setOShipType(O_SHIP_TYPE);
        order.setOShipDate(O_SHIP_DATE);
        order.setAddressByOBillAddrId(addressDao.findById(O_BILL_ADDR_ID));
        order.setAddressByOShipAddrId(addressDao.findById(O_SHIP_ADDR_ID));
        order.setOStatus(O_STATUS);
        order.setCcXactses(new HashSet<ICcXacts>());
        order.setOrderLines(new HashSet<IOrderLine>());

        for (int j = 1; j <= num_items; j++) {
            int OL_ID = j;
            int OL_O_ID = i;
            int OL_I_ID = getRandomInt(1, NUM_ITEMS);
            int OL_QTY = getRandomInt(1, 300);
            double OL_DISCOUNT = (double) getRandomInt(0, 30) / 100;
            String OL_COMMENTS = getRandomAString(20, 100);

            OrderLine orderLine = new OrderLine();
            orderLine.setOlId(OL_ID);
            orderLine.setItem(itemDao.findById(OL_I_ID));
            orderLine.setOlQty(OL_QTY);
            orderLine.setOlDiscount(OL_DISCOUNT);
            orderLine.setOlComment(OL_COMMENTS);
            orderLine.setOrders(order);

            orderLineDao.shrani(orderLine);

            HashSet<IOrderLine> set = new HashSet<IOrderLine>();
            set.add(orderLine);
            set.addAll(order.getOrderLines());

            order.setOrderLines(set);
            ordersDao.shrani(order);
        }

        CX_TYPE = credit_cards[getRandomInt(0, num_card_types - 1)];
        CX_NUM = getRandomNString(16);
        CX_NAME = getRandomAString(14, 30);
        cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, getRandomInt(10, 730));
        CX_EXPIRY = new java.sql.Date(cal.getTime().getTime());
        CX_AUTH_ID = getRandomAString(15);
        CX_CO_ID = getRandomInt(1, 92);

        CcXacts ccXacts = new CcXacts();
        ccXacts.setId(i);
        ccXacts.setCountry(countryDao.findById(CX_CO_ID));
        ccXacts.setCxType(CX_TYPE);
        ccXacts.setCxNum(CX_NUM);
        ccXacts.setCxName(CX_NAME);
        ccXacts.setCxExpiry(CX_EXPIRY);
        ccXacts.setCxAuthId(CX_AUTH_ID);
        ccXacts.setCxXactAmt(O_TOTAL);
        ccXacts.setCxXactDate(O_SHIP_DATE);

        ccXacts.setOrders(order);
        ccXactsDao.shrani(ccXacts);

        HashSet<ICcXacts> set = new HashSet<ICcXacts>();
        set.add(ccXacts);
        set.addAll(order.getCcXactses());

        order.setCcXactses(set);

        ordersDao.shrani(order);

    }

    System.out.println("");
}

From source file:org.toobsframework.biz.validation.CustomValidationUtils.java

/**
 * Given three integer inputs representing a date, checks to 
 * see if the specified date is after today's date.
 * @param day - int 1-31/*from  w ww  .  j a  v a 2 s .co  m*/
 * @param month - int 1-12
 * @param year
 * @return
 */
public static boolean rejectIfDayMonthYearBeforeToday(Integer day, Integer month, Integer year) {
    if (day != null && month != null && year != null) {
        //construct Calendar object
        //set time fields with inputted parameters
        GregorianCalendar cal = new GregorianCalendar();
        // note month conversion: Calendar expects 0 based month param
        cal.set(year, month - 1, day);
        //get a date object out of the 
        Date closingDate = cal.getTime();

        //if the closing date is before right now,
        //then throw a validation error
        if (closingDate.before(new Date(System.currentTimeMillis()))) {
            return false;
        }
    }

    return true;
}

From source file:uk.org.openeyes.oink.hl7v2.A19Processor.java

public Message buildQuery(OINKRequestMessage request) throws HL7Exception, IOException, OinkException {
    // Validate OINKRequestMessage

    QRY_A19 msg = new QRY_A19();

    // MSH segment
    msg.initQuickstart("QRY", "A19", "P");

    // QRD segment
    QRD qrd = msg.getQRD();//from   w  w w  .j av a  2s .c o  m
    // Set query time
    TS ts = qrd.getQueryDateTime();
    ts.getTs1_TimeOfAnEvent().setValue(new GregorianCalendar());
    // Set query format code
    qrd.getQueryFormatCode().setValue("R");
    // Set query priority
    qrd.getQueryPriority().setValue("I");
    // Set query id
    String queryId = queryIdGenerator.nextString();
    qrd.getQueryID().setValue(queryId);
    // Set quantity limited request
    qrd.getQuantityLimitedRequest().getQuantity().setValue("10");
    qrd.getQuantityLimitedRequest().getUnits().getIdentifier().setValue("RD");
    // !! Set who subject filter
    if (isSearchByIdentifierNumber(request)) {
        log.info("Building an A19 with search by identifier");
        String system = extractSystem(request);
        String value = extractIdentifierValue(request);
        XCN who0 = qrd.getWhoSubjectFilter(0);
        who0.getIDNumber().setValue(value);
        who0.getIdentifierTypeCode().setValue(system);
    } else if (isSearchByFamilyName(request)) {
        log.info("Building an A19 with search by family name");
        String familyName = getQueryParameterValue(request, "family");
        XCN who0 = qrd.getWhoSubjectFilter(0);
        who0.getFamilyName().getSurname().setValue(familyName);
    } else {
        throw new OinkException("Only search by NHS number or family name currently supported");
    }
    // Set what subject filter
    qrd.getWhatSubjectFilter(0).getIdentifier().setValue("DEM");
    // Set what department data code SKIP?

    // QRF
    // None

    return msg;
}

From source file:com.mycollab.db.persistence.service.DefaultCrudService.java

@Override
public Integer updateWithSession(T record, String username) {
    try {//from  w w w.ja  v a2 s  .  c  o m
        PropertyUtils.setProperty(record, "lastupdatedtime", new GregorianCalendar().getTime());
    } catch (Exception e) {
    }

    if (cacheUpdateMethod == null) {
        findCacheUpdateMethod();
    }
    try {
        cacheUpdateMethod.invoke(getCrudMapper(), record);
        return 1;
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new MyCollabException(e);
    }
}

From source file:com.github.dozermapper.core.converters.XMLGregorianCalendarConverter.java

/**
 * {@inheritDoc}/*from  www .  jav a 2 s . c om*/
 */
public Object convert(Class destClass, Object srcObj) {
    Class sourceClass = srcObj.getClass();
    Calendar result = new GregorianCalendar();

    if (java.util.Date.class.isAssignableFrom(sourceClass)) {
        // Date --> XMLGregorianCalendar
        result.setTime(java.util.Date.class.cast(srcObj));
    } else if (Calendar.class.isAssignableFrom(sourceClass)) {
        // Calendar --> XMLGregorianCalendar
        Calendar c = Calendar.class.cast(srcObj);
        result.setTime(c.getTime());
        result.setTimeZone(c.getTimeZone());
    } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) {
        result = XMLGregorianCalendar.class.cast(srcObj).toGregorianCalendar();
    } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) {
        if ("".equals(String.class.cast(srcObj))) {
            return null;
        }

        try {
            long time = dateFormat.parse(String.class.cast(srcObj)).getTime();
            result.setTime(new Date(time));
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
    } else {
        try {
            long time = Long.parseLong(srcObj.toString());
            result.setTime(new Date(time));
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    if (dateFormat != null && String.class.isAssignableFrom(destClass)) {
        return dateFormat.format(result.getTime());
    }

    return dataTypeFactory().newXMLGregorianCalendar(GregorianCalendar.class.cast(result));
}

From source file:org.pegadi.server.publication.PublicationServerImpl.java

/**
 * Returns all years which have publications.
 *
 * @return an array of <code>int</code>'s
 */// w w w.  j  av  a  2  s. co  m
public List<Integer> getYearsWithPublications() { // never used?
    List<Publication> pubs = template.query("SELECT DISTINCT year(releasedate) FROM Publication", mapper);
    List<Integer> res = new ArrayList<Integer>();
    Calendar cal = new GregorianCalendar();
    for (Publication pub : pubs) {
        cal.setTime(pub.getReleaseDate());
        res.add(cal.get(Calendar.YEAR));
    }
    return res;
}

From source file:com.kloudtek.buildmagic.tools.createinstaller.deb.CopyrightList.java

public String getCurrentYear() {
    if (year == null) {
        year = Integer.toString(new GregorianCalendar().get(Calendar.YEAR));
    }/*from  ww w.  ja  v  a2  s  . c  o  m*/
    return year;
}

From source file:TimePeriod.java

/**
 * Setzt den von-Wert auf einen der Standard-Typen
 * /*from  w w w  .  java  2 s  . c o m*/
 * @param neuertyp
 */
public void setFromTyp(int neuertyp) {
    this.from_typ = neuertyp;
    if (from_typ != to_typ)
        typ = -1;
    else
        typ = neuertyp;
    switch (neuertyp) {
    case TODAY:
        from = new GregorianCalendar();
        setMidnight(from);
        break;
    case YESTERDAY:
        from = new GregorianCalendar();
        setMidnight(from);
        from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY);
        break;
    case THIS_WEEK:
        from = new GregorianCalendar();
        setMidnight(from);
        int day_of_week = from.get(Calendar.DAY_OF_WEEK);
        // unsere Woche beginnt am Montag. Montag hat den Wert 2
        int day_offset_von;
        // wenn es sonntag ist, wird die zurck liegende woche betrachtet
        if (day_of_week == 1) {
            day_offset_von = -6;
        } else {
            day_offset_von = 2 - day_of_week;
        }
        // bis ist logischerweise 6-Tage nach von
        from.setTimeInMillis(from.getTimeInMillis() + ONE_DAY * day_offset_von);
        break;
    case LAST_WEEK:
        // wie diese woche, nur 7 tage weiter zurck
        from = new GregorianCalendar();
        setMidnight(from);
        int day_of_week2 = from.get(Calendar.DAY_OF_WEEK);
        // unsere Woche beginnt am Montag. Montag hat den Wert 2
        int day_offset_von2;
        // wenn es sonntag ist, wird die zurck liegende woche betrachtet
        if (day_of_week2 == 1) {
            day_offset_von2 = -13;
        } else {
            day_offset_von2 = -5 - day_of_week2;
        }
        from.setTimeInMillis(from.getTimeInMillis() + ONE_DAY * day_offset_von2);
        break;
    case THIS_MONTH:
        from = new GregorianCalendar();
        setMidnight(from);
        from.set(Calendar.DAY_OF_MONTH, 1);
        break;
    case LAST_MONTH:
        from = new GregorianCalendar();
        setMidnight(from);
        from.set(Calendar.DAY_OF_MONTH, 1);
        // der erste tag des letzten Monats ist vielleicht nicht mehr in
        // diesem Jahr, also
        // rckwrts laufen, bis wieder ein erster Tag gefunden wird
        from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY);
        while (from.get(Calendar.DAY_OF_MONTH) != 1)
            from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY);
        break;
    case THIS_JEAR:
        from = new GregorianCalendar();
        setMidnight(from);
        from.set(Calendar.DAY_OF_MONTH, 1);
        from.set(Calendar.MONTH, 0);
        break;
    case LAST_JEAR:
        from = new GregorianCalendar();
        int jahr = from.get(Calendar.YEAR);
        jahr--;
        setMidnight(from);
        from.set(Calendar.DAY_OF_MONTH, 1);
        from.set(Calendar.MONTH, 0);
        from.set(Calendar.YEAR, jahr);
        break;
    case EVER:
        from = new GregorianCalendar();
        int jahr2 = from.get(Calendar.YEAR);
        setMidnight(from);
        from.set(2000, 0, 1, 0, 0, 0);
        break;
    }
    // Von darf nicht nach bis liegen, ist bis schon initialisiert, so muss
    // es angepasst werden
    if (to != null)
        if (to.before(from)) {
            to.setTimeInMillis(from.getTimeInMillis());
            set2359(to);
            if (from_typ == TODAY)
                to_typ = TODAY;
        }
}