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

public static Calendar stringToCalendar(String date, String format) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    Calendar cal = GregorianCalendar.getInstance();
    try {//from  ww w.  j  ava2 s  .c om
        cal.setTime(dateFormat.parse(date));
    } catch (ParseException e) {
        cal = null;
    }

    return cal;
}

From source file:Main.java

/**
 * Determines the Date "date" adjusted by "days".
 * A negative value will return a date in the past.
 * @param date The date to be adjusted//from   w ww. j  a  va 2  s.  c  o  m
 * @param days The number of days to adjust by
 * @return Returns a date object
 */
public static Date getDateAdjustedByDays(Date date, int days) {
    Calendar cal = Calendar.getInstance();
    if (date != null)
        cal.setTime(date);
    cal.add(Calendar.DAY_OF_YEAR, days);
    return cal.getTime();
}

From source file:Main.java

/**
 * Returns date before given days//w  w w . jav  a  2  s  . co m
 *
 * @param days days to before
 * @return string date string before days
 */
public static String getDateBefore(int days) {
    Date today = new Date();
    Calendar cal = new GregorianCalendar();
    cal.setTime(today);
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    Date date = cal.getTime();
    SimpleDateFormat gmtFormat = new SimpleDateFormat();
    gmtFormat.applyPattern("yyyy-MM-dd 00:00:00");
    TimeZone gmtTime = TimeZone.getTimeZone("GMT");
    gmtFormat.setTimeZone(gmtTime);
    return gmtFormat.format(date);
}

From source file:Main.java

/**
 *Returns now//from   w  w  w  .j av a 2 s. c  o m
 * @return Calendar object
 * */
public static Calendar getTimeNow() {
    Calendar now = Calendar.getInstance();
    now.setTime(new Date());
    return now;
}

From source file:Main.java

public static int getWeekOfYear(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.WEEK_OF_YEAR);
}

From source file:Main.java

public static int getWeekOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.WEEK_OF_MONTH);
}

From source file:Main.java

/**
 * This method returns a new Date with a specified hour, minute, second and millis
 *
 * @param date//from  w w  w .  j  a v  a 2  s  .c  o m
 *         Start date
 * @param hourOfDay
 *         Target hour (0 - 23)
 * @param minute
 *         Target minute (0 - 59)
 * @param second
 *         Target second (0 - 59)
 * @param millis
 *         Target millis (0 - 999)
 *
 * @return The requested time
 */
public static Date setTime(Date date, int hourOfDay, int minute, int second, int millis) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, second);
    cal.set(Calendar.MILLISECOND, millis);
    return cal.getTime();
}

From source file:Main.java

public static int getWeekIndex(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.DAY_OF_WEEK);
}

From source file:com.googlecode.commons.swing.util.DateUtils2.java

public static int getWeekNumber(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

/**
 * @param date/*w  w w  .  j  av a  2s  .co  m*/
 * @return
 */
public static String formatToYesterdayOrToday(Date date) {
    Calendar today = Calendar.getInstance();
    Calendar yesterday = Calendar.getInstance();
    yesterday.add(Calendar.DATE, -1);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");

    if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
        return "Today, " + timeFormatter.format(date);
    } else if (calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR)) {
        return "Yesterday, " + timeFormatter.format(date);
    } else {
        return DateFormat.format("MMM dd, ", date).toString() + timeFormatter.format(date);

    }
}