如何通过java从String读取在mongodb中插入相同的日期? [英] How to insert the same date in mongodb as read from String through java?
问题描述
DateOfTransaction
,具有这样的值21/08/2012
。 我写了一个java程序来读取CSV文件并将文档插入到mongodb中。
代码插入日期
SimpleDateFormat formatter = new SimpleDateFormat(dd / MM / yyyy); //设置日期格式
日期d2 = new SimpleDateFormat(dd / mm / yyyy)。parse(arr [10]);
bookRenterLineItemInstance.setDateoftransaction(d2.getTime());
解析adn dateobject中的每一件事情都是正确的。
但是当我查询mongodb集合时,会给我一天的记录。
示例如果我插入日期:21 / 08/2012
保存在mongodb中:DateOfTransaction:ISODate(2012-01-20T18: 38:00Z)
我所有的查询都与此字段相关。
请帮助,以便日期不被更改并保存为 ISODate(2012-01-21T18:38:00Z)
21/08/2012
目前正在尝试的两个问题要做:
- 您在第二行代码中输入了错字,这意味着您的
个月被解析为分钟 -
- 如上所述,您没有考虑时区。
要解决第一个问题,您需要更改
日期d2 = new SimpleDateFormat(dd / mm / yyyy)。parse(arr [10]) ;
至
Date d2 = new SimpleDateFormat(dd / MM / yyyy)。parse(arr [10]);
日期格式区分大小写,您尝试做的是将该月解析为** m ** inute而不是** M ** onth。在测试中,我跑了,这意味着几个月都是出现在1月份,我可以看到在你的例子中也发生了。另外,我注意到您的第一行代码,格式化程序(正确设置)不被使用。
第二个问题是,是的,Java Date
s的行为不符合您的期望。他们必须有一个时间部分,即使你不关心它只是日期。此外,他们必须有一个时区,因为他们有一段时间。
你可能没有选择移动到上级的Joda图书馆,在这种情况下有办法解决这个问题。我已经写了一个应该证明这一点的测试:
@Test
public void shouldParseDateCorrectly()throws Exception {
//给定
SimpleDateFormat format = new SimpleDateFormat(dd / MM / yyyy);
format.setTimeZone(TimeZone.getTimeZone(UTC));
//当
日期parsedDate = format.parse(21/08/2012);
// Then
Assert.assertThat(parsedDate.toString(),is(Tue Aug 21 02:00:00 CEST 2012));
Assert.assertThat(parsedDate.toGMTString(),is(2012年8月21日00:00:00 GMT));
}
所以你可以看到我使用了正确的格式,但也设置它的时区到UTC(所以它的偏移为零,没有夏令时)。然后当您使用此格式化程序解析日期时,日期将在UTC时区的午夜时间出现。因为我的电脑在欧洲,当我打印这个日期时,它显示的时间是凌晨2点,因为这个时区比UTC提前2个小时。但是当我使用不推荐使用的 toGMTString
方法时,时间会显示为零。
将此日期存入MongoDB使用Java驱动,它会保存日期,时间和时区,即使你关心的是日期。当您再次阅读日期时,您需要做的是记住它们是UTC并设置适当的时区。
或者,您可以存储它们在MongoDB中,无需更改时区,只要您始终读取它们并将其写入相同的时区。但是,当a)处理午夜的时间和b)夏令时开始(或停止)时,这充满了意想不到的错误。
或者只需使用 Joda-Time 。
I have a text file(CSV) with data in it from MySQL it has many fields and one of the fields is DateOfTransaction
with value like this "21/08/2012"
.
I have written a java program to read the CSV file and insert the document in mongodb.
code to insert date
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //set the date format
Date d2 = new SimpleDateFormat("dd/mm/yyyy").parse(arr[10]);
bookRenterLineItemInstance.setDateoftransaction(d2.getTime());
Every thing is fine in parsing adn dateobject iscorrectly created. But when i query the mongodb collection it give me record with one day behind.
example if i insert date : "21/08/2012"
saved in mongodb as : "DateOfTransaction" : ISODate("2012-01-20T18:38:00Z")
All my queries are related to this field.
Please help so that the date is not changed and saved as ISODate("2012-01-21T18:38:00Z")
for "21/08/2012"
There are two issues with what you're currently trying to do:
- You have a typo in the second line of code which means your months are being parsed as minutes
- As mentioned, you're not taking into account timezone.
To address the first problem, you need to change
Date d2 = new SimpleDateFormat("dd/mm/yyyy").parse(arr[10]);
to
Date d2 = new SimpleDateFormat("dd/MM/yyyy").parse(arr[10]);
Date formats are case sensitive, what you were trying to do is to parse the month as a **m**inute instead of a **M**onth. In the test I ran, that meant the months were all coming out as January, which I can see was happening in your example as well. Also, I notice that your first line of code, the formatter (which IS set up correctly) is not used.
The second problem is that yes, Java Date
s don't behave the way you expect. They MUST have a time component, even though you don't care about it for simply the date. In addition, they must have a timezone since they have a time.
You might not have the choice of moving to the superior Joda library, in which case there are ways to work around this. I've written a test which should demonstrate this:
@Test
public void shouldParseDateCorrectly() throws Exception {
// Given
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
// When
Date parsedDate = format.parse("21/08/2012");
// Then
Assert.assertThat(parsedDate.toString(), is("Tue Aug 21 02:00:00 CEST 2012"));
Assert.assertThat(parsedDate.toGMTString(), is("21 Aug 2012 00:00:00 GMT"));
}
So you can see that I've used the correct format, but also set its timezone to UTC (so it has an offset of zero and no daylight savings). Then when you parse the date with this formatter, the date will come out with a time that's midnight in the UTC timezone. Because my computer is in Europe, when I print this date it shows a time of 2am, because this timezone is 2 hours ahead of UTC. But when I use the deprecated toGMTString
method, the time comes out as zero.
When you store this date into MongoDB using the Java driver, it will save the date, the time and the timezone with it, even if all you care about is the date. What you'll need to do when you read the dates back out again is to remember they're in UTC and set the timezone appropriately.
Alternatively, you can store them in MongoDB without changing the timezone, provided you'll always read them in and write them out in the same timezone. But this is fraught with unexpected bugs when a) dealing with times over midnight and b) daylight savings time kicks in (or stops).
Or just use Joda-Time.
这篇关于如何通过java从String读取在mongodb中插入相同的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!