Example usage for java.util Calendar HOUR_OF_DAY

List of usage examples for java.util Calendar HOUR_OF_DAY

Introduction

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

Prototype

int HOUR_OF_DAY

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

Click Source Link

Document

Field number for get and set indicating the hour of the day.

Usage

From source file:com.apextom.util.DateUtil.java

/**
 * ????.yyyy-MM-dd//from   w  w w .  j  a  va2 s  .c o  m
 *
 * @param date
 * @param formatFormat
 * @return
 */
public static Date getDayDate(String date, String formatFormat) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(strToDate(date, formatFormat));
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    return cal.getTime();
}

From source file:de.csdev.ebus.command.datatypes.ext.EBusTypeDateTime.java

@Override
public EBusDateTime decodeInt(byte[] data) throws EBusTypeException {

    IEBusType<Object> dateType = getDateType();
    IEBusType<Object> timeType = getTimeType();

    byte[] timeData = null;
    byte[] dateData = null;

    if (timeFirst) {
        timeData = Arrays.copyOf(data, timeType.getTypeLength());
        dateData = Arrays.copyOfRange(data, timeData.length, timeData.length + dateType.getTypeLength());
    } else {/*from   ww w .  j ava  2s  .c  o  m*/
        dateData = Arrays.copyOf(data, dateType.getTypeLength());
        timeData = Arrays.copyOfRange(data, dateData.length, dateData.length + timeType.getTypeLength());
    }

    EBusDateTime time = (EBusDateTime) timeType.decode(timeData);
    EBusDateTime date = (EBusDateTime) dateType.decode(dateData);

    Calendar calendar = date.getCalendar();

    calendar.set(Calendar.HOUR_OF_DAY, time.getCalendar().get(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, time.getCalendar().get(Calendar.MINUTE));
    calendar.set(Calendar.SECOND, time.getCalendar().get(Calendar.SECOND));

    return new EBusDateTime(calendar, false, false);
}

From source file:eu.annocultor.converters.solr.SolrPeriodsTagger.java

static Date endOfDay(Date date) {
    if (date == null) {
        return null;
    }/*from   ww w. j  a  va  2s  .  com*/
    Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    return calendar.getTime();
}

From source file:io.github.microcks.listener.DailyStatisticsFeeder.java

@Override
public void onApplicationEvent(MockInvocationEvent event) {
    log.debug(/*from  w  w w  . ja va2s  .c  o  m*/
            "Received a MockInvocationEvent on " + event.getServiceName() + " - v" + event.getServiceVersion());

    // Compute day string representation.
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(event.getInvocationTimestamp());

    // Computing keys based on invocation date.
    int month = calendar.get(Calendar.MONTH) + 1;
    String monthStr = (month < 10 ? "0" : "") + String.valueOf(month);
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    String dayOfMonthStr = (dayOfMonth < 10 ? "0" : "") + String.valueOf(dayOfMonth);

    String day = String.valueOf(calendar.get(Calendar.YEAR)) + monthStr + dayOfMonthStr;
    String hourKey = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
    String minuteKey = String
            .valueOf((60 * calendar.get(Calendar.HOUR_OF_DAY)) + calendar.get(Calendar.MINUTE));
    if (log.isDebugEnabled()) {
        log.debug("hourKey for statistic is " + hourKey);
        log.debug("minuteKey for statistic is " + minuteKey);
    }

    // First check if there's a statistic document for invocation day.
    DailyStatistic statistic = statisticsRepository.findByDayAndServiceNameAndServiceVersion(day,
            event.getServiceName(), event.getServiceVersion());

    if (statistic == null) {
        // No statistic's yet...
        log.debug("There's no statistics for " + day + " yet. Create one....");
        // Initialize a new 0 filled structure.
        statistic = new DailyStatistic();
        statistic.setDay(day);
        statistic.setServiceName(event.getServiceName());
        statistic.setServiceVersion(event.getServiceVersion());
        statistic.setHourlyCount(initializeHourlyMap());
        statistic.setMinuteCount(initializeMinuteMap());
        // Now set first values before saving.
        statistic.setDailyCount(1);
        statistic.getHourlyCount().put(hourKey, 1);
        statistic.getMinuteCount().put(minuteKey, 1);
        statisticsRepository.save(statistic);
    } else {
        // Already a statistic document for this day, increment fields.
        log.debug("Found an existing statistic document for " + day);
        statisticsRepository.incrementDailyStatistic(day, event.getServiceName(), event.getServiceVersion(),
                hourKey, minuteKey);
    }

    log.debug("Processing of MockInvocationEvent done !");
}

From source file:Main.java

/**
 * Parse a date from ISO-8601 formatted string. It expects a format
 * yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
 *
 * @param date ISO string to parse in the appropriate format.
 * @return the parsed date//from  ww  w .j ava2 s  . co  m
 * @throws IllegalArgumentException if the date is not in the appropriate format
 */
public static Date parse(String date) {
    try {
        int offset = 0;

        // extract year
        int year = parseInt(date, offset, offset += 4);
        checkOffset(date, offset, '-');

        // extract month
        int month = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, '-');

        // extract day
        int day = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, 'T');

        // extract hours, minutes, seconds and milliseconds
        int hour = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int minutes = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int seconds = parseInt(date, offset += 1, offset += 2);
        // milliseconds can be optional in the format
        // always use 0 otherwise returned date will include millis of current time
        int milliseconds = 0;

        if (date.charAt(offset) == '.') {
            checkOffset(date, offset, '.');
            int digitCount = 1;
            while (offset + digitCount < date.length() && digitCount < 3
                    && date.charAt(offset + 1 + digitCount) != 'Z'
                    && date.charAt(offset + 1 + digitCount) != '+'
                    && date.charAt(offset + 1 + digitCount) != '-') {
                digitCount++;
            }
            String msString = date.substring(offset += 1, offset += digitCount);
            while (msString.length() < 3) {
                msString += '0';
            }
            milliseconds = parseInt(msString, 0, 3);
        }

        // extract timezone
        String timezoneId = null;
        while (offset < date.length()) {
            char timezoneIndicator = date.charAt(offset);
            if (timezoneIndicator == '+' || timezoneIndicator == '-') {
                timezoneId = GMT_ID + date.substring(offset);
                break;
            } else if (timezoneIndicator == 'Z') {
                timezoneId = GMT_ID;
                break;
            }
            offset++;
        }
        if (timezoneId == null) {
            throw new IndexOutOfBoundsException("Invalid time zone indicator ");
        }
        TimeZone timezone = TimeZone.getTimeZone(timezoneId);
        if (!timezone.getID().equals(timezoneId)) {
            throw new IndexOutOfBoundsException();
        }

        Calendar calendar = new GregorianCalendar(timezone);
        calendar.setLenient(false);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);

        return calendar.getTime();
    } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    }
}

