Here you can find the source of roundDatetimeValue(Object value)
Parameter | Description |
---|---|
value | a parameter |
public static Object roundDatetimeValue(Object value)
//package com.java2s; //License from project: Open Source License import java.sql.Timestamp; import java.util.Calendar; public class Main { /**// w w w . java 2 s . co m * Utility method for a datetime value * * @param value * @return */ public static Object roundDatetimeValue(Object value) { if (value == null) return null; Timestamp ts = value instanceof Timestamp ? (Timestamp) value : new Timestamp(((Calendar) value).getTimeInMillis()); int millis = ts.getNanos() / 1000000; int lastDigit = (int) (millis % 10); switch (lastDigit) { // 0, 1 -> 0 case 1: ts.setNanos((millis - 1) * 1000000); break; // 2, 3, 4 -> 3 case 2: ts.setNanos((millis + 1) * 1000000); break; case 4: ts.setNanos((millis - 1) * 1000000); break; // 5, 6, 7, 8 -> 7 case 5: ts.setNanos((millis + 2) * 1000000); break; case 6: ts.setNanos((millis + 1) * 1000000); break; case 8: ts.setNanos((millis - 1) * 1000000); break; // 9 -> 0 with overflow case 9: ts.setNanos(0); ts.setTime(ts.getTime() + millis + 1); break; // default, i.e. 0, 3, 7 -> 0, 3, 7 // don't change the millis but make sure that any // sub-millisecond digits are zeroed out default: ts.setNanos((millis) * 1000000); } if (value instanceof Calendar) { ((Calendar) value).setTimeInMillis(ts.getTime()); ((Calendar) value).getTimeInMillis(); return value; } return ts; } }