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.baifendian.swordfish.common.utils.DateUtils.java

/**
 * ?/*w  ww  . j av a2s .  c o m*/
 *
 * @param d1
 * @param d2
 * @return
 */
public static long diffMs(Date d1, Date d2) {
    return Math.abs(d1.getTime() - d2.getTime());
}

From source file:com.clican.pluto.dataprocess.dpl.function.impl.Duration.java

public static double duration(Date d1, Date d2, String step) throws PrefixAndSuffixException {
    if (StringUtils.isEmpty(step) || step.equals("day")) {
        return (d1.getTime() - d2.getTime()) / (1000L * 3600 * 24);
    } else if (step.equals("month")) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);//from ww  w . j  a  v a2 s .c o  m
        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);
        int month = (c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12
                + (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
        return month;
    } else {
        throw new PrefixAndSuffixException("??");
    }
}

From source file:Main.java

public static Date getTimeCurrent() {
    Calendar cal = Calendar.getInstance();

    Date timeNow = new Date();

    cal.setTime(timeNow);/*from  w  w  w.  jav a2  s.  co  m*/

    // Um numero negativo vai decrementar a data
    cal.add(Calendar.MILLISECOND, TimeZone.getDefault().getOffset(timeNow.getTime()) * -1);

    return cal.getTime();
}

From source file:com.cloud.utils.NumbersUtil.java

/**
 * Converts a string of the format 'yy-MM-dd'T'HH:mm:ss.SSS" into ms.
 *
 * @param str containing the interval.//w  ww . j  av  a  2s.  c  o  m
 * @param defaultValue value to return if str doesn't parse.  If -1, throws VmopsRuntimeException
 * @return interval in ms
 */
public static long parseInterval(String str, long defaultValue) {
    try {
        if (str == null) {
            throw new ParseException("String is wrong", 0);
        }

        SimpleDateFormat sdf = null;
        if (str.contains("D")) {
            sdf = new SimpleDateFormat("dd'D'HH'h'mm'M'ss'S'SSS'ms'");
        } else if (str.contains("h")) {
            sdf = new SimpleDateFormat("HH'h'mm'M'ss'S'SSS'ms'");
        } else if (str.contains("M")) {
            sdf = new SimpleDateFormat("mm'M'ss'S'SSS'ms'");
        } else if (str.contains("S")) {
            sdf = new SimpleDateFormat("ss'S'SSS'ms'");
        } else if (str.contains("ms")) {
            sdf = new SimpleDateFormat("SSS'ms'");
        }
        if (sdf == null) {
            throw new ParseException("String is wrong", 0);
        }

        Date date = sdf.parse(str);
        return date.getTime();
    } catch (ParseException e) {
        if (defaultValue != -1) {
            return defaultValue;
        } else {
            throw new CloudRuntimeException("Unable to parse: " + str, e);
        }
    }
}

From source file:Main.java

/**
 *
 * @param date//  ww  w  . j a v  a 2 s  .  c om
 * @return Converts a java.util.Date into an instance of
 * XMLGregorianCalendar
 */
public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        return df.newXMLGregorianCalendar(gc);
    }
}

From source file:org.envirocar.wps.util.BasicStats.java

/**
 * calculates acceleration (delta_speed/delta_time in m/s^2) between two single
 * track measurements, represented as Geotools simple features
 * //from   w  w w . j a  v a 2s. co  m
 * 
 * @param feat1
 *            first track measurement (must be temporally before second)
 * @param feat2
 *            second track measurement
 * @return acceleration between first and second measurement
 * @throws ParseException 
 */
public static double calculateAcceleration(SimpleFeature feat1, SimpleFeature feat2) throws ParseException {
    double acceleration = 0;

    //compute delta t in seconds
    ISO8601DateFormat df = new ISO8601DateFormat();
    Date timeStart = df.parse((String) feat1.getAttribute(FeatureProperties.TIME));
    Date timeEnd = df.parse((String) feat2.getAttribute(FeatureProperties.TIME));
    long delta_t = (timeEnd.getTime() - timeStart.getTime()) * 1000;

    //compute delta speed in m/s
    double delta_speed = 0;
    Object spo1 = feat1.getAttribute(FeatureProperties.SPEED);
    Object spo2 = feat2.getAttribute(FeatureProperties.SPEED);

    //compute delta speed in km/h
    if (spo1 != null && spo2 != null) {
        double speed1 = Double.parseDouble((String) spo1);
        double speed2 = Double.parseDouble((String) spo2);
        if (speed2 > speed1) {
            delta_speed = speed2 - speed1;
        }
    }

    //convert to m/s
    delta_speed = delta_speed * 0.278;

    //compute acceleration in m/s^2
    acceleration = delta_speed / delta_t;

    return acceleration;
}

From source file:com.wavemaker.commons.json.deserializer.WMDateDeSerializer.java

public static Date getDate(String value) {
    if (StringUtils.isBlank(value)) {
        return null;
    }/*ww  w. j  av a  2s  . co m*/
    try {
        Date parsedDate = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT).parse(value);
        return new Timestamp(parsedDate.getTime());
    } catch (ParseException e) {
        logger.trace("{} is not in the expected date time format {}", value, DEFAULT_DATE_TIME_FORMAT);
    }
    try {
        Date parsedDate = new SimpleDateFormat(DEFAULT_DATE_FORMAT).parse(value);
        return new java.sql.Date(parsedDate.getTime());
    } catch (ParseException e) {
        logger.trace("{} is not in the expected date format {}", value, DEFAULT_DATE_FORMAT);
    }
    try {
        Date parsedDate = new SimpleDateFormat(DEFAULT_TIME_FORMAT).parse(value);
        return new Time(parsedDate.getTime());
    } catch (ParseException e) {
        logger.trace("{} is not in the expected time format {}", value, DEFAULT_TIME_FORMAT);
    }
    throw new WMRuntimeException("Failed to parse the string " + value + "as java.util.Date");
}

From source file:es.uvigo.ei.sing.gc.view.models.PasswordRecoveryViewModel.java

private static boolean lessThan48hours(Date date) {
    final long difference = System.currentTimeMillis() - date.getTime();

    return difference <= 48 * 60 * 60 * 1000; // 48 hours in milliseconds
}

From source file:Main.java

public static long toTimestamp(String date) {
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    java.util.Date temp;

    try {//  w w  w.j a va 2  s  .c om
        temp = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return -1;
    }
    return temp.getTime();

}

From source file:AIR.Common.Utilities.Dates.java

public static Timestamp getTimestamp(Date date) {
    if (date == null)
        return null;
    // TODO Shiva what about timezone?
    Timestamp ts = new Timestamp(date.getTime());
    return ts;/*from w w w.j a  v a  2  s.c  o  m*/
}