/** * Sets the nanoseconds within the current second * <p> * Note that this method sets the * same value as {@link #setMillis(int)} but with more precision. * </p> */ public BaseDateTimeDt setNanos(long theNanos) { validateValueInRange(theNanos, 0, NANOS_PER_SECOND-1); String fractionalSeconds = StringUtils.leftPad(Long.toString(theNanos), 9, '0'); // Strip trailing 0s for (int i = fractionalSeconds.length(); i > 0; i--) { if (fractionalSeconds.charAt(i-1) != '0') { fractionalSeconds = fractionalSeconds.substring(0, i); break; } } int millis = (int)(theNanos / NANOS_PER_MILLIS); setFieldValue(Calendar.MILLISECOND, millis, fractionalSeconds, 0, 999); return this; }
private void setFieldValue(int theField, int theValue, String theFractionalSeconds, int theMinimum, int theMaximum) { validateValueInRange(theValue, theMinimum, theMaximum); Calendar cal; if (getValue() == null) { cal = new GregorianCalendar(0, 0, 0); } else { cal = getValueAsCalendar(); } if (theField != -1) { cal.set(theField, theValue); } if (theFractionalSeconds != null) { myFractionalSeconds = theFractionalSeconds; } else if (theField == Calendar.MILLISECOND) { myFractionalSeconds = StringUtils.leftPad(Integer.toString(theValue), 3, '0'); } super.setValue(cal.getTime()); }
/** * Sets the nanoseconds within the current second * <p> * Note that this method sets the * same value as {@link #setMillis(int)} but with more precision. * </p> */ public BaseDateTimeDt setNanos(long theNanos) { validateValueInRange(theNanos, 0, NANOS_PER_SECOND-1); String fractionalSeconds = StringUtils.leftPad(Long.toString(theNanos), 9, '0'); // Strip trailing 0s for (int i = fractionalSeconds.length(); i > 0; i--) { if (fractionalSeconds.charAt(i-1) != '0') { fractionalSeconds = fractionalSeconds.substring(0, i); break; } } int millis = (int)(theNanos / NANOS_PER_MILLIS); setFieldValue(Calendar.MILLISECOND, millis, fractionalSeconds, 0, 999); return this; }
private void setFieldValue(int theField, int theValue, String theFractionalSeconds, int theMinimum, int theMaximum) { validateValueInRange(theValue, theMinimum, theMaximum); Calendar cal; if (getValue() == null) { cal = new GregorianCalendar(0, 0, 0); } else { cal = getValueAsCalendar(); } if (theField != -1) { cal.set(theField, theValue); } if (theFractionalSeconds != null) { myFractionalSeconds = theFractionalSeconds; } else if (theField == Calendar.MILLISECOND) { myFractionalSeconds = StringUtils.leftPad(Integer.toString(theValue), 3, '0'); } super.setValue(cal.getTime()); }