Example usage for java.util Calendar getTimeInMillis

List of usage examples for java.util Calendar getTimeInMillis

Introduction

In this page you can find the example usage for java.util Calendar getTimeInMillis.

Prototype

public long getTimeInMillis() 

Source Link

Document

Returns this Calendar's time value in milliseconds.

Usage

From source file:Main.java

public static String findDateDifference(Long timestamp) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm a");
    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.setTimeInMillis(timestamp);
    calendar2.setTimeInMillis(System.currentTimeMillis());
    try {/*from  ww w  . j a  va 2 s .c o  m*/

        //in milliseconds
        long diff = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        long diffYears = diffDays / 365;
        //            System.out.print(diffDays + " days, ");
        //            System.out.print(diffHours + " hours, ");
        //            System.out.print(diffMinutes + " minutes, ");
        //            System.out.print(diffSeconds + " seconds.");
        //            Log.e("difference is", diffDays + " days, " + diffHours + " hours, " + diffMinutes + " minutes, ");
        if (diffDays == 0) {
            if (diffHours > 0)
                return diffHours + "HOURS AGO";
            else if (diffMinutes > 0) {
                return diffMinutes + " MINS AGO";
                //return timeFormat.format(calendar1.getTime());
            } else
                return "Just Now";
        } else if (diffDays == 1) {
            return "Yesterday";
        } else if (diffDays <= 30) {
            return "" + diffDays + " DAYS AGO";
        } //else
          //return format.format(calendar1.getTime());

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static long getDayInUTC(long time) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);//w  ww. j  a v a  2s . c  o m
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    return cal.getTimeInMillis();
}

From source file:es.tekniker.framework.ktek.util.Utils.java

private static long getTimeinMillisUTC() {
    Calendar c = getCalendarUTC();
    System.out.println("current: " + c.getTime());

    long timeinmillis = c.getTimeInMillis();
    System.out.println(" System time in millis " + timeinmillis);

    return timeinmillis;
}

From source file:Main.java

public static Date firstTimeOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:helper.util.DateHelper.java

public static CalendarRange getRangeHoursBeforeNow(TimeZone tz, int hours) {
    Calendar start = Calendar.getInstance(tz);
    Calendar end = Calendar.getInstance(tz);
    start.setTimeInMillis(end.getTimeInMillis());
    start.add(Calendar.HOUR_OF_DAY, -hours);
    //fix for BST range issue on check (first dock issue). 
    // BST is not observed so jst roll forward here)
    // also allows for normal variations in timestamp (will still include last one if +n minutes)
    end.add(Calendar.HOUR_OF_DAY, 24);
    return new CalendarRange(start, end);
}

From source file:com.adaptris.core.LogHandlerTest.java

public static List<Long> createLogFiles(File dir, String prefix, int count) throws Exception {
    ensureDirectory(dir);//from   w  ww  . j  av a2 s  . c  o m
    ArrayList<Long> ages = new ArrayList<Long>();
    // Shoudld give us files ranging from 9 days in the past to 1hour in the
    // past.
    for (int i = count - 1; i >= 0; i--) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 0 - i);
        c.add(Calendar.MINUTE, -1);
        ages.add(c.getTimeInMillis());
    }
    for (Long age : ages) {
        File f = File.createTempFile(prefix, null, dir);
        f.setLastModified(age);
    }
    return ages;
}

From source file:net.reichholf.dreamdroid.helpers.enigma2.Timer.java

/**
 * @return//ww w .j  a v  a  2  s . c o  m
 */
public static ExtendedHashMap getInitialTimer() {
    ExtendedHashMap timer = new ExtendedHashMap();
    timer.put(KEY_DESCRIPTION, "");
    timer.put(KEY_LOCATION, "/hdd/movie/");
    timer.put(KEY_DISABLED, "0"); // enabled
    timer.put(KEY_JUST_PLAY, "0"); // record
    timer.put(KEY_AFTER_EVENT, Afterevents.AUTO.toString()); // auto
    timer.put(KEY_REPEATED, "0"); // One-Time-Event

    // Calculate values for begin and end
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(new Date());

    long s = (cal.getTimeInMillis() / 1000);
    long e = s + 3600;

    String begin = Long.toString(s);
    String end = Long.toString(e);

    timer.put(KEY_BEGIN, begin);
    timer.put(KEY_END, end);

    return timer;
}

From source file:Main.java

/**
 * Returns milliseconds of the date argument dt. If the argument
 * isIncludeTime is false then the returned milliseconds does not include
 * time./*from   www.j  ava2  s. c om*/
 * 
 * @param dt
 * @param isIncludeTime
 * @return
 */
private static long getDate(Date dt, boolean isIncludeTime) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(dt);
    if (!isIncludeTime) {
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
    return cal.getTimeInMillis();
}

From source file:gov.wa.wsdot.cms.utils.Migration.java

/**
 * Calculate number of days since the 1900 epoch.
 * //  w w  w .  ja v  a 2s.co  m
 * @param daysSinceEpoch days since epoch in milliseconds
 * @return converted date in the form "Tue Apr 09 07:56:37 PDT 2013"
 */
public static String convertDays(double daysSinceEpoch) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // Set TimeZone to UTC
    Calendar startDate = Calendar.getInstance();
    startDate.set(1899, 11, 31, 16, 0, 0);
    long MILLIS_PER_DAY = 86400000;
    long createdWhen = (long) (daysSinceEpoch * MILLIS_PER_DAY + startDate.getTimeInMillis())
            - 2 * MILLIS_PER_DAY;

    return dateFormat.format(new Date(createdWhen));
}

From source file:Main.java

/**
 * @param startDateTime/*from   ww w .ja v a  2 s  .c om*/
 * @param endDateTime
 * @return the difference in days between the second timestamp and first
 */
public static double getDifferenceInDays(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);
    startCompare.set(Calendar.SECOND, 0);
    startCompare.set(Calendar.MILLISECOND, 0);

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

    return (endCompare.getTimeInMillis() - startCompare.getTimeInMillis()) / (24 * 60 * 60 * 1000);
}