Example usage for java.sql Timestamp getNanos

List of usage examples for java.sql Timestamp getNanos

Introduction

In this page you can find the example usage for java.sql Timestamp getNanos.

Prototype

public int getNanos() 

Source Link

Document

Gets this Timestamp object's nanos value.

Usage

From source file:org.apache.hadoop.hive.serde2.io.TimestampWritable.java

/**
 * Writes a Timestamp's serialized value to byte array b at
 * @param t/*from   w  ww  .j  a va  2s . co m*/
 * @param b
 */
public static void convertTimestampToBytes(Timestamp t, byte[] b, int offset) {
    if (b.length < 9) {
        LOG.error("byte array too short");
    }
    long millis = t.getTime();
    int nanos = t.getNanos();

    boolean hasDecimal = nanos != 0 && setNanosBytes(nanos, b, offset + 4);
    setSecondsBytes(millis, b, offset, hasDecimal);
}

From source file:org.apereo.portal.concurrency.locking.RDBMEntityLockStore.java

/**
 * @return long// ww  w. ja va 2  s  .  co  m
 */
private static long getTimestampMillis(Timestamp ts) {
    if (timestampHasMillis) {
        return ts.getTime();
    } else {
        return (ts.getTime() + ts.getNanos() / 1000000);
    }
}

From source file:net.sf.jasperreports.data.cache.TimestampStore.java

@Override
public void addValue(Object object) {
    if (!(object instanceof Timestamp)) {
        throw new IllegalArgumentException();
    }/*  w  w w  .j  a  v a  2s  .  c  o  m*/

    Timestamp value = (Timestamp) object;
    long time = value.getTime();
    int nanos = value.getNanos();
    timeStore.add(time);
    nanoStore.add(nanos);
}

From source file:org.apache.hadoop.hive.ql.udf.generic.GenericUDFFromUtcTimestamp.java

protected Timestamp applyOffset(long offset, Timestamp t) {
    long newTime = t.getTime() + offset;
    Timestamp t2 = new Timestamp(newTime);
    t2.setNanos(t.getNanos());

    return t2;/*from ww w. j av a  2 s.  co  m*/
}

From source file:org.dozer.converters.DateConverter.java

