private String dateToObjectId(String dateStr) throws BusinessException {
StringBuffer objectId = new StringBuffer("");
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = sdf.parse(dateStr);
objectId.append(Long.toHexString(date.getTime() / 1000)); //转换为16进制的字符串
while(objectId.length() < 24) { //bson-3.6.4.jar 版本校验ObjectId的长度为24位,不足24位补0
objectId.append(“0”);
}
return objectId.toString();
}
Java日期转换为MongoDB的ObjectId
最新推荐文章于 2022-11-11 22:25:02 发布