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

public static int diff(Date date1, Date date2) {
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();

    c1.setTime(date1);
    c2.setTime(date2);//from w w w.ja  va  2  s .co  m
    int diffDay = 0;

    if (c1.before(c2)) {
        diffDay = countDiffDay(c1, c2);
    } else {
        diffDay = countDiffDay(c2, c1);
    }

    return diffDay;
}

From source file:Main.java

/**
 * Returns the day of the year (IE 365 = Dec 31st)
 * @param date This is the date to use to calculate the day of the year
 * @return Day - Day of the year based on date.
 *//*from  www  .  j  a va2 s. co m*/
public static Integer getCurrentDay(Date date) {
    Calendar cal = Calendar.getInstance();
    if (date != null)
        cal.setTime(date);
    return cal.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
@SuppressWarnings("unchecked")
public static <T> T convert(String string, Class<T> type) {
    try {/*from  w w w  . ja v a2 s.  c o m*/
        if (string == null || string.trim().length() == 0) {
            return null;
        }
        if (type.getName().equals("java.util.Date")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            return (T) dateFormat.parse(string);

        }
        if (type.getName().equals("java.sql.Date")) {
            return (T) new java.sql.Date(java.sql.Date.parse(string));
        }
        if (type.getSimpleName().equals("Calendar")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateFormat.parse(string));
            return (T) calendar;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date getDifferenceDaysDate(long times, int diffDay) {
    Calendar now = Calendar.getInstance();
    Date d = new Date();
    d.setTime(times);//from w  ww .  j av a2s .  c o m
    now.setTime(d);
    now.set(Calendar.DATE, now.get(Calendar.DATE) + diffDay);
    return now.getTime();
}

From source file:com.mediaportal.ampdroid.api.JsonUtils.java

public static BasicNameValuePair newPair(String _name, Date _value) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(_value);
    int offset = (int) ((cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 60000);

    cal.add(Calendar.MINUTE, offset);
    String dateString = IsoDate.dateToString(cal.getTime(), IsoDate.DATE_TIME);

    return new BasicNameValuePair(_name, dateString);
}

From source file:Main.java

/**
 * Get the age of the user. Takes in their birthday and calculates it according to today's date
 * @param birthday Date Object/*from  w w  w .jav  a2 s  .co  m*/
 * @return Returns an int of their age (IE 20, 55, 18). If the date is in the future, it will
 *         return -1 instead.
 */
public static int getAge(Date birthday) {

    Calendar now = Calendar.getInstance();
    Calendar dob = Calendar.getInstance();
    dob.setTime(birthday);

    //First check for in the future:
    if (dob.after(now)) {
        return -1;
    }

    int year1 = now.get(Calendar.YEAR);
    int year2 = dob.get(Calendar.YEAR);

    int age = year1 - year2;

    int month1 = now.get(Calendar.MONTH);
    int month2 = dob.get(Calendar.MONTH);

    if (month2 > month1) {
        age--;

    } else if (month1 == month2) {
        int day1 = now.get(Calendar.DAY_OF_MONTH);
        int day2 = dob.get(Calendar.DAY_OF_MONTH);
        if (day2 > day1) {
            age--;
        }
    }

    return age;
}

From source file:Main.java

public static Calendar parseDateString(String dateString, String pattern) throws ParseException {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    c.setTime(sdf.parse(dateString));// all done
    return c;//from   w  w w  .j  a v  a 2  s  . c  o  m
}

From source file:Main.java

public static String getDateTimeString(Date dateTime) {
    Calendar cal = Calendar.getInstance();
    if (dateTime != null) {
        cal.setTime(dateTime);
    }/* w  w w .  j a  v  a  2 s  .c  o m*/
    return DatatypeConverter.printDateTime(cal);
}

From source file:Main.java

public static boolean isSameDay(long timestamp1, long timestamp2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();

    cal1.setTime(new Date(timestamp1));
    cal2.setTime(new Date(timestamp2));

    boolean sameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
    boolean sameDay = cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);

    return sameYear && sameDay;
}

From source file:Main.java

/**
 * @param startDateTime//from   www . j a  v a  2  s . co m
 * @param endDateTime
 * @return the difference in hours between the second timestamp and first
 */
public static double getDifferenceInHours(Timestamp startDateTime, Timestamp endDateTime) {
    int difference = 0;

    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTime(startDateTime);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(endDateTime);

    // First, get difference in whole days
    Calendar startCompare = Calendar.getInstance();
    startCompare.setTime(startDateTime);
    startCompare.set(Calendar.HOUR_OF_DAY, 0);
    startCompare.set(Calendar.MINUTE, 0);

    Calendar endCompare = Calendar.getInstance();
    endCompare.setTime(endDateTime);
    endCompare.set(Calendar.HOUR_OF_DAY, 0);
    endCompare.set(Calendar.MINUTE, 0);

    return (endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (60.0000 * 60.0000 * 1000.0000);
}