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:io.github.tavernaextras.biocatalogue.model.Util.java

/**
 * Calculates time difference between two {@link Calendar} instances.
 * /*w ww .  j  a  v a2s.  co  m*/
 * @param earlier The "earlier" date.
 * @param later The "later" date.
 * @param maxDifferenceMillis The maximum allowed time difference between the two
 *                            {@link Calendar} instances, in milliseconds. If the calculated
 *                            difference will exceed <code>maxDifferenceMillis</code>,
 *                            <code>null</code> will be returned. If this parameter has
 *                            a value <code>less or equal to zero</code>, any time difference
 *                            between the {@link Calendar} instances will be permitted.
 * @return String in the form "XX seconds|minutes|hours|days ago". Proper pluralisation will
 *         be performed on the name of the time unit. <code>null</code> will be returned in
 *         cases where one of the {@link Calendar} instances is <code>null</code>, time
 *         difference between the provided instances is greater than <code>maxDifferenceMillis</code>
 *         or <code>earlier</code> date is not really earlier than <code>later</code> one.
 */
public static String getAgoString(Calendar earlier, Calendar later, long maxDifferenceMillis) {
    // one of the dates is missing
    if (earlier == null || later == null) {
        return null;
    }

    if (earlier.before(later)) {
        long differenceMillis = later.getTimeInMillis() - earlier.getTimeInMillis();

        if (maxDifferenceMillis <= 0 || (maxDifferenceMillis > 0 && differenceMillis <= maxDifferenceMillis)) {
            long result = 0;
            String unitName = "";

            if (differenceMillis < 60 * 1000) {
                result = differenceMillis / 1000;
                unitName = "second";
            } else if (differenceMillis < 60 * 60 * 1000) {
                result = differenceMillis / (60 * 1000);
                unitName = "minute";
            } else if (differenceMillis < 24 * 60 * 60 * 1000) {
                result = differenceMillis / (60 * 60 * 1000);
                unitName = "hour";
            } else {
                result = differenceMillis / (24 * 60 * 60 * 1000);
                unitName = "day";
            }

            return (result + " " + Util.pluraliseNoun(unitName, result, true) + " ago");
        } else {
            // the difference is too large - larger than the supplied threshold
            return null;
        }
    } else {
        // the "later" date is not really later than the "earlier" one
        return null;
    }
}

From source file:arc.noaa.weather.activities.MainActivity.java

public static long saveLastUpdateTime(SharedPreferences sp) {
    Calendar now = Calendar.getInstance();
    sp.edit().putLong("lastUpdate", now.getTimeInMillis()).apply();
    return now.getTimeInMillis();
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the ending date of the month of the
 * specified date and TimeZone. If TimeZone is null, meaning use default
 * TimeZone of the JVM./*from   www. j a  v  a 2  s .c o  m*/
 */
final public static Date endOfYear(Date when, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();
    final Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(when.getTime()); // don't call cal.setTime(Date) which
                                         // will reset the TimeZone.

    final int year = cal.get(Calendar.YEAR);
    cal.clear();
    cal.set(year + 1, Calendar.JANUARY, 1);
    cal.setTimeInMillis(cal.getTimeInMillis() - 1);
    return cal.getTime();
}

From source file:com.espertech.esper.epl.datetime.reformatop.ReformatOpToMillisec.java

public Object evaluate(Calendar cal) {
    return cal.getTimeInMillis();
}

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

private static long getTimeinMillis4FullDateTimeUTC(int year, int month, int day, int hours, int minutes,
        int seconds) {
    Calendar c = Calendar.getInstance();
    System.out.println("current: " + c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    if (z.inDaylightTime(new Date())) {
        offset = offset + z.getDSTSavings();
    }/*from  www.j a va 2  s  .  c  o m*/
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs: " + offsetHrs);
    System.out.println("offsetMin: " + offsetMins);

    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);

    c.set(Calendar.HOUR_OF_DAY, (hours - offsetHrs));
    c.set(Calendar.MINUTE, (minutes - offsetMins));
    c.set(Calendar.SECOND, seconds);

    System.out.println("GMT Time: " + c.getTime());

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

    return timeinmillis;
}

From source file:adalid.commons.util.TimeUtils.java

public static Date newDate(int year, int monthOfYear, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);/*w ww  . ja  va 2 s  .  com*/
    c.set(Calendar.MONTH, monthOfYear - 1);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return new Date(c.getTimeInMillis());
}

From source file:com.rsltc.profiledata.main.MainActivity.java

public static DataReadRequest queryFitnessData() {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    Date now = new Date();
    cal.setTime(now);/*from  www  .  java 2  s.c  om*/
    //        cal.add(Calendar.YEAR, -1);
    //        cal.set(2015,7,20);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.YEAR, -1);

    long startTime = cal.getTimeInMillis();

    java.text.DateFormat dateFormat = getDateInstance();
    Log.i(TAG, "Range Start: " + dateFormat.format(startTime));
    Log.i(TAG, "Range End: " + dateFormat.format(endTime));

    DataReadRequest readRequest = new DataReadRequest.Builder()

            .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)

            //                .read(DataType.TYPE_WEIGHT)
            //                .read(DataType.TYPE_HEIGHT)
            //                .read(DataType.TYPE_HEART_RATE_BPM)
            .bucketByActivitySegment(5, TimeUnit.MINUTES)
            //                .bucketByActivityType(1, TimeUnit.SECONDS)
            //                .bucketByTime(1, TimeUnit.DAYS)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();

    return readRequest;
}

From source file:adalid.commons.util.TimeUtils.java

public static Time newTime(int hourOfDay, int minuteOfHour, int secondOfMinute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, 1970);/*  w ww  . j  a v  a 2s .c  om*/
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DAY_OF_MONTH, 1);
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minuteOfHour);
    c.set(Calendar.SECOND, secondOfMinute);
    c.set(Calendar.MILLISECOND, 0);
    return new Time(c.getTimeInMillis());
}

From source file:adalid.commons.util.TimeUtils.java

public static Timestamp newTimestamp(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour,
        int secondOfMinute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);//from ww  w.j  a v a  2 s  .  com
    c.set(Calendar.MONTH, monthOfYear - 1);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minuteOfHour);
    c.set(Calendar.SECOND, secondOfMinute);
    c.set(Calendar.MILLISECOND, 0);
    return new Timestamp(c.getTimeInMillis());
}

From source file:Dates.java

/**
 * Given a date, a proper TimeZone, return the last millisecond date of the
 * specified date and TimeZone. If TimeZone is null, meaning use Defult
 * TimeZone of the JVM./*  ww  w  . j a  va2 s . c  o m*/
 */
final public static Date endOfDate(Date when, TimeZone tz) {
    if (tz == null)
        tz = TimeZones.getCurrent();

    final Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(when.getTime());// don't call cal.setTime(Date) which
                                        // will reset the TimeZone.
    final int day = cal.get(Calendar.DAY_OF_MONTH);
    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH);
    cal.clear();
    cal.set(year, month, day + 1);
    cal.setTimeInMillis(cal.getTimeInMillis() - 1);

    return cal.getTime();
}