MySQLDateBinaryProtocolValue implements MySQLBinaryProtocolValue { @Override public void write(final MySQLPacketPayload payload, final Object value) { Timestamp timestamp = (Timestamp) value; Calendar calendar = Calendar.getInstance(); calendar.setTime(timestamp); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); int seconds = calendar.get(Calendar.SECOND); int nanos = timestamp.getNanos(); boolean isTimeAbsent = 0 == hourOfDay && 0 == minutes && 0 == seconds; boolean isNanosAbsent = 0 == nanos; if (isTimeAbsent && isNanosAbsent) { payload.writeInt1(4); writeDate(payload, year, month, dayOfMonth); return; } if (isNanosAbsent) { payload.writeInt1(7); writeDate(payload, year, month, dayOfMonth); writeTime(payload, hourOfDay, minutes, seconds); return; } payload.writeInt1(11); writeDate(payload, year, month, dayOfMonth); writeTime(payload, hourOfDay, minutes, seconds); writeNanos(payload, nanos); }  @Override Object read(final MySQLPacketPayload payload); @Override void write(final MySQLPacketPayload payload, final Object value);  }
@Test public void assertWriteWithFourBytes() { MySQLDateBinaryProtocolValue actual = new MySQLDateBinaryProtocolValue(); actual.write(payload, Timestamp.valueOf("1970-01-14 0:0:0")); verify(payload).writeInt1(4); verify(payload).writeInt2(1970); verify(payload).writeInt1(1); verify(payload).writeInt1(14); }
@Test public void assertWriteWithSevenBytes() { MySQLDateBinaryProtocolValue actual = new MySQLDateBinaryProtocolValue(); actual.write(payload, Timestamp.valueOf("1970-01-14 12:10:30")); verify(payload).writeInt1(7); verify(payload).writeInt2(1970); verify(payload).writeInt1(1); verify(payload).writeInt1(14); verify(payload).writeInt1(12); verify(payload).writeInt1(10); verify(payload).writeInt1(30); }
@Test public void assertWriteWithElevenBytes() { MySQLDateBinaryProtocolValue actual = new MySQLDateBinaryProtocolValue(); actual.write(payload, Timestamp.valueOf("1970-01-14 12:10:30.1")); verify(payload).writeInt1(11); verify(payload).writeInt2(1970); verify(payload).writeInt1(1); verify(payload).writeInt1(14); verify(payload).writeInt1(12); verify(payload).writeInt1(10); verify(payload).writeInt1(30); verify(payload).writeInt4(100000000); }