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 long formatToLong(String time, String template) {
    SimpleDateFormat sdf = new SimpleDateFormat(template, Locale.CHINA);
    try {//from  w  ww.ja  va2s  .  com
        Date d = sdf.parse(time);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        long l = c.getTimeInMillis();
        return l;
    } catch (ParseException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

private static String getTimestamp() {
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

    long secondsSince = calendar.getTimeInMillis() / 1000L;

    return "" + secondsSince;
}

From source file:Main.java

public static Long createDate(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance();

    calendar.set(year, month, day);/*from w w w  .  j  av  a2  s. co m*/

    return calendar.getTimeInMillis();

}

From source file:Main.java

public static int daysBetween(Date smdate, Date bdate) throws Exception {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    smdate = sdf.parse(sdf.format(smdate));
    bdate = sdf.parse(sdf.format(bdate));
    Calendar cal = Calendar.getInstance();
    cal.setTime(smdate);//w  w  w .j a  v  a2 s  . co  m
    long time1 = cal.getTimeInMillis();
    cal.setTime(bdate);
    long time2 = cal.getTimeInMillis();
    long between_days = (time2 - time1) / (1000 * 3600 * 24);

    return Integer.parseInt(String.valueOf(between_days));
}

From source file:Main.java

private static long currentTime() {
    Calendar time = Calendar.getInstance();
    time.add(Calendar.MILLISECOND, -time.getTimeZone().getOffset(time.getTimeInMillis()));
    return time.getTime().getTime();
}

From source file:Main.java

public static long getStartOfCurrentYear() {
    Calendar calendar = getCurrentCalendar();
    calendar.set(Calendar.MONTH, 0);
    return calendar.getTimeInMillis();
}

From source file:Main.java

private static void setAlarm(AlarmManager alarmManager, Calendar calendar, PendingIntent pendingIntent) {
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            pendingIntent);//from w  w  w  .  jav a 2s. c  om
}

From source file:Main.java

/**
 * Convert a date/time into milliseconds.
 *
 * @param date/*  www.j  a  va 2 s . co m*/
 * @return
 */
public static long dateToMilliseconds(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static long getMinTimeStampForIgMedia() {
    Calendar rightNow = Calendar.getInstance();
    rightNow.add(Calendar.DAY_OF_YEAR, -2);

    return rightNow.getTimeInMillis() / 1000;
}

From source file:Main.java

public static String getShortDuration(Calendar startDate, Calendar endDate) {
    long startTime = startDate.getTimeInMillis();
    long endTime = endDate.getTimeInMillis();
    long milliseconds = endTime - startTime;

    int days = (int) (milliseconds / (1000 * 60 * 60 * 24));
    int hours = (int) (milliseconds / (1000 * 60 * 60));
    int min = (int) (milliseconds - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours)) / (1000 * 60);

    if (hours < 1) {
        return min + " min";
    } else {//  w ww . j  a va  2s .  c o m
        StringBuilder durationText = new StringBuilder();
        if (hours == 1) {
            durationText.append(hours + " hr");
        } else {
            durationText.append(hours + " hrs");
        }

        if (min > 30) {
            durationText.append(" " + min + " min");
        }

        return durationText.toString();
    }

}