Example usage for java.util GregorianCalendar get

List of usage examples for java.util GregorianCalendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:org.squale.welcom.outils.DateUtil.java

/**
 * @param date date en entre//from  w w  w. j a  va 2  s  .  c  o  m
 * @return retourne le jour de la semaine correspondant  la date en entre
 */
public static int getDay(final java.util.Date date) {
    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);

    return gc.get(Calendar.DAY_OF_WEEK);
}

From source file:oscar.util.DateUtils.java

/**
 * This will take 2 date objects, presumably one that holds the date and the other that holds the time
 * and it will merge the two into one object that has both the date and the time. This method is not 
 * normally useful but our database seems to have a lot of split date/time objects.
 * If either parameters are null it will return null.
 * This method will materialise the result before returning.
 *///  w w w.  j  a va 2  s.com
public static GregorianCalendar toGregorianCalendar(Date date, Date time) {
    if (date == null || time == null)
        return (null);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);

    GregorianCalendar cal2 = new GregorianCalendar();
    cal2.setTime(time);

    cal.set(GregorianCalendar.HOUR_OF_DAY, cal2.get(GregorianCalendar.HOUR_OF_DAY));
    cal.set(GregorianCalendar.MINUTE, cal2.get(GregorianCalendar.MINUTE));
    cal.set(GregorianCalendar.SECOND, cal2.get(GregorianCalendar.SECOND));
    cal.set(GregorianCalendar.MILLISECOND, cal2.get(GregorianCalendar.MILLISECOND));
    cal.getTime();

    return (cal);
}

From source file:org.squale.welcom.outils.DateUtil.java

/**
 * retourne le porchain jour de la semaine saisi en paramtre par rapport  la date d'entre
 * /* w ww.j av  a2 s.com*/
 * @param date date d'entre
 * @param dayofweek Jour de la semaine  utiliser
 * @return la date du prochain jour de la semaine
 */
public static java.util.Date getNext(final java.util.Date date, final int dayofweek) {
    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);

    final int actualday = gc.get(Calendar.DAY_OF_WEEK);
    gc.add(Calendar.DATE, ((dayofweek - actualday + WEEK_DAYS) % WEEK_DAYS));

    return gc.getTime();
}

From source file:org.squale.welcom.outils.DateUtil.java

/**
 * Retourn l'anne en int d'une date/*from   w w  w .j  av a2 s. com*/
 * 
 * @param gc gregorien calendar
 * @param date date
 * @return l'anne en int d'une date
 */
private static int getYearOfDate(GregorianCalendar gc, final Date date) {

    // recupere l'anne de debut
    gc.setTime(date);
    return gc.get(Calendar.YEAR);
}

From source file:org.squale.welcom.outils.DateUtil.java

/**
 * @param day = jour  tester au format Date
 * @return Retourne si la date passe en entre correspond  un jour feri
 *///from w w w .  j  a va2  s . com
public static boolean isPublicHolyday(final Date day) {
    if (day == null) {
        return false;
    }

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(day);

    final ArrayList ph = getPublicHolydays(gc.get(Calendar.YEAR));

    for (final Iterator iter = ph.iterator(); iter.hasNext();) {
        final Date d = (Date) iter.next();

        if (d.equals(day)) {
            return true;
        }
    }

    return false;
}

From source file:org.apache.hadoop.hive.ql.QueryPlan.java

public static String makeQueryId() {
    GregorianCalendar gc = new GregorianCalendar();
    String userid = System.getProperty("user.name");

    return userid + "_"
            + String.format("%1$4d%2$02d%3$02d%4$02d%5$02d%6$02d", gc.get(Calendar.YEAR),
                    gc.get(Calendar.MONTH) + 1, gc.get(Calendar.DAY_OF_MONTH), gc.get(Calendar.HOUR_OF_DAY),
                    gc.get(Calendar.MINUTE), gc.get(Calendar.SECOND))
            + "_" + UUID.randomUUID().toString();
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * Method returns date object with normalized date:
 * - when pattern doesn't contain year pattern -> current year is used
 * - when pattern doesn't contain month pattern
 *    date represents current year -> current month is used
 *                        else -> january is used
 * - when pattern doesn't contain day pattern
 *    date represents current year and month -> current day is used
 *                                 else -> first day of month is used
 *///from  w w  w .  j  a  v  a 2 s .  co m
private static Date normalizeDate(Calendar nowCld, Date date, Locale locale, String pattern) {
    GregorianCalendar dateCld = new GregorianCalendar();
    dateCld.setTime(date);
    //set current date
    if (pattern.toLowerCase(locale).indexOf('y') == -1) {
        dateCld.set(Calendar.YEAR, nowCld.get(Calendar.YEAR));
    }

    if (pattern.indexOf('M') == -1) {
        if (nowCld.get(Calendar.YEAR) == dateCld.get(Calendar.YEAR)) {
            dateCld.set(Calendar.MONTH, nowCld.get(Calendar.MONTH));
        } else {
            dateCld.set(Calendar.MONTH, 0);
        }
    }
    if (pattern.toLowerCase(locale).indexOf('d') == -1) {
        if (nowCld.get(Calendar.YEAR) != dateCld.get(Calendar.YEAR)
                || nowCld.get(Calendar.MONTH) != dateCld.get(Calendar.MONTH)) {
            dateCld.set(Calendar.DAY_OF_MONTH, 1);
        } else {
            dateCld.set(Calendar.DAY_OF_MONTH, nowCld.get(Calendar.DAY_OF_MONTH));
        }
    }
    return dateCld.getTime();
}

From source file:org.mifos.calendar.CalendarUtils.java

public static DateTime getNextDayForMonthUsingWeekRankAndWeekday(final DateTime startDate,
        final int weekOfMonth, final int dayOfWeek, final int every) {

    /*/*from  w  w  w .  j  av  a 2s  . c om*/
     * In Joda time MONDAY=1 and SUNDAY=7, so shift these to SUNDAY=1, SATURDAY=7 to match this class
     */
    int calendarDayOfWeek = (dayOfWeek % 7) + 1;

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());

    DateTime scheduleDate;

    if (!RankOfDay.getRankOfDay(weekOfMonth).equals(RankOfDay.LAST)) {
        // apply month recurrence
        gc.add(GregorianCalendar.MONTH, every);
        gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, weekOfMonth);
        scheduleDate = new DateTime(gc.getTime().getTime());
    } else {// weekCount=-1
        gc.set(GregorianCalendar.DATE, 15);
        gc.add(GregorianCalendar.MONTH, every);
        gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
        int M1 = gc.get(GregorianCalendar.MONTH);
        // assumption: there are 5 weekdays in the month
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 5);
        int M2 = gc.get(GregorianCalendar.MONTH);
        // if assumption fails, it means there exists 4 weekdays in a
        // month, return last weekday date
        // if M1==M2, means there exists 5 weekdays otherwise 4 weekdays
        // in a month
        if (M1 != M2) {
            gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
            gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 4);
        }
        scheduleDate = new DateTime(gc.getTime().getTime());
    }

    return scheduleDate;
}

From source file:org.squale.welcom.outils.DateUtil.java

/**
 * @param dateDebut : date de dbut (java.util.Date)
 * @param dateFin : date de fin (java.util.Date)
 * @param unit : unit de temps dans laquelle doit tre calcule la dure (MILLI_SEC,SEC,DAY,HOUR,MIN)
 * @return retourne le nombre de jour entre deux dates les samedi et dimanche n'tant pas inclu
 *///from w w  w.j  a  v  a2s.  c  om
public static long durationNoWeekEnd(final java.util.Date dateDebut, final java.util.Date dateFin,
        final long unit) {
    long duration = duration(dateDebut, dateFin, unit);
    final GregorianCalendar gc = new GregorianCalendar();

    if (duration != 0) {
        gc.setTime(dateDebut);

        while (gc.getTime().getTime() <= dateFin.getTime()) {
            if ((gc.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
                    || (gc.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
                duration--;
            }

            gc.add(Calendar.DATE, 1);
        }
    }

    return duration;
}

From source file:org.jamwiki.parser.image.ImageUtil.java

/**
 * Given a filename, generate the URL to use to store the file on the filesystem.
 *///from   w w  w .  j  ava 2s  . co m
public static String generateFileUrl(String virtualWiki, String filename, Date date) throws WikiException {
    if (StringUtils.isBlank(virtualWiki)) {
        throw new WikiException(new WikiMessage("common.exception.novirtualwiki"));
    }
    String url = filename;
    if (StringUtils.isBlank(url)) {
        throw new WikiException(new WikiMessage("upload.error.filename"));
    }
    // file is appended with a timestamp of DDHHMMSS
    GregorianCalendar cal = new GregorianCalendar();
    if (date != null) {
        cal.setTime(date);
    }
    String day = Integer.toString(cal.get(Calendar.DAY_OF_MONTH));
    if (day.length() == 1) {
        day = "0" + day;
    }
    String hour = Integer.toString(cal.get(Calendar.HOUR_OF_DAY));
    if (hour.length() == 1) {
        hour = "0" + hour;
    }
    String minute = Integer.toString(cal.get(Calendar.MINUTE));
    if (minute.length() == 1) {
        minute = "0" + minute;
    }
    String second = Integer.toString(cal.get(Calendar.SECOND));
    if (second.length() == 1) {
        second = "0" + second;
    }
    String suffix = "-" + day + hour + minute + second;
    int pos = url.lastIndexOf('.');
    url = (pos == -1) ? url + suffix : url.substring(0, pos) + suffix + url.substring(pos);
    // now pre-pend the file system directory
    // subdirectory is composed of vwiki/year/month
    String year = Integer.toString(cal.get(Calendar.YEAR));
    String month = Integer.toString(cal.get(Calendar.MONTH) + 1);
    String subdirectory = "/" + virtualWiki + "/" + year + "/" + month;
    if (isImagesOnFS()) {
        File directory = ImageUtil.buildAbsoluteFile(subdirectory);
        if (!directory.exists() && !directory.mkdirs()) {
            throw new WikiException(
                    new WikiMessage("upload.error.directorycreate", directory.getAbsolutePath()));
        }
    }
    return subdirectory + "/" + url;
}