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 boolean isSameDay(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
        throw new IllegalArgumentException("The date must not be null");
    }/*from  w w  w.j a v  a2 s .  co  m*/
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isSameDay(cal1, cal2);
}

From source file:Main.java

/**
 * Packs java time value into an MS-DOS time value.
 * @param time the time value// w  w w .  j  av  a 2  s. c  o m
 * @return the MS-DOS packed time
 */
public static int packTime(long time) {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date(time));

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

    /*
     * Here is how MS-DOS packs a time value:
     * 0-4: seconds (divided by 2 because we only have 5 bits = 32 different numbers)
     * 5-10: minutes (6 bits = 64 possible values)
     * 11-15: hours (5 bits = 32 possible values)
     *
     * source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247(v=vs.85).aspx
     */
    return (hours << 11) & (minutes << 5) & (seconds / 2);
}

From source file:Main.java

/**
 * Set when the last update has been performed
 * @param context/*from w  w w .  j  a  va 2 s .c om*/
 */
public static void setLastSave(Context context) {
    SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
    Editor editor = prefs.edit();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    int current_hours = calendar.get(Calendar.HOUR_OF_DAY);
    editor.putString(KEY_LAST_SAVE, current_hours > 13 ? "PM" : "AM");
    editor.commit();
}

From source file:Main.java

/**
 * Convert date into milliseconds.//from  w  w  w . ja va  2 s.c  o  m
 * 
 * @param crisisDate
 * @return
 */
public static long getMiliseconds(String crisisDate) {
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        Date date = (Date) formatter.parse(crisisDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTimeInMillis();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static String getDaysBetween(String date1, String date2) {
    // input is expected to be exactly like; 2011-01-05
    // date2 must be before date1
    String result = "";

    try {//from ww w  .  ja v  a2  s .c  o m

        Date dateOne = DateUtils.parseDate(date1, new String[] { "yyyy-MM-dd" });
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(dateOne);

        Date dateTwo = DateUtils.parseDate(date2, new String[] { "yyyy-MM-dd" });
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(dateTwo);

        long diff = dateOne.getTime() - dateTwo.getTime();
        Log.d(TAG, "days in between:" + (TimeUnit.MILLISECONDS.toSeconds(diff) / 60 / 60 / 24));
    } catch (Exception ex) {
        Log.w(TAG, ex.toString());
    }

    return result;
}

From source file:Main.java

public static boolean isLeapYear(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int year = cal.get(Calendar.YEAR);
    return isLeapYear(year);
}

From source file:Util.java

public static byte[] packDate(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    // Byte bits: 00000000 11111111 22222222 33333333 44444444
    // Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS
    byte[] bytes = new byte[5];
    int s = c.get(Calendar.SECOND);
    int m = c.get(Calendar.MINUTE);
    int h = c.get(Calendar.HOUR_OF_DAY);
    int d = c.get(Calendar.DATE);
    int mm = c.get(Calendar.MONTH) + 1;
    int y = c.get(Calendar.YEAR);

    bytes[4] = (byte) ((m << 6) | s);
    bytes[3] = (byte) ((m >> 2) | (h << 4));
    bytes[2] = (byte) ((h >> 4) | (d << 1) | (mm << 6));
    bytes[1] = (byte) ((mm >> 2) | (y << 2));
    bytes[0] = (byte) (y >> 6);

    return bytes;
}

From source file:Main.java

/**
 * Returns Date object of the next day of d.
 * @return// w ww . j a  va 2 s .c  o  m
 */
public static Date getNextDay(Date d) {
    Calendar c = Calendar.getInstance();
    if (d != null)
        c.setTime(d);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    c.add(Calendar.DAY_OF_MONTH, 1);
    return c.getTime();
}

From source file:Main.java

private static long betweenTime(int what, String t1, String t2, String... patterns) {
    if (patterns.length == 0) {
        throw new RuntimeException("must give me a pattern");
    }/*from   w ww.  ja v  a 2s.  c o m*/
    Date d1 = getDate(t1, patterns[0]);
    Date d2 = getDate(t2, patterns.length > 1 ? patterns[1] : patterns[0]);
    if (d1 != null && d2 != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        int i1 = calendar.get(what);
        calendar.setTime(d2);
        int i2 = calendar.get(what);
        return i1 - i2;
    }
    return Long.MIN_VALUE;
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.Duration.java

public static double duration(Date d1, Date d2, String step) throws PrefixAndSuffixException {
    if (StringUtils.isEmpty(step) || step.equals("day")) {
        return (d1.getTime() - d2.getTime()) / (1000L * 3600 * 24);
    } else if (step.equals("month")) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);//w w w.jav a 2 s . c  o  m
        int month = (c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12
                + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
        return month;
    } else {
        throw new PrefixAndSuffixException("??");
    }
}