Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:Util.java

public static Date getMonday(Date today) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(today);

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    while (dow != Calendar.MONDAY) {
        int date = cal.get(Calendar.DATE);

        if (date == 1) {
            int month = cal.get(Calendar.MONTH);

            if (month == Calendar.JANUARY) {
                month = Calendar.DECEMBER;

                cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
            } else {
                month--;/*from  w w w . j a  va  2s .c o  m*/
            }

            cal.set(Calendar.MONTH, month);

            date = getMonthLastDate(month, cal.get(Calendar.YEAR));
        } else {
            date--;
        }

        cal.set(Calendar.DATE, date);

        dow = cal.get(Calendar.DAY_OF_WEEK);
    }

    return cal.getTime();
}

From source file:Main.java

/**
 * Returns a ISO 8601 representation of the given date. This method 
 * is thread safe and non-blocking.//from w  w  w  .j av a2  s .  co  m
 *
 * @see <a href="https://issues.apache.org/jira/browse/TIKA-495">TIKA-495</a>
 * @param date given date
 * @return ISO 8601 date string, including timezone details
 */
public static String formatDate(Date date) {
    Calendar calendar = GregorianCalendar.getInstance(UTC, Locale.US);
    calendar.setTime(date);
    return doFormatDate(calendar);
}

From source file:eionet.gdem.qa.WQCleanerJob.java

/**
 * Check the job's age and return true if it is possible to delete it.
 *
 * @param job// w  w w  .  j  ava 2 s  .  c o  m
 *            Workqueue job object
 * @return true if job can be deleted.
 */
public static boolean canDeleteJob(WorkqueueJob job) {
    boolean canDelete = false;
    if (job != null && job.getJobTimestamp() != null && job.getStatus() >= Constants.XQ_READY) {
        Calendar now = Calendar.getInstance();
        int maxAge = Properties.wqJobMaxAge == 0 ? -1 : -Properties.wqJobMaxAge;
        now.add(Calendar.HOUR, maxAge);
        Calendar jobCal = Calendar.getInstance();
        jobCal.setTime(job.getJobTimestamp());
        if (now.after(jobCal)) {
            canDelete = true;
        }
    }
    return canDelete;
}

From source file:Main.java

public static String getTimeString(Date fromdate) {

    long then;//from   w w w.  j av  a  2  s. c  o m
    then = fromdate.getTime();
    Date date = new Date(then);

    StringBuffer dateStr = new StringBuffer();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    Calendar now = Calendar.getInstance();

    int days = daysBetween(calendar.getTime(), now.getTime());
    int minutes = hoursBetween(calendar.getTime(), now.getTime());
    int hours = minutes / 60;
    if (days == 0) {

        int second = minuteBetween(calendar.getTime(), now.getTime());
        if (minutes > 60) {

            if (hours >= 1 && hours <= 24) {
                dateStr.append(hours).append("h");
            }

        } else {

            if (second <= 10) {
                dateStr.append("Now");
            } else if (second > 10 && second <= 30) {
                dateStr.append("few seconds ago");
            } else if (second > 30 && second <= 60) {
                dateStr.append(second).append("s");
            } else if (second >= 60 && minutes <= 60) {
                dateStr.append(minutes).append("m");
            }
        }
    } else

    if (hours > 24 && days <= 7) {
        dateStr.append(days).append("d");
    } else {
        dateStr.append(DateUtils.getRelativeTimeSpanString(date.getTime(), System.currentTimeMillis(),
                DateUtils.SECOND_IN_MILLIS));
    }

    return dateStr.toString();
}

From source file:net.ceos.project.poi.annotated.bean.PropagationVerticalObjectBuilder.java

/**
 * Validate the PropagationVerticalObject based on the object build with the method
 * 'buildPropagationVerticalObject'/*w  ww .  j  a  v  a 2 s .  c o m*/
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validatePropagationVerticalObject(PropagationVerticalObject toValidate) {
    PropagationVerticalObject base = buildPropagationVerticalObject();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    assertEquals(base.getIntegerPrimitiveAttribute(), toValidate.getIntegerPrimitiveAttribute());
    assertEquals(base.getDoublePrimitiveAttribute(), toValidate.getDoublePrimitiveAttribute());
    assertEquals(base.getLongPrimitiveAttribute(), toValidate.getLongPrimitiveAttribute());
    assertEquals(base.isBooleanPrimitiveAttribute(), toValidate.isBooleanPrimitiveAttribute());
    assertEquals(base.getFloatPrimitiveAttribute(), toValidate.getFloatPrimitiveAttribute());
    // TODO add new validation below
}

From source file:Main.java

public static String convertUtc2LocalPro(String utcTime) {
    if (TextUtils.isEmpty(utcTime)) {
        return "";
    }//from  ww w .j a  v a2  s .  co m
    // 2014-10-24T02:58:05.932Z
    SimpleDateFormat utcFormatter, localFormatter;
    utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
    Date date;
    Calendar cal = Calendar.getInstance();
    try {
        date = utcFormatter.parse(utcTime);
        cal.setTime(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    cal.add(Calendar.HOUR_OF_DAY, +8);
    date = cal.getTime();
    localFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    return localFormatter.format(date);
}

From source file:com.jennifer.ui.util.TimeUtil.java

public static Date add(Date d, String type, int interval) {
    Calendar c = Calendar.getInstance();
    c.setTime(d);

    if (Time.YEARS.equals(type)) {
        c.add(Calendar.YEAR, interval);
    } else if (Time.MONTHS.equals(type)) {
        c.add(Calendar.MONTH, interval);
    } else if (Time.DAYS.equals(type)) {
        c.add(Calendar.DATE, interval);
    } else if (Time.HOURS.equals(type)) {
        c.add(Calendar.HOUR_OF_DAY, interval);
    } else if (Time.MINUTES.equals(type)) {
        c.add(Calendar.MINUTE, interval);
    } else if (Time.SECONDS.equals(type)) {
        c.add(Calendar.SECOND, interval);
    } else if (Time.MILLISECONDS.equals(type)) {
        c.add(Calendar.MILLISECOND, interval);
    } else if (Time.WEEKS.equals(type)) {
        c.add(Calendar.DATE, interval * 7);
    }/*from  ww w .  j  a va 2  s . com*/

    return c.getTime();
}

From source file:DateUtils.java

/**
 * Converts an instance of {@link java.util.Date} object to {@link java.util.Calendar}.
 *
 * @param date/*from w ww  .j  av a2s .  c  om*/
 *            date object to convert.
 * @return Converted <code>Calendar</code> object.
 */
public static Calendar toCalendar(final Date date) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
}

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

private static Calendar getTimeToday(Calendar _today, String _sTime) {
    Calendar todayTime = new GregorianCalendar();
    try {//from   w  ww  .  j  av a  2 s  .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:com.wanikani.wklib.UserInformation.java

private static Calendar getNormalizedCalendar(Date date) {
    Calendar ans;

    ans = Calendar.getInstance();
    ans.setTime(date);
    ans.set(Calendar.HOUR, 1);/*from ww w . j ava  2 s. c  o m*/
    ans.set(Calendar.MINUTE, 2);
    ans.set(Calendar.SECOND, 3);
    ans.set(Calendar.MILLISECOND, 4);

    return ans;
}