Example usage for java.sql Timestamp setNanos

List of usage examples for java.sql Timestamp setNanos

Introduction

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

Prototype

public void setNanos(int n) 

Source Link

Document

Sets this Timestamp object's nanos field to the given value.

Usage

From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java

/**
 * <pre>//from   w  w w  . j  av a 2s.  c o m
 * Convert an object to a java.util.Date.  allows, dates, null, blank, 
 * yyyymmdd or yyyymmdd hh24:mm:ss
 * or yyyy/MM/dd HH:mm:ss.SSS
 * </pre>
 * @param inputObject
 *          is the String or Date to convert
 * 
 * @return the Date
 */
public static Date dateValue(Object inputObject) {
    if (inputObject == null) {
        return null;
    }

    if (inputObject instanceof java.util.Date) {
        return (Date) inputObject;
    }

    if (inputObject instanceof String) {
        String input = (String) inputObject;
        //trim and handle null and empty
        if (isBlank(input)) {
            return null;
        }

        try {
            if (input.length() == 8) {

                return dateFormat().parse(input);
            }
            if (!contains(input, '.')) {
                if (contains(input, '/')) {
                    return dateMinutesSecondsFormat.parse(input);
                }
                //else no slash
                return dateMinutesSecondsNoSlashFormat.parse(input);
            }
            if (contains(input, '/')) {
                //see if the period is 6 back
                int lastDotIndex = input.lastIndexOf('.');
                if (lastDotIndex == input.length() - 7) {
                    String nonNanoInput = input.substring(0, input.length() - 3);
                    Date date = timestampFormat.parse(nonNanoInput);
                    //get the last 3
                    String lastThree = input.substring(input.length() - 3, input.length());
                    int lastThreeInt = Integer.parseInt(lastThree);
                    Timestamp timestamp = new Timestamp(date.getTime());
                    timestamp.setNanos(timestamp.getNanos() + (lastThreeInt * 1000));
                    return timestamp;
                }
                return timestampFormat.parse(input);
            }
            //else no slash
            return timestampNoSlashFormat.parse(input);
        } catch (ParseException pe) {
            throw new RuntimeException(errorStart + toStringForLog(input));
        }
    }

    throw new RuntimeException("Cannot convert Object to date : " + toStringForLog(inputObject));
}

From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java

/**
 * return a date based on input, null safe.  Allow any of the three 
 * formats://from ww  w.j  a  v a  2  s .  co m
 * yyyyMMdd
 * yyyy/MM/dd HH:mm:ss
 * yyyy/MM/dd HH:mm:ss.SSS
 * yyyy/MM/dd HH:mm:ss.SSSSSS
 * 
 * @param input
 * @return the millis, -1 for null
 */
synchronized static Date stringToTimestampHelper(String input) {
    //trim and handle null and empty
    if (isBlank(input)) {
        return null;
    }

    try {
        //convert mainframe
        if (equals("99999999", input) || equals("999999", input)) {
            input = "20991231";
        }
        if (input.length() == 8) {

            return dateFormat().parse(input);
        }
        if (!contains(input, '.')) {
            if (contains(input, '/')) {
                return dateMinutesSecondsFormat.parse(input);
            }
            //else no slash
            return dateMinutesSecondsNoSlashFormat.parse(input);
        }
        if (contains(input, '/')) {
            //see if the period is 6 back
            int lastDotIndex = input.lastIndexOf('.');
            if (lastDotIndex == input.length() - 7) {
                String nonNanoInput = input.substring(0, input.length() - 3);
                Date date = timestampFormat.parse(nonNanoInput);
                //get the last 3
                String lastThree = input.substring(input.length() - 3, input.length());
                int lastThreeInt = Integer.parseInt(lastThree);
                Timestamp timestamp = new Timestamp(date.getTime());
                timestamp.setNanos(timestamp.getNanos() + (lastThreeInt * 1000));
                return timestamp;
            }
            return timestampFormat.parse(input);
        }
        //else no slash
        return timestampNoSlashFormat.parse(input);
    } catch (ParseException pe) {
        throw new RuntimeException(errorStart + input);
    }
}

From source file:org.openTwoFactor.server.util.TwoFactorServerUtilsElSafe.java

/**
 * return a date based on input, null safe.  Allow any of the three 
 * formats://from  w  ww.  j ava  2  s . com
 * yyyyMMdd
 * yyyy/MM/dd
 * yyyy/MM/dd HH:mm:ss
 * yyyy/MM/dd HH:mm:ss.SSS
 * yyyy/MM/dd HH:mm:ss.SSSSSS
 * 
 * @param input
 * @return the millis, -1 for null
 */
synchronized static Date stringToTimestampHelper(String input) {
    //trim and handle null and empty
    if (isBlank(input)) {
        return null;
    }
    input = input.trim();
    try {
        //convert mainframe
        if (equals("99999999", input) || equals("999999", input)) {
            input = "20991231";
        }
        if (input.length() == 8) {

            return dateFormat().parse(input);
        }
        if (input.length() == 10) {

            return dateFormat2().parse(input);
        }
        if (!contains(input, '.')) {
            if (contains(input, '/')) {
                return dateMinutesSecondsFormat.parse(input);
            }
            //else no slash
            return dateMinutesSecondsNoSlashFormat.parse(input);
        }
        if (contains(input, '/')) {
            //see if the period is 6 back
            int lastDotIndex = input.lastIndexOf('.');
            if (lastDotIndex == input.length() - 7) {
                String nonNanoInput = input.substring(0, input.length() - 3);
                Date date = timestampFormat.parse(nonNanoInput);
                //get the last 3
                String lastThree = input.substring(input.length() - 3, input.length());
                int lastThreeInt = Integer.parseInt(lastThree);
                Timestamp timestamp = new Timestamp(date.getTime());
                timestamp.setNanos(timestamp.getNanos() + (lastThreeInt * 1000));
                return timestamp;
            }
            return timestampFormat.parse(input);
        }
        //else no slash
        return timestampNoSlashFormat.parse(input);
    } catch (ParseException pe) {
        throw new RuntimeException(errorStart + input);
    }
}