Example usage for java.util Calendar DAY_OF_MONTH

List of usage examples for java.util Calendar DAY_OF_MONTH

Introduction

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

Prototype

int DAY_OF_MONTH

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

Click Source Link

Document

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

Usage

From source file:com.worldline.easycukes.rest.utils.DateHelper.java

/**
 * Used to convert date value in json date format
 *
 * @param value/* w  w  w .  j  ava  2s.  co m*/
 * @return
 */
public static String convertDateToJsonFormat(@NonNull final Date value) {
    log.info("setting the date value " + value + " to format json");
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(value);
    return calendar.get(Calendar.YEAR) + "-" + formatTo2Digit(calendar.get(Calendar.MONTH) + 1) + "-"
            + formatTo2Digit(calendar.get(Calendar.DAY_OF_MONTH)) + "T"
            + formatTo2Digit(calendar.get(Calendar.HOUR_OF_DAY)) + ":"
            + formatTo2Digit(calendar.get(Calendar.MINUTE)) + ":"
            + formatTo2Digit(calendar.get(Calendar.SECOND)) + ":" + calendar.get(Calendar.MILLISECOND) + "Z";
}

From source file:Main.java

/**
 * Given a calendar (possibly containing only a day of the year), returns the earliest possible
 * anniversary of the date that is equal to or after the current point in time if the date
 * does not contain a year, or the date converted to the local time zone (if the date contains
 * a year./*from  w  w  w .  j a  v  a 2 s.  com*/
 *
 * @param target The date we wish to convert(in the UTC time zone).
 * @return If date does not contain a year (year < 1900), returns the next earliest anniversary
 * that is after the current point in time (in the local time zone). Otherwise, returns the
 * adjusted Date in the local time zone.
 */
public static Date getNextAnnualDate(Calendar target) {
    final Calendar today = Calendar.getInstance();
    today.setTime(new Date());

    // Round the current time to the exact start of today so that when we compare
    // today against the target date, both dates are set to exactly 0000H.
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    final boolean isYearSet = isYearSet(target);
    final int targetYear = target.get(Calendar.YEAR);
    final int targetMonth = target.get(Calendar.MONTH);
    final int targetDay = target.get(Calendar.DAY_OF_MONTH);
    final boolean isFeb29 = (targetMonth == Calendar.FEBRUARY && targetDay == 29);
    final GregorianCalendar anniversary = new GregorianCalendar();
    // Convert from the UTC date to the local date. Set the year to today's year if the
    // there is no provided year (targetYear < 1900)
    anniversary.set(!isYearSet ? today.get(Calendar.YEAR) : targetYear, targetMonth, targetDay);
    // If the anniversary's date is before the start of today and there is no year set,
    // increment the year by 1 so that the returned date is always equal to or greater than
    // today. If the day is a leap year, keep going until we get the next leap year anniversary
    // Otherwise if there is already a year set, simply return the exact date.
    if (!isYearSet) {
        int anniversaryYear = today.get(Calendar.YEAR);
        if (anniversary.before(today) || (isFeb29 && !anniversary.isLeapYear(anniversaryYear))) {
            // If the target date is not Feb 29, then set the anniversary to the next year.
            // Otherwise, keep going until we find the next leap year (this is not guaranteed
            // to be in 4 years time).
            do {
                anniversaryYear += 1;
            } while (isFeb29 && !anniversary.isLeapYear(anniversaryYear));
            anniversary.set(anniversaryYear, targetMonth, targetDay);
        }
    }
    return anniversary.getTime();
}

From source file:ezbake.services.provenance.graph.Utils.java

