Java??GregorianCalendar???????????????,???????????GregorianCalendar???????????:
- ???: ??GregorianCalendar??????????,?????Java?ObjectOutputStream????,????GregorianCalendar????????????????,????????????????:
import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.util.GregorianCalendar; public class CalendarSerialization { public static void main(String[] args) { GregorianCalendar calendar = new GregorianCalendar(); // ??????? calendar.set(2021, 9, 1, 0, 0, 0); try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(calendar); objectOutputStream.close(); byte[] serializedCalendar = byteArrayOutputStream.toByteArray(); System.out.println("Serialized calendar: " + bytesToHex(serializedCalendar)); } catch (Exception e) { e.printStackTrace(); } } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("x", b)); } return sb.toString(); } }
- ????: ???????????GregorianCalendar??,?????Java?ObjectInputStream????,???????????????????,?????????????GregorianCalendar??:
import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import java.util.GregorianCalendar; public class CalendarDeserialization { public static void main(String[] args) { byte[] serializedCalendar = new byte[]{...}; // ?????????????? try { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedCalendar); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); GregorianCalendar deserializedCalendar = (GregorianCalendar) objectInputStream.readObject(); objectInputStream.close(); System.out.println("Deserialized calendar: " + deserializedCalendar.getTime()); } catch (Exception e) { e.printStackTrace(); } } }
??,??????GregorianCalendar????????????????,???????????,????????????????????