Skip to content

Instantly share code, notes, and snippets.

@ashishb888
Created July 10, 2020 03:21
Show Gist options
  • Save ashishb888/ad6ecd4fa4b4a451e44124913832fb59 to your computer and use it in GitHub Desktop.
Save ashishb888/ad6ecd4fa4b4a451e44124913832fb59 to your computer and use it in GitHub Desktop.
Java ByteBuffer: Putting dates, timestamps and strings. And getting them back
import java.nio.ByteBuffer;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class ByteBufferUsage {
public static void main(String[] args) {
ByteBuffer bb = ByteBuffer.allocate(40);
int p1 = 0;
int p2 = 10;
int len = 10;
long l = System.currentTimeMillis();
Date d = new Date(l);
Timestamp t = new Timestamp(l);
bb.position(p1);// Setting position manually
bb.put("Hello".getBytes()); // Putting strings
bb.position(p2);
bb.put("There".getBytes());
bb.putInt(20, 10);
bb.putLong(24, d.getTime()); // Putting dates
bb.putLong(32, t.getTime()); // Putting timestamps
System.out.println(getStringFromByteBuffer(bb, p1, len)); // Getting strings
System.out.println(getStringFromByteBuffer(bb, p2, len));
System.out.println(bb.getInt(20));
System.out.println(new Date(bb.getLong(24))); // Getting dates
System.out.println(new Timestamp(bb.getLong(32))); // Getting timestamps
System.out.println(toDate(bb.getLong(24))); // Formatting date to a custom format
System.out.println(toTimestamp(bb.getLong(32))); // Formatting timestamp to a custom format
}
static DateTimeFormatter timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static Timestamp toTimestamp(long l) {
return Timestamp.valueOf(
timestampFormatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault())));
}
private static Date toDate(long l) {
return Date.valueOf(dateFormatter.format(Instant.ofEpochMilli(l).atZone(ZoneId.systemDefault()).toLocalDate()));
}
private static String getStringFromByteBuffer(ByteBuffer byteBuffer, int offset, int length) {
byte[] bArr = new byte[length];
byteBuffer.position(offset);
byteBuffer.get(bArr, 0, length);
return new String(bArr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment