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:Main.java

/**
 * Get passed time from given date/*from   ww w.  j  a v  a2s  .com*/
 *
 * @param date
 * @return
 */
public static String getTimeString(Date date) {
    String strTime = "";
    Calendar thatDay = Calendar.getInstance();
    thatDay.setTime(date);

    Calendar today = Calendar.getInstance();

    long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

    long second = diff / 1000;
    int min = (int) second / 60;
    int hour = min / 60;
    int day = hour / 24;
    int month = day / 30;
    int year = month / 12;

    if (min < 60) {
        strTime = String.format("%d min", min);
    } else if (min >= 60 && min < 60 * 24) {
        if (hour < 24) {
            strTime = String.format("%d hour", hour);
        }
    } else if (day < 31) {
        strTime = String.format("%d day", day);
    } else if (month < 12) {
        strTime = String.format("%d month", month);
    } else {
        strTime = String.format("%d year", year);
    }

    return strTime;
}

From source file:Main.java

/**
 * Calculate the Julian Day for a given date using the following formula:
 * JD = 367 * Y - INT(7 * (Y + INT((M + 9)/12))/4) + INT(275 * M / 9)
 *      + D + 1721013.5 + UT/24//  www  .  j  a  v  a  2 s .  c om
 *
 * Note that this is only valid for the year range 1900 - 2099.
 */
public static double calculateJulianDay(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);

    double hour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0f
            + cal.get(Calendar.SECOND) / 3600.0f;

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);

    double jd = 367.0 * year - Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0)
            + Math.floor(275.0 * month / 9.0) + day + 1721013.5 + hour / 24.0;
    return jd;
}

From source file:Main.java

public static int getDayOfWeek(Date date) {
    Calendar calendar = Calendar.getInstance();
    if (date == null)
        date = new Date();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

public static Calendar getCalendar(final Date dateAndTime) {
    if (null == dateAndTime) {
        return null;
    }// w w w .  j  av  a 2  s. c  o m
    Calendar cal = (Calendar) Calendar.getInstance(UTC).clone();
    cal.setTime(dateAndTime);
    return cal;
}

From source file:Utils.java

public static Date getEndOfDay(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);
    cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:Main.java

/**
 * Convert a BPMN variable value to a XSL parameter value
 * /*from  ww  w.jav a  2  s  .  co  m*/
 * @param bpmnVariableValue
 *            A BPMN variable value
 * @return The XSL parameter value
 */
public static String convertBpmnVariableValueToXslParam(final Object bpmnVariableValue) {

    final String xslParamValue;
    if (bpmnVariableValue != null) {
        if (bpmnVariableValue instanceof Date) {
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime((Date) bpmnVariableValue);
            xslParamValue = DatatypeConverter.printDateTime(calendar);
        } else {
            xslParamValue = bpmnVariableValue.toString();
        }
    } else {
        xslParamValue = "";
    }

    return xslParamValue;
}

From source file:Main.java

public static boolean sameDay(Date then, Date now) {

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(then);
    cal2.setTime(now);//from   w  ww.j a  va  2s.  c o m

    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);

}

From source file:Main.java

private static final Calendar getUtcDate(Date date, boolean noYear) {
    final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE, Locale.US);
    calendar.setTime(date);
    if (noYear) {
        calendar.set(Calendar.YEAR, 0);
    }/*  w  w w .  java 2  s.  co m*/
    return calendar;
}

From source file:Main.java

/**
 * Copy the hour and minutes from a date to another
 * @param dateOrigin Date origin where to get the data
 * @param dateDestiny Date destiny where to set the data
 * @return Date with hour and minutes from dateOrigin and rest from dateDestiny
 *///from   w w  w . ja v  a  2s.  c om
public static Date copyHourMinute(Date dateOrigin, Date dateDestiny) {

    //check null values
    if (dateOrigin == null && dateDestiny == null) {
        return Calendar.getInstance().getTime();
    } else if (dateOrigin == null) {
        return dateDestiny;
    } else if (dateDestiny == null) {
        return dateOrigin;
    }

    //convert to calendars
    Calendar calOrigin = Calendar.getInstance();
    calOrigin.setTime(dateOrigin);
    Calendar calDestiny = Calendar.getInstance();
    calDestiny.setTime(dateDestiny);

    //return the time of destiny
    return copyHourMinute(calOrigin, calDestiny).getTime();
}

From source file:Main.java

public static boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);//from   w  w w. j  av a 2  s.c  om
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);

    return sameDay;
}