List of usage examples for java.sql Timestamp getNanos
public int getNanos()
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 w w. ja v a 2s . 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); } }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Set the given value as a parameter to the statement. *///from ww w. j a v a2s . co m public void setTimestamp(PreparedStatement stmnt, int idx, Timestamp val, Calendar cal, Column col) throws SQLException { // ensure that we do not insert dates at a greater precision than // that at which they will be returned by a SELECT int rounded = (int) Math.round(val.getNanos() / (double) datePrecision); int nanos = rounded * datePrecision; if (nanos > 999999999) { // rollover to next second val.setTime(val.getTime() + 1000); nanos = 0; } Timestamp valForStmnt = new Timestamp(val.getTime()); valForStmnt.setNanos(nanos); if (cal == null) stmnt.setTimestamp(idx, valForStmnt); else stmnt.setTimestamp(idx, valForStmnt, cal); }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Convert the specified column of the SQL ResultSet to the proper * java type. Converts the date from a {@link Timestamp} by default. *///w w w .ja va2 s. c o m public Date getDate(ResultSet rs, int column) throws SQLException { Timestamp tstamp = getTimestamp(rs, column, null); if (tstamp == null) return null; // get the fractional seconds component, rounding away anything beyond // milliseconds int fractional = 0; if (roundTimeToMillisec) { fractional = (int) Math.round(tstamp.getNanos() / (double) MILLI); } // get the millis component; some JDBC drivers round this to the // nearest second, while others do not long millis = (tstamp.getTime() / 1000L) * 1000L; return new Date(millis + fractional); }
From source file:org.etudes.component.app.melete.ModuleDB.java
public List getViewModules(String userId, String courseId) throws Exception { Connection dbConnection = null; List resList = new ArrayList(); List courseIdList = new ArrayList(); List sectionsList = null;//from w ww .ja v a 2 s . c o m Module mod = null; Query sectionQuery = null; try { dbConnection = SqlService.borrowConnection(); //Check the special access table to see if there are any records //for this user in this course ResultSet accRs, rs = null; String sql = "select a.module_id,a.start_date,a.end_date from melete_special_access a,melete_course_module c where a.users like ? and a.module_id=c.module_id and c.course_id = ?"; PreparedStatement accPstmt = dbConnection.prepareStatement(sql); accPstmt.setString(1, "%" + userId + "%"); accPstmt.setString(2, courseId); accRs = accPstmt.executeQuery(); Map accMap = new HashMap(); if (accRs != null) { int accModuleId; while (accRs.next()) { accModuleId = accRs.getInt("module_id"); AccessDates ad = new AccessDates(accRs.getTimestamp("start_date"), accRs.getTimestamp("end_date")); accMap.put(accModuleId, ad); } } sql = "select m.module_id,c.seq_no,m.title as modTitle,m.whats_next,m.seq_xml,d.start_date,d.end_date,s.section_id,s.content_type,s.title as secTitle from melete_module m inner join melete_module_shdates d on m.module_id=d.module_id inner join melete_course_module c on m.module_id=c.module_id left outer join melete_section s on m.module_id = s.module_id where c.course_id = ? and c.delete_flag=0 and c.archv_flag=0 and (s.delete_flag=0 or s.delete_flag is NULL) order by c.seq_no"; PreparedStatement pstmt = dbConnection.prepareStatement(sql); pstmt.setString(1, courseId); rs = pstmt.executeQuery(); ViewSecBean vsBean = null; Map vsBeanMap = null; SubSectionUtilImpl ssuImpl; StringBuffer rowClassesBuf; List vsBeanList = null; int prevModId = 0, prevSeqNo = 0; int moduleId, seqNo; ViewModBean vmBean = null; String seqXml, prevSeqXml = null; java.sql.Timestamp startTimestamp, endTimestamp; if (rs != null) { while (rs.next()) { moduleId = rs.getInt("module_id"); seqNo = rs.getInt("seq_no"); seqXml = rs.getString("seq_xml"); // Associate vsBeans to vmBean //This means its a new module if ((prevModId != 0) && (moduleId != prevModId)) { if (vsBeanMap != null) { if (vsBeanMap.size() > 0) { ssuImpl = new SubSectionUtilImpl(); ssuImpl.traverseDom(prevSeqXml, Integer.toString(prevSeqNo)); xmlSecList = ssuImpl.getXmlSecList(); rowClassesBuf = new StringBuffer(); //Comment for now xmlSecList = correctSections(vsBeanMap, mod, xmlSecList); vsBeanList = new ArrayList(); processViewSections(vsBeanMap, vsBeanList, xmlSecList, rowClassesBuf); vmBean.setVsBeans(vsBeanList); vmBean.setRowClasses(rowClassesBuf.toString()); } } vsBeanMap = null; } //Populate each vsBean and add to vsBeanMap int sectionId = rs.getInt("section_id"); if (sectionId != 0) { if (vsBeanMap == null) vsBeanMap = new LinkedHashMap(); vsBean = new ViewSecBean(); vsBean.setSectionId(sectionId); vsBean.setContentType(rs.getString("content_type")); vsBean.setTitle(rs.getString("secTitle")); vsBeanMap.put(new Integer(sectionId), vsBean); } //Populate vmBean //This means its the first module or a new module if ((prevModId == 0) || (moduleId != prevModId)) { vmBean = new ViewModBean(); vmBean.setModuleId(moduleId); vmBean.setSeqNo(seqNo); vmBean.setTitle(rs.getString("modTitle")); vmBean.setWhatsNext(rs.getString("whats_next")); vmBean.setSeqXml(seqXml); // what's next display seq number is number of top level sections + 1 SubSectionUtilImpl ssuImpl1 = new SubSectionUtilImpl(); int top = ssuImpl1.noOfTopLevelSections(seqXml); top = top + 1; String ns_number = new String(seqNo + "."); ns_number = ns_number.concat(Integer.toString(top)); vmBean.setNextStepsNumber(ns_number); startTimestamp = null; endTimestamp = null; //If special access is set up, use those dates; otherwise, //use module dates if (accMap.size() > 0) { AccessDates ad = (AccessDates) accMap.get(moduleId); if (ad == null) { startTimestamp = rs.getTimestamp("start_date"); endTimestamp = rs.getTimestamp("end_date"); } else { startTimestamp = ad.getAccStartTimestamp(); endTimestamp = ad.getAccEndTimestamp(); } } else { startTimestamp = rs.getTimestamp("start_date"); endTimestamp = rs.getTimestamp("end_date"); } java.sql.Timestamp currentTimestamp = new java.sql.Timestamp( Calendar.getInstance().getTimeInMillis()); if (((startTimestamp == null) || (startTimestamp.before(currentTimestamp))) && ((endTimestamp == null) || (endTimestamp.after(currentTimestamp)))) { vmBean.setVisibleFlag(true); } else { vmBean.setVisibleFlag(false); } if (startTimestamp != null) { vmBean.setStartDate(new java.util.Date( startTimestamp.getTime() + (startTimestamp.getNanos() / 1000000))); } if (endTimestamp != null) { vmBean.setEndDate(new java.util.Date( endTimestamp.getTime() + (endTimestamp.getNanos() / 1000000))); } resList.add(vmBean); } prevModId = moduleId; prevSeqNo = seqNo; prevSeqXml = seqXml; } //End while //The last module will not have had its sections added //so we do it here if (vsBeanMap != null) { if (vsBeanMap.size() > 0) { ssuImpl = new SubSectionUtilImpl(); ssuImpl.traverseDom(prevSeqXml, Integer.toString(prevSeqNo)); xmlSecList = ssuImpl.getXmlSecList(); rowClassesBuf = new StringBuffer(); xmlSecList = correctSections(vsBeanMap, mod, xmlSecList); vsBeanList = new ArrayList(); processViewSections(vsBeanMap, vsBeanList, xmlSecList, rowClassesBuf); vmBean.setVsBeans(vsBeanList); vmBean.setRowClasses(rowClassesBuf.toString()); } } accRs.close(); accPstmt.close(); rs.close(); pstmt.close(); } } catch (Exception e) { if (logger.isErrorEnabled()) logger.error(e); throw e; } finally { try { if (dbConnection != null) SqlService.returnConnection(dbConnection); } catch (Exception e1) { if (logger.isErrorEnabled()) logger.error(e1); throw e1; } } return resList; }
From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java
/** * return a date based on input, null safe. Allow any of the three * formats:// w w w.j av a 2 s. com * 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.client.util.TwoFactorClientCommonUtils.java
/** * <pre>/*w ww .ja v a 2s . c om*/ * 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)); }