我知道 Joda Instant 与 Unix Timestamp 类似,因为它存储自 1.1.1970 以来的毫秒数。所以我想将它作为 INTEGER 插入到我的 Android SQLite 数据库中。但是在我的应用程序中,我使用 Joda LocalDate,因此我需要将 LocalDate 转换为 Instants,反之亦然。
谁能告诉我怎么做?
为了将 a 转换为 a LocalDate
,Instant
您需要一天中的时间和时区。该类LocalDate
提供了toDateTime()方法,因此您可以使用此示例并根据需要对其进行调整:
LocalDate date = new LocalDate();
LocalTime time = LocalTime.MIDNIGHT;
DateTimeZone zone = DateTimeZone.getDefault();
Instant instant = date.toDateTime(time, zone).toInstant();
反向也需要 timezone。
Instant instant = Instant.now();
DateTimeZone zone = DateTimeZone.getDefault();
LocalDate date = instant.toDateTime(zone).toLocalDate();
尽管可以在 Joda-Time 中省略区域参数(这意味着系统时区),但我建议明确指定它以使时区依赖性更清晰。同一瞬间/时刻可能与全球不同日期相关(由于某些地点的午夜变化或由于跨越国际日期变更线)。