From source file:de.csdev.ebus.command.datatypes.ext.EBusTypeDate.java

@Override
public EBusDateTime decodeInt(byte[] data) throws EBusTypeException {

    if (data == null) {
        // TODO replace value
        return null;
    }//from w w  w  .j  a v a2 s.  c o m

    IEBusType<BigDecimal> bcdType = types.getType(EBusTypeBCD.TYPE_BCD);
    IEBusType<BigDecimal> wordType = types.getType(EBusTypeWord.TYPE_WORD);
    IEBusType<BigDecimal> charType = types.getType(EBusTypeChar.TYPE_CHAR);

    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    BigDecimal day = null;
    BigDecimal month = null;
    BigDecimal year = null;

    if (data.length != getTypeLength()) {
        throw new EBusTypeException(
                String.format("Input byte array must have a length of %d bytes!", getTypeLength()));
    }

    if (StringUtils.equals(variant, SHORT)) {
        day = bcdType.decode(new byte[] { data[0] });
        month = bcdType.decode(new byte[] { data[1] });
        year = bcdType.decode(new byte[] { data[2] });

    } else if (StringUtils.equals(variant, DEFAULT)) {
        day = bcdType.decode(new byte[] { data[0] });
        month = bcdType.decode(new byte[] { data[1] });
        year = bcdType.decode(new byte[] { data[3] });

    } else if (StringUtils.equals(variant, HEX_SHORT)) {
        day = charType.decode(new byte[] { data[0] });
        month = charType.decode(new byte[] { data[1] });
        year = charType.decode(new byte[] { data[2] });

    } else if (StringUtils.equals(variant, HEX)) {
        day = charType.decode(new byte[] { data[0] });
        month = charType.decode(new byte[] { data[1] });
        year = charType.decode(new byte[] { data[3] });

    } else if (StringUtils.equals(variant, DAYS)) {
        BigDecimal daysSince1900 = wordType.decode(data);
        calendar.set(1900, 0, 1, 0, 0);
        calendar.add(Calendar.DAY_OF_YEAR, daysSince1900.intValue());
    }

    if (day != null && month != null && year != null) {
        if (day.intValue() < 1 || day.intValue() > 31) {
            throw new EBusTypeException("A valid day must be in a range between 1-31 !");
        }
        if (month.intValue() < 1 || month.intValue() > 12) {
            throw new EBusTypeException("A valid day must be in a range between 1-12 !");
        }
    }

    if (year != null) {
        if (year.intValue() < 70) {
            year = year.add(new BigDecimal(2000));
        } else {
            year = year.add(new BigDecimal(1900));
        }
        calendar.set(Calendar.YEAR, year.intValue());
    }

    if (month != null) {
        calendar.set(Calendar.MONTH, month.intValue() - 1);
    }

    if (day != null && day.intValue() > 0 && day.intValue() < 32) {
        calendar.set(Calendar.DAY_OF_MONTH, day.intValue());
    }

    return new EBusDateTime(calendar, false, true);
}

