Example usage for java.util Date getTime

List of usage examples for java.util Date getTime

Introduction

In this page you can find the example usage for java.util Date getTime.

Prototype

public long getTime() 

Source Link

Document

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

Usage

From source file:com.liusoft.dlog4j.util.DateUtils.java

/**
 * /*from ww  w . j a v a  2  s.c o m*/
 * @param d1
 * @param d2
 * @return
 */
public static int diff_in_date(Date d1, Date d2) {
    return (int) (d1.getTime() - d2.getTime()) / 86400000;
}

From source file:Main.java

public static int getDiffHour(String startTime, String endTime) {
    long diff = 0;
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {//from www  .j  a  va  2s .  c om
        Date startDate = ft.parse(startTime);
        Date endDate = ft.parse(endTime);
        diff = startDate.getTime() - endDate.getTime();
        diff = diff / (1000 * 60 * 60);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return new Long(diff).intValue();
}

From source file:Main.java

/**
 * converts time (in milliseconds) to human-readable format
 * "<w> days, <x> hours, <y> minutes and (z) seconds"
 *///from w w w .  j  a v  a 2 s  .  c o  m
public static String getDisplayableDateTime(Date date) {
    long timeStamp = date.getTime();

    long duration = (new Date()).getTime() - timeStamp;
    duration = (duration < 0) ? 0 : duration;

    StringBuffer res = new StringBuffer();

    long temp;

    if (duration >= ONE_SECOND) {
        if (duration >= ONE_DAY) {
            return DATE_FORMATTER.format(new Date(timeStamp));
        } else {
            temp = duration / ONE_HOUR;
            if (temp > 0) {
                duration -= temp * ONE_HOUR;
                res.append(temp).append(" hr").append(temp > 1 ? "s" : "")
                        .append(duration >= ONE_MINUTE ? ", " : "");
            }

            temp = duration / ONE_MINUTE;
            if (temp > 0) {
                duration -= temp * ONE_MINUTE;
                res.append(temp).append(" min").append(temp > 1 ? "s" : "");
            }

            if (!res.toString().equals("") && duration >= ONE_SECOND) {
                res.append(" and ");
            }

            temp = duration / ONE_SECOND;
            if (temp > 0) {
                res.append(temp).append(" sec").append(temp > 1 ? "s" : "");
            }
            return res.toString();
        }

    } else {
        return "0 second";
    }
}

From source file:Main.java

public static long getDiff(String startTime, String endTime) {
    long diff = 0;
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {/* w  ww.j a  va 2 s. com*/
        Date startDate = ft.parse(startTime);
        Date endDate = ft.parse(endTime);
        diff = startDate.getTime() - endDate.getTime();
        diff = diff / 1000;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return diff;
}

From source file:Main.java

public static long changeToTimeStamp(String time) {

    long rand = 0;
    try {/*from   w w  w.ja  v a2  s .c  o m*/
        Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time + " 00:00:00");

        rand = date1.getTime();
        return rand;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return rand;

}

From source file:Util.java

public static Timestamp convertStringToTimestamp(String str_date) {
    try {/*from  w  w  w.j av a  2s .  c  om*/
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date date = (Date) formatter.parse(str_date);
        java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());

        return timeStampDate;
    } catch (ParseException e) {
        System.out.println("Exception :" + e);
        return null;
    }
}

From source file:Main.java

public static String formatDateRange(Context context, Date start, Date end) {
    if (start != null && end != null) {
        return DateUtils.formatDateRange(context, start.getTime(), end.getTime(), DateUtils.FORMAT_ABBREV_ALL);
    }// ww  w.j  a v  a2  s  .  c  o  m
    return "";
}

From source file:Main.java

public static long timestampToMillis(String timestamp, long defaultValue) {
    if (TextUtils.isEmpty(timestamp)) {
        return defaultValue;
    }//from w  ww . j  a va2s.c o  m
    Date d = parseTimestamp(timestamp);
    return d == null ? defaultValue : d.getTime();
}

From source file:Main.java

public static boolean timeCompare(String beforeTime, String endTime, String format) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    try {//  w  w  w.  jav  a  2s  .com
        Date beforeDate = simpleDateFormat.parse(beforeTime);
        long beforeTimeLong = beforeDate.getTime();

        Date endDate = simpleDateFormat.parse(endTime);
        long endTimeLong = endDate.getTime();

        return beforeTimeLong > endTimeLong;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.vamonossoftware.core.DateUtil.java

/**
 * @return The number of days difference between the 2 dates.
 *//*from w  ww .ja v a2 s. c  o  m*/
public static int diff(Date date1, Date date2) {
    return (int) ((date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24));
}