public static ezbake.base.thrift.DateTime convertDate2DateTime(final java.util.Date theDate) {
    final Calendar cal = new GregorianCalendar();
    cal.setTime(theDate);/* www . j  av  a2  s  .  c o  m*/

    // get calendar parts
    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH) + 1;
    final int day = cal.get(Calendar.DAY_OF_MONTH);
    final int hour = cal.get(Calendar.HOUR_OF_DAY);
    final int minute = cal.get(Calendar.MINUTE);
    final int second = cal.get(Calendar.SECOND);
    int offsetMinutes = (cal.getTimeZone().getOffset(cal.getTimeInMillis())) / (1000 * 60);

    // set thrift DateTime propertiesd
    final ezbake.base.thrift.DateTime dt = new ezbake.base.thrift.DateTime();
    // Date
    final ezbake.base.thrift.Date date = new ezbake.base.thrift.Date();
    date.setMonth((short) month).setDay((short) day).setYear((short) year);
    dt.setDate(date);

    // Time with TimeZone
    final ezbake.base.thrift.Time t = new ezbake.base.thrift.Time();
    boolean afterUtc = offsetMinutes > 0;
    offsetMinutes = Math.abs(offsetMinutes);
    final ezbake.base.thrift.TimeZone tz = new ezbake.base.thrift.TimeZone((short) (offsetMinutes / 60),
            (short) (offsetMinutes % 60), afterUtc);
    t.setHour((short) hour).setMinute((short) minute).setSecond((short) second).setTz(tz);
    dt.setTime(t);

    return dt;
}

From source file:datojava.jcalendar.DJFechasEspInv.java

@Override
public boolean isSpecial(Date date) {
    calendar.setTime(date);/*from  w w w  . jav a  2  s .c o  m*/
    for (i = 0; i < djjcalendar.getFechasOcupadas().size(); i++) {
        if (calendar.get(Calendar.MONTH) == djjcalendar.getFechasOcupadas().get(i).get(Calendar.MONTH)
                && calendar.get(Calendar.DAY_OF_MONTH) == djjcalendar.getFechasOcupadas().get(i)
                        .get(Calendar.DAY_OF_MONTH)) {
            return true;
        }
    }
    return false;
}

From source file:com.u2apple.tool.dao.AndroidDeviceDaoImpl.java

@Override
public List<AndroidDeviceRanking> getUnidentifiedDevices(int days) throws IOException, JSchException {
    SqlSession sqlSession = MyBatisHelper.getStatSqlSessionFactory().openSession();
    AndroidDeviceMapper mapper = sqlSession.getMapper(AndroidDeviceMapper.class);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -days);
    return mapper.selectUnidentifiedDevices(SqlUtils.getMonthlyTable("log_device_init"), calendar.getTime());
}

From source file:DateParser.java

private static Calendar getCalendar(String isodate) {
    // YYYY-MM-DDThh:mm:ss.sTZD
    StringTokenizer st = new StringTokenizer(isodate, "-T:.+Z", true);

    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.clear();/*from w  w w. ja  v a2s .com*/
    try {
        // Year
        if (st.hasMoreTokens()) {
            int year = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.YEAR, year);
        } else {
            return calendar;
        }
        // Month
        if (check(st, "-") && (st.hasMoreTokens())) {
            int month = Integer.parseInt(st.nextToken()) - 1;
            calendar.set(Calendar.MONTH, month);
        } else {
            return calendar;
        }
        // Day
        if (check(st, "-") && (st.hasMoreTokens())) {
            int day = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.DAY_OF_MONTH, day);
        } else {
            return calendar;
        }
        // Hour
        if (check(st, "T") && (st.hasMoreTokens())) {
            int hour = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.HOUR_OF_DAY, hour);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }
        // Minutes
        if (check(st, ":") && (st.hasMoreTokens())) {
            int minutes = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.MINUTE, minutes);
        } else {
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }

        //
        // Not mandatory now
        //

        // Secondes
        if (!st.hasMoreTokens()) {
            return calendar;
        }
        String tok = st.nextToken();
        if (tok.equals(":")) { // secondes
            if (st.hasMoreTokens()) {
                int secondes = Integer.parseInt(st.nextToken());
                calendar.set(Calendar.SECOND, secondes);
                if (!st.hasMoreTokens()) {
                    return calendar;
                }
                // frac sec
                tok = st.nextToken();
                if (tok.equals(".")) {
                    // bug fixed, thx to Martin Bottcher
                    String nt = st.nextToken();
                    while (nt.length() < 3) {
                        nt += "0";
                    }
                    nt = nt.substring(0, 3); // Cut trailing chars..
                    int millisec = Integer.parseInt(nt);
                    // int millisec = Integer.parseInt(st.nextToken()) * 10;
                    calendar.set(Calendar.MILLISECOND, millisec);
                    if (!st.hasMoreTokens()) {
                        return calendar;
                    }
                    tok = st.nextToken();
                } else {
                    calendar.set(Calendar.MILLISECOND, 0);
                }
            } else {
                throw new RuntimeException("No secondes specified");
            }
        } else {
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        // Timezone
        if (!tok.equals("Z")) { // UTC
            if (!(tok.equals("+") || tok.equals("-"))) {
                throw new RuntimeException("only Z, + or - allowed");
            }
            boolean plus = tok.equals("+");
            if (!st.hasMoreTokens()) {
                throw new RuntimeException("Missing hour field");
            }
            int tzhour = Integer.parseInt(st.nextToken());
            int tzmin = 0;
            if (check(st, ":") && (st.hasMoreTokens())) {
                tzmin = Integer.parseInt(st.nextToken());
            } else {
                throw new RuntimeException("Missing minute field");
            }
            if (plus) {
                calendar.add(Calendar.HOUR, -tzhour);
                calendar.add(Calendar.MINUTE, -tzmin);
            } else {
                calendar.add(Calendar.HOUR, tzhour);
                calendar.add(Calendar.MINUTE, tzmin);
            }
        }
    } catch (NumberFormatException ex) {
        throw new RuntimeException("[" + ex.getMessage() + "] is not an integer");
    }
    return calendar;
}

From source file:Gestores.GestorUsuarios.java

public void registraUsuario(Restaurante restaurante) {
    Calendar cal = new GregorianCalendar();
    String fecha = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
            + cal.get(Calendar.DAY_OF_MONTH);
    try {/*from   ww  w .  j  a  v  a 2 s. c  o m*/
        String query = "INSERT INTO restaurante (nombre, direccion, telefono, username, passwd, web, email, fecha) "
                + "VALUES (?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(query,
                new Object[] { restaurante.getNombre(), restaurante.getDireccion(), restaurante.getTelefono(),
                        restaurante.getUsername(), gHash.md5(restaurante.getPassword()), restaurante.getWeb(),
                        restaurante.getEmail(), fecha });
    } catch (Exception e) {
        General.log("GestorUsuarios", "ERROR en registraUsuario: " + e.getMessage());
    }
}

From source file:com.pureinfo.force.util.TimerUtil.java

private static Calendar getTimeToday(Calendar _today, String _sTime) {
    Calendar todayTime = new GregorianCalendar();
    try {/*from   w  w w  . ja va 2s  .c  om*/
        todayTime.setTime(TIME_FORMAT.parse(_sTime));
    } catch (ParseException ex) {
        throw new PureRuntimeException(PureException.INVALID_REQUEST, "time must be HH:mm TIME_FORMAT", ex);
    }
    todayTime.set(Calendar.YEAR, _today.get(Calendar.YEAR));
    todayTime.set(Calendar.MONTH, _today.get(Calendar.MONTH));
    todayTime.set(Calendar.DAY_OF_MONTH, _today.get(Calendar.DAY_OF_MONTH));
    return todayTime;
}

From source file:sf.wicketlearningapplication.server.TestUtility.java

public static Bug createNewBugInstance() {
    final int estimatedHours = random.nextInt(8) + 1;
    final Severity severity = Severity.values()[(int) (Math.random() * Severity.values().length)];
    final Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, random.nextInt(30));
    ///*ww w  .  j a  va  2s .  c om*/
    final Bug bug = new Bug();
    bug.setSummary(text());
    bug.setSeverity(severity);
    bug.setEstimatedHours(estimatedHours);
    bug.setDueByDate(calendar.getTime());
    return bug;
}

From source file:com.qpark.eip.core.spring.auth.LimitedAccessDataProvider.java

/**
 * Get a {@link Date}, where hours, minutes, seconds and milliseconds are
 * set to 0.//from ww w . ja  v  a  2s  . c  o  m
 *
 * @return the {@link Date} and the corresponding log string.
 */
private static SimpleEntry<Date, String> getRequestDate() {
    Calendar gc = new GregorianCalendar();
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    String hmss = String.format("%04d%02d%02d", gc.get(Calendar.YEAR), gc.get(Calendar.MONTH) + 1,
            gc.get(Calendar.DAY_OF_MONTH));
    SimpleEntry<Date, String> entry = new SimpleEntry<Date, String>(gc.getTime(), hmss);
    return entry;
}