From source file:be.fedict.eid.idp.model.applet.SecureCardReaderServiceBean.java

@Override
public String getTransactionMessage() {
    Boolean transactionMessageSigning = this.configuration.getValue(ConfigProperty.TRANSACTION_MESSAGE_SIGNING,
            Boolean.class);
    if (null == transactionMessageSigning) {
        return null;
    }//ww  w .  j av  a2 s  . co m
    if (Boolean.FALSE.equals(transactionMessageSigning)) {
        return null;
    }
    RPEntity relyingPartyEntity = AppletUtil.getSessionAttribute(Constants.RP_SESSION_ATTRIBUTE);
    String applicationName;
    if (null != relyingPartyEntity) {
        applicationName = relyingPartyEntity.getName();
    } else {
        applicationName = AppletUtil.getSessionAttribute(Constants.RP_DOMAIN_SESSION_ATTRIBUTE);
    }
    Calendar calendar = Calendar.getInstance();
    String transactionMessage = applicationName + " @ " + calendar.get(Calendar.DAY_OF_MONTH) + "/"
            + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.YEAR) + " "
            + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":"
            + calendar.get(Calendar.SECOND);
    LOG.debug("transaction message: " + transactionMessage);
    return transactionMessage;
}

From source file:com.evolveum.midpoint.repo.sql.CleanupTest.java

License:asdf

private Calendar create_2013_07_12_12_00_Calendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2013);
    calendar.set(Calendar.MONTH, Calendar.MAY);
    calendar.set(Calendar.DAY_OF_MONTH, 7);
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return calendar;
}

From source file:org.jasig.portlet.ClassifiedsPortlet.service.AdGroomer.java

@Override
public void run() {
    Date now;//from   w w w  . j  a v a2  s  . c o m
    Calendar nowCal = new GregorianCalendar();
    long lastCheckTime = System.currentTimeMillis();
    boolean firstCheck = true;

    while (bRun) {
        now = new Date();
        nowCal.setTime(now);

        if (nowCal.get(Calendar.HOUR_OF_DAY) == hourToCheck
                && nowCal.get(Calendar.MINUTE) <= (minuteToCheck + 1)
                && (firstCheck || System.currentTimeMillis() > (lastCheckTime + maxCheckIntervalMillis))) {

            log.info("Ad groomer to delete expired ads at " + now.toString());
            adService.deleteExpiredAds();
            lastCheckTime = System.currentTimeMillis();
            firstCheck = false;
        }
        try {
            log.debug("Ad groomer Waiting to see if we should check the time...");
            sleep(checkInterval * 1000);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:com.silverpeas.calendar.DateTimeTest.java

@Test
public void createsAtASpecifiedDateTime() {
    Calendar now = getInstance();
    DateTime expected = new DateTime(now.getTime());
    DateTime actual = DateTime.dateTimeAt(now.get((Calendar.YEAR)), now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE),
            now.get(Calendar.SECOND), now.get(Calendar.MILLISECOND));
    assertEquals(expected.getTime(), actual.getTime(), 100);
}