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

/**
 * Gets the number of hours in between two given dates
 *
 * @param calendar1 the first date//from  ww w  .ja  v  a  2 s . c  o  m
 * @param calendar2 the second date
 * @return the number hours
 */
public static int getHoursDifference(Calendar calendar1, Calendar calendar2) {
    if (calendar1 == null || calendar2 == null) {
        return 0;
    }

    return (int) ((calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60));
}

From source file:Main.java

/**
 * Gets the number of days in between two given dates
 *
 * @param calendar1 the first date//from   w  w w . j a  v a 2  s  .c om
 * @param calendar2 the second date
 * @return the number days
 */
public static int getDaysDifference(Calendar calendar1, Calendar calendar2) {
    if (calendar1 == null || calendar2 == null) {
        return 0;
    }

    return (int) ((calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}

From source file:Main.java

public static long getEndOfCurrentMonth() {
    Calendar calendar = getCurrentCalendar();
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    return calendar.getTimeInMillis();
}

From source file:Main.java

public static boolean isToday(Calendar time) {
    long now = getSystemTime();
    int[] nowFields = getTimeFields(now);
    int[] timeFields = getTimeFields(time.getTimeInMillis());
    return nowFields[0] == timeFields[0] && nowFields[1] == timeFields[1] && nowFields[2] == timeFields[2];
}

From source file:com.mycompany.app.VariousTest.java

public static void timeDifference() {
    Calendar t1 = new GregorianCalendar(2014, 06, 05, 6, 0);
    Calendar t2 = new GregorianCalendar(2014, 06, 05, 8, 59);

    int minutes = minutesDiff(new Date(t1.getTimeInMillis()), new Date(t2.getTimeInMillis()));
    System.out.println("Minutes diff: " + minutes);
}

From source file:Main.java

public static long[] getEntryExit(Double id, Calendar date, Connection con, PreparedStatement stmt)
        throws Exception {
    stmt.setDate(1, new java.sql.Date(date.getTimeInMillis()));
    // stmt.setDate(2, new java.sql.Date(date.getTimeInMillis()+1000000));
    stmt.execute();// www.  j ava 2s. c om
    ResultSet rs = stmt.getResultSet();
    if (rs != null) {

        if (rs.next()) {
            Timestamp d1 = rs.getTimestamp(1);
            Timestamp d2 = rs.getTimestamp(2);
            if (d1 != null && d2 != null) {
                System.out.println(id + ":" + new SimpleDateFormat("dd/MM/yyyy").format(date.getTime()) + ":"
                        + d1.toString());
                long[] res = new long[] { d1.getTime(), d2.getTime() };
                return res;
            }
        }
        rs.close();
    }
    return null;
}

From source file:Main.java

public static String[] getCalendarShowTime(String paramString) {
    try {// w  w  w.  j a va2s . com
        long l = Long.valueOf(paramString);
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTimeInMillis(1000L * l);
        return getCalendarShowTime(localCalendar.getTimeInMillis());
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.pureinfo.force.util.TimerUtil.java

/**
 * @param _sTime/*  w w  w.ja  v a2  s .com*/
 *            must be "HH:mm" format
 */
public static Timer scheduleFrom(String _sTime, long _lPeriod, TimerTask _task) {
    Calendar nowTime = new GregorianCalendar();
    Calendar firstTime = getTimeToday(nowTime, _sTime);
    firstTime.setTimeInMillis(getNext(firstTime.getTimeInMillis(), nowTime.getTimeInMillis(), _lPeriod));
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(_task, firstTime.getTime(), _lPeriod);
    return timer;
}

From source file:Main.java

public static String getCurrentDateTime() {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
    String currentDateandTime = sdf.format(new Date(c.getTimeInMillis()));
    return currentDateandTime;
}

From source file:Main.java

/**
 * This method is used to generate random number between given range.
 *///  w  w  w .j a  v  a2 s . c o m
public static long getRandomNumberBetween() {
    Calendar rightNow = Calendar.getInstance();
    long offset = rightNow.get(Calendar.ZONE_OFFSET) + rightNow.get(Calendar.DST_OFFSET);
    long sinceMidnight = (rightNow.getTimeInMillis() + offset) % (24 * 60 * 60 * 1000);
    return sinceMidnight;
}