我的日期字符串格式是这样的:2012年1月2日 在Instant.parse()方法之后,即时实例变为2012年1月1日的日期是1天前,为什么?如果原来的日期字符串为2012年1月1日,即时将是12月31日,2011年在MongoDB中的日期:当将日期对象插入Mongo数据库时,日期比它自己早1天
String dateString="Jan 1, 2012";
Instant instant = Instant.parse(dateString, new DateTimeFormatterBuilder()
.appendMonthOfYearShortText()
.appendLiteral(" ")
.appendDayOfMonth(1)
.appendLiteral(", ")
.appendYear(4, 4)
.toFormatter());
DateTime dateTime = new DateTime(instant);
Date date = new Date(dateTime.getMillis());
document.append("time", new Date(dateTime.getMillis()));
tagsDbCollection.insert(document);
我使用MongoDB中存储这些日期。我已经测试过,它显示格式化日期字符串 - >即时没有错误。 但是,当我将这个Date类型对象插入到MongoDB中时,MongoDB中的日期字符串早于1天。为什么?
在MongoDB中:
/* 0 */
{
"_id" : ObjectId("50221a40da74d74053abb445"),
"time" : ISODate("2011-12-31T14:00:00Z")
}
您是否尝试添加时区组件?它可能与你的本地和日期抵消你的时区的东西。 – 2012-08-07 21:22:50