public Object convert(Class destClass, Object srcObj) {
    final Class srcFieldClass = srcObj.getClass();

    long time;/*from   w  w  w .j av a 2  s  . c o  m*/
    int nanos = 0;
    if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar inVal = (Calendar) srcObj;
        time = inVal.getTime().getTime();
    } else if (Timestamp.class.isAssignableFrom(srcFieldClass)) {
        Timestamp timestamp = (Timestamp) srcObj;
        time = timestamp.getTime();
        nanos = timestamp.getNanos();
    } else if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        time = ((java.util.Date) srcObj).getTime();
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        time = ((XMLGregorianCalendar) srcObj).toGregorianCalendar().getTimeInMillis();
    } else if (dateFormat != null && String.class.isAssignableFrom(srcObj.getClass())) {
        try {
            if ("".equals(srcObj)) {
                return null;
            }
            time = dateFormat.parse((String) srcObj).getTime();
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
        // Default conversion
    } else {
        try {
            time = Long.parseLong(srcObj.toString());
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    try {
        if (Calendar.class.isAssignableFrom(destClass)) {
            Constructor constructor = destClass.getConstructor();
            Calendar result = (Calendar) constructor.newInstance();
            result.setTimeInMillis(time);
            return result;
        }
        Constructor constructor = destClass.getConstructor(Long.TYPE);
        Object result = constructor.newInstance(time);
        if (nanos != 0 && (Timestamp.class.isAssignableFrom(destClass))) {
            ((Timestamp) result).setNanos(nanos);
        }
        return result;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}

From source file:com.github.dozermapper.core.converters.DateConverter.java

public Object convert(Class destClass, Object srcObj) {
    final Class srcFieldClass = srcObj.getClass();

    long time;/*www.  ja  v a2s.c  om*/
    int nanos = 0;
    if (Calendar.class.isAssignableFrom(srcFieldClass)) {
        Calendar inVal = (Calendar) srcObj;
        time = inVal.getTime().getTime();
    } else if (Timestamp.class.isAssignableFrom(srcFieldClass)) {
        Timestamp timestamp = (Timestamp) srcObj;
        time = timestamp.getTime();
        nanos = timestamp.getNanos();
    } else if (java.util.Date.class.isAssignableFrom(srcFieldClass)) {
        time = ((java.util.Date) srcObj).getTime();
    } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) {
        time = ((XMLGregorianCalendar) srcObj).toGregorianCalendar().getTimeInMillis();
    } else if (dateFormat != null && String.class.isAssignableFrom(srcObj.getClass())) {
        try {
            if ("".equals(srcObj)) {
                return null;
            }
            time = dateFormat.parse((String) srcObj).getTime();
        } catch (ParseException e) {
            throw new ConversionException("Unable to parse source object using specified date format", e);
        }
        // Default conversion
    } else {
        try {
            time = Long.parseLong(srcObj.toString());
        } catch (NumberFormatException e) {
            throw new ConversionException("Unable to determine time in millis of source object", e);
        }
    }

    try {
        if (Calendar.class.isAssignableFrom(destClass)) {
            Constructor constructor = destClass.getConstructor();
            Calendar result = (Calendar) constructor.newInstance();
            result.setTimeInMillis(time);
            return result;
        }

        if (dateFormat != null && String.class.isAssignableFrom(destClass)) {
            return dateFormat.format(new java.util.Date(time));
        }

        Constructor constructor = destClass.getConstructor(Long.TYPE);
        Object result = constructor.newInstance(time);
        if (nanos != 0 && (Timestamp.class.isAssignableFrom(destClass))) {
            ((Timestamp) result).setNanos(nanos);
        }
        return result;
    } catch (Exception e) {
        throw new ConversionException(e);
    }
}

From source file:org.zaproxy.zap.extension.websocket.WebSocketMessageDTO.java

/**
 * Helper to set {@link WebSocketMessageDTO#timestamp} and {@link WebSocketMessageDTO#dateTime}.
 *
 * @param ts/*  ww w.  j  a va 2 s  . co m*/
 */
public void setTime(Timestamp ts) {
    timestamp = ts.getTime() + (ts.getNanos() / 1000000000);

    synchronized (dateFormatter) {
        dateTime = dateFormatter.format(ts);
    }

    String nanos = Integer.toString(ts.getNanos()).replaceAll("0+$", "");
    if (nanos.length() == 0) {
        nanos = "0";
    }

    dateTime = dateTime.replaceFirst("([0-9]+:[0-9]+:[0-9]+)", "$1." + nanos);
}

From source file:DateUtils.java

/**
 * Convert timestamp to string including it's nanosecond portion so that it 
 * can be safely stored in variable of web page.
 * //  w ww.  ja  va2  s . com
 * @param tsTimestamp - timestamp to convert
 * @return String - text containing time and nanosecond portion of timestamp
 */
public static String getTimestampAsString(Timestamp tsTimestamp) {
    StringBuffer sbTimestamp = new StringBuffer();

    sbTimestamp.append(tsTimestamp.getTime());
    sbTimestamp.append(NANO_SEPARATOR);
    sbTimestamp.append(tsTimestamp.getNanos());

    return sbTimestamp.toString();
}

From source file:org.jdesigner.platform.web.converter.DateTimeConverter.java

/**
 * Convert the input object into a Date object of the specified type.
 * <p>//from   w ww.j av a 2s . co  m
 * This method handles conversions between the following types:
 * <ul>
 * <li><code>java.util.Date</code></li>
 * <li><code>java.util.Calendar</code></li>
 * <li><code>java.sql.Date</code></li>
 * <li><code>java.sql.Time</code></li>
 * <li><code>java.sql.Timestamp</code></li>
 * </ul>
 * 
 * It also handles conversion from a <code>String</code> to any of the above
 * types.
 * <p>
 * 
 * For <code>String</code> conversion, if the converter has been configured
 * with one or more patterns (using <code>setPatterns()</code>), then the
 * conversion is attempted with each of the specified patterns. Otherwise
 * the default <code>DateFormat</code> for the default locale (and
 * <i>style</i> if configured) will be used.
 * 
 * @param targetType
 *            Data type to which this value should be converted.
 * @param value
 *            The input value to be converted.
 * @return The converted value.
 * @throws Exception
 *             if conversion cannot be performed successfully
 */
protected Object convertToType(Class targetType, Object value) throws Exception {

    // Class sourceType = value.getClass();

    // Handle java.sql.Timestamp
    if (value instanceof java.sql.Timestamp) {

        // ---------------------- JDK 1.3 Fix ----------------------
        // N.B. Prior to JDK 1.4 the Timestamp's getTime() method
        // didn't include the milliseconds. The following code
        // ensures it works consistently accross JDK versions
        java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
        long timeInMillis = ((timestamp.getTime() / 1000) * 1000);
        timeInMillis += timestamp.getNanos() / 1000000;
        // ---------------------- JDK 1.3 Fix ----------------------
        return toDate(targetType, timeInMillis);
    }

    // Handle Date (includes java.sql.Date & java.sql.Time)
    if (value instanceof Date) {
        Date date = (Date) value;
        return toDate(targetType, date.getTime());
    }

    // Handle Calendar
    if (value instanceof Calendar) {
        Calendar calendar = (Calendar) value;
        return toDate(targetType, calendar.getTime().getTime());
    }

    // Handle Long
    if (value instanceof Long) {
        Long longObj = (Long) value;
        return toDate(targetType, longObj.longValue());
    }

    // Convert all other types to String & handle
    String stringValue = value.toString().trim();
    if (stringValue.length() == 0) {
        return handleMissing(targetType);
    }

    // Default String conversion
    return toDate(targetType, stringValue);

}

From source file:de.kp.ames.web.core.domain.model.JsonRegistryObject.java

/**
 * Convert registry object into JSON object
 * //from w  ww. j  a  v a  2  s  . c  om
 * @param ro
 * @param locale
 * @return
 * @throws JAXRException 
 * @throws JSONException 
 * @throws Exception
 */
public void set(RegistryObjectImpl ro, Locale locale) throws JSONException, JAXRException {

    /*
     * Convert identifier       
     */
    put(JaxrConstants.RIM_ID, ro.getId());
    put(JaxrConstants.RIM_LID, ro.getLid());

    /* 
     * Convert name
     */
    String name = jaxrBase.getName(ro, locale);

    /*
     * If no matching locale string exists, get the closest match
     */
    name = (name == "") ? ro.getDisplayName() : name;

    put(JaxrConstants.RIM_NAME, name);

    /*
     * Convert description
     */
    String description = jaxrBase.getDescription(ro, locale);
    description = (description == "") ? ((InternationalStringImpl) ro.getDescription()).getClosestValue()
            : description;

    put(JaxrConstants.RIM_DESC, description);

    /*
     * Convert object type
     */
    String objectType = ro.getObjectType().getKey().getId();
    put(JaxrConstants.RIM_TYPE, objectType);

    /*
     * Convert home
     */
    String home = jaxrBase.getHome(ro);
    put(JaxrConstants.RIM_HOME, home);

    /*
     * Convert status
     */
    String status = jaxrBase.getStatus(ro);
    put(JaxrConstants.RIM_STATUS, status);

    /* 
     * Convert version               
     */
    String version = ro.getVersionName();
    put(JaxrConstants.RIM_VERSION, version);

    /*
     * Convert author
     */
    String author = jaxrBase.getAuthor(ro);
    put(JaxrConstants.RIM_AUTHOR, author);

    /*
     * Convert owner
     */
    String owner = jaxrBase.getOwner(ro);
    put(JaxrConstants.RIM_OWNER, owner);

    /*
     * Convert timestamp & events
     */
    AuditableEventImpl auditableEvent = jaxrBase.getLastEvent(ro);

    Timestamp lastModified = (auditableEvent == null) ? null : auditableEvent.getTimestamp();
    if (lastModified != null) {

        /*
         * Timestamp
         */
        long milliseconds = lastModified.getTime() + (lastModified.getNanos() / 1000000);
        put(JaxrConstants.RIM_TIMESTAMP, new Date(milliseconds).toString());

        /*
         * Event
         */
        String eventType = jaxrBase.getLastEventType(ro);
        put(JaxrConstants.RIM_EVENT, eventType);

    }

    /*
     * Classifications
     */
    JSONObject jClases = getClassifications(ro);
    put(JaxrConstants.RIM_CLAS, jClases.toString());

    /*
     * Slots
     */
    JSONObject jSlot = getSlots(ro);
    put(JaxrConstants.RIM_SLOT, jSlot.toString());

}