如何将 bytea 转换为 joda.time.DateTime 或时间戳?
How to convert bytea to joda.time.DateTime or to Timestamp?
在我的数据库中,我有一个 bytea 类型的列。
我想检索该值并转换为日期时间。
我的值是这样的:
\xaced0005737200166f72672e6a6f64612e74696d652e4461746554696d65b83c78646a5bddf90200007872001f6f72672e6a6f64612e74696d652e626173652e426173654461746554696d65fffff9e14f5d2ea30200024a0007694d696c6c69734c000b694368726f6e6f6c6f677974001a4c6f72672f6a6f64612f74696d652f4368726f6e6f6c6f67793b787000000168cd2d829e737200276f72672e6a6f64612e74696d652e6368726f6e6f2e49534f4368726f6e6f6c6f67792453747562a9c811667137502703000078707372001f6f72672e6a6f64612e74696d652e4461746554696d655a6f6e652453747562a62f019a7c321ae30300007870770500035554437878
我的数据库访问码:
ResultSet resultSet = statement.executeQuery("SELECT * FROM public.history");
while (resultSet.next()) {
System.out.printf("\n acquire_date: " + resultSet.getString("acquire_Date") );
}
如何将该值转换为 joda DateTime 或 java.util.Date?
我希望时间格式如下:yyyy-MM-dd'T'HH:mm:ss'Z'
Database view
看起来您实际上已经将 org.joda.time.DateTime
个对象序列化为字节数组并存储在数据库中(这是一种糟糕的日期存储方式)。您只需要将这些数组反序列化回 DateTime
对象。下面是一些使用您的示例值的概念验证代码:
import java.io.ByteArrayInputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import javax.xml.bind.DatatypeConverter;
import org.joda.time.DateTime;
public class SerialTest {
public static void main(final String... args) throws Exception {
final String s = "aced0005737200166f72672e6a6f64612e74696d652e4461746554696d65b83c78646a5bddf90200007872001f6f72672e6a6f64612e74696d652e626173652e426173654461746554696d65fffff9e14f5d2ea30200024a0007694d696c6c69734c000b694368726f6e6f6c6f677974001a4c6f72672f6a6f64612f74696d652f4368726f6e6f6c6f67793b787000000168cd2d829e737200276f72672e6a6f64612e74696d652e6368726f6e6f2e49534f4368726f6e6f6c6f67792453747562a9c811667137502703000078707372001f6f72672e6a6f64612e74696d652e4461746554696d655a6f6e652453747562a62f019a7c321ae30300007870770500035554437878";
final byte[] bytes = DatatypeConverter.parseHexBinary(s);
// you would probably use bytes = resultSet.getBytes("acquire_Date") to get the byte array
final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
final ObjectInput in = new ObjectInputStream(bis);
final DateTime dt = (DateTime) in.readObject();
System.out.println(dt);
}
}
结果:2019-02-08T12:55:38.398Z
另请注意,反序列化 java.
中的值时可能 security issues
在我的数据库中,我有一个 bytea 类型的列。 我想检索该值并转换为日期时间。
我的值是这样的:
\xaced0005737200166f72672e6a6f64612e74696d652e4461746554696d65b83c78646a5bddf90200007872001f6f72672e6a6f64612e74696d652e626173652e426173654461746554696d65fffff9e14f5d2ea30200024a0007694d696c6c69734c000b694368726f6e6f6c6f677974001a4c6f72672f6a6f64612f74696d652f4368726f6e6f6c6f67793b787000000168cd2d829e737200276f72672e6a6f64612e74696d652e6368726f6e6f2e49534f4368726f6e6f6c6f67792453747562a9c811667137502703000078707372001f6f72672e6a6f64612e74696d652e4461746554696d655a6f6e652453747562a62f019a7c321ae30300007870770500035554437878
我的数据库访问码:
ResultSet resultSet = statement.executeQuery("SELECT * FROM public.history");
while (resultSet.next()) {
System.out.printf("\n acquire_date: " + resultSet.getString("acquire_Date") );
}
如何将该值转换为 joda DateTime 或 java.util.Date?
我希望时间格式如下:yyyy-MM-dd'T'HH:mm:ss'Z'
Database view
看起来您实际上已经将 org.joda.time.DateTime
个对象序列化为字节数组并存储在数据库中(这是一种糟糕的日期存储方式)。您只需要将这些数组反序列化回 DateTime
对象。下面是一些使用您的示例值的概念验证代码:
import java.io.ByteArrayInputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import javax.xml.bind.DatatypeConverter;
import org.joda.time.DateTime;
public class SerialTest {
public static void main(final String... args) throws Exception {
final String s = "aced0005737200166f72672e6a6f64612e74696d652e4461746554696d65b83c78646a5bddf90200007872001f6f72672e6a6f64612e74696d652e626173652e426173654461746554696d65fffff9e14f5d2ea30200024a0007694d696c6c69734c000b694368726f6e6f6c6f677974001a4c6f72672f6a6f64612f74696d652f4368726f6e6f6c6f67793b787000000168cd2d829e737200276f72672e6a6f64612e74696d652e6368726f6e6f2e49534f4368726f6e6f6c6f67792453747562a9c811667137502703000078707372001f6f72672e6a6f64612e74696d652e4461746554696d655a6f6e652453747562a62f019a7c321ae30300007870770500035554437878";
final byte[] bytes = DatatypeConverter.parseHexBinary(s);
// you would probably use bytes = resultSet.getBytes("acquire_Date") to get the byte array
final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
final ObjectInput in = new ObjectInputStream(bis);
final DateTime dt = (DateTime) in.readObject();
System.out.println(dt);
}
}
结果:2019-02-08T12:55:38.398Z
另请注意,反序列化 java.
中的值时可能 security issues