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 String formatAsMySQLDatetime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DATE);

    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minutes = calendar.get(Calendar.MINUTE);
    int seconds = calendar.get(Calendar.SECOND);

    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}

From source file:Main.java

public static String format(Date date) {
    SimpleDateFormat myFmt1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss EEE");
    String[] days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    String[] daysFull = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    int month = calendar.get(Calendar.MONTH);
    return String
            .valueOf(days[day - 1] + " " + myFmt1.format(date) + " " + months[month] + " " + daysFull[day - 1]);

}

From source file:Main.java

public static Date getDateMinuteBefore(Date originalDate, int minutes) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(originalDate);
    cal.add(Calendar.MINUTE, minutes * -1);
    return cal.getTime();
}

From source file:Main.java

public static Date getDateDayBefore(Date originalDate, int days) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(originalDate);
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    return cal.getTime();
}

From source file:Main.java

public static boolean isFuture(long date) {

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(new Date(date));

    Calendar todayCalendar = Calendar.getInstance();
    todayCalendar.setTime(new Date(System.currentTimeMillis()));

    return todayCalendar.get(Calendar.DATE) > compareCalendar.get(Calendar.DATE);
}

From source file:Main.java

public static boolean isYesterday(long date) {

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(new Date(date));

    Calendar todayCalendar = Calendar.getInstance();
    todayCalendar.setTime(new Date(System.currentTimeMillis()));

    return todayCalendar.get(Calendar.DATE) < compareCalendar.get(Calendar.DATE);
}

From source file:Main.java

public static Calendar getCalendar(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
}

From source file:Main.java

static Calendar toNextWholeMinute(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());
    c.add(Calendar.MINUTE, 1);/*  ww w  .j a  v  a2  s.co m*/
    c.set(Calendar.SECOND, 0);
    return c;
}

From source file:Main.java

/**
 * Return the difference between 2 dates (dayTwo - dayOne) in days
 * @param dayTwo The first day to compare against
 * @param dayOne The second day to compare against
 * @return an int of the difference between the 2 days
 *///from  w w  w .j  a  va2  s .c  o m
public static int diffDays(Date dayTwo, Date dayOne) {
    Calendar calOne = Calendar.getInstance();
    calOne.setTime(dayOne);
    long msOne = calOne.getTimeInMillis();

    Calendar calTwo = Calendar.getInstance();
    calTwo.setTime(dayTwo);
    long msTwo = calTwo.getTimeInMillis();

    return (int) ((msTwo - msOne) / (1000L * 60L * 60L * 24L));
}

From source file:Util.java

/**
 * Utility for setting the time on a date.
 * @param date//from  w w w . j  a va 2 s.c o m
 * @param h
 * @param m
 * @param s
 * @return the date with the time set
 */
public static Date setTime(Date date, int h, int m, int s) {
    if (date == null)
        return null;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, h);
    calendar.set(Calendar.MINUTE, m);
    calendar.set(Calendar.SECOND, s);
    return calendar.getTime();
}