I have certain object that has a date property. When i print the date value in Java i get a normal date like "11/08/2021". The thing is, when i try to check the date of my object (which is stored in mongoDB) in Robo 3T, i get a number like that : 1507037760657.
I searched and i found this kind of converter that allows to convert timeStamp format to date format. But that value : 1507037760657 is converted to some really weird date like 15/2/49726.
Is mongoDB using another timestamp format i don't know? How can i get a good date from this number ? 1507037760657
Thanks
Related
I am trying to use new SimpleDateFormat to parse a string in the format dd-MM. Basically, I want to create a date object out of the string and persist in the database.
When I checked the database entry I see that it appends 1970 to the year column. I believe it is the default value of the year provided when it is empty. Is there a way to prevent the year value. I do not want to store information about the year.
My code -
String dateOfBirth = "14-Feb";
dbObject.save(new SimpleDateFormat("dd-MMM").parse(dateOfBirth));
For the sake of simplicity, assume dbObject.save() the method expects a date object to be provided. I do not want to create a date of value - 14-Feb-1970, instead it should be just 14-Feb.
I would strongly suggest you use the java.time.MonthDay class to store your dates. If your database doesn't support storing that, you can just store it as a string, and parse it when you get it out of the database.
Here is how you would parse your date:
MonthDay md = MonthDay.parse("14-Feb", DateTimeFormatter.ofPattern("dd-MMM").withLocale(Locale.US));
You can then store the string returned by .toString into the database (it will be something like --02-14), and the next time you parse it, you don't need a date time formatter:
MonthDay md = MonthDay.parse("--02-14");
I am consuming an API that produces dates as Epoch timestamps but in string format:
{ "date":"1499762012700"}
Is there any way of getting this to go into a pojo as a Date object without writing some sort of custom serializer etc?
This works fine if the timestamp is a number but unfortunately this is the way it is given.
Is it a 9 digit timestamp? It would be helpful to have an actual example. If its unix time and do you want a java Date or a JODA object. I'd go with JODA
If its a unix 10 digit time stamp look here
Java: Date from unix timestamp
or
I'm working on JBake, and would like to get the year part of the date at which a document was stored.
I know I can get the date by doing
SELECT date FROM post
And OrientDB will return me a java.util.Date.
I know I can also add the year as another field, but I find this data duplication unsatistfying.
So, is it possible to store the date, and to to have the year extracted through some kind of SQL "function" ?
Typically I would love to do
SELECT date, {date.getYear()} FROM post
You could use the format() function using the SimpleDateFormat syntax:
select date.format('YYYY') from post
Is it possible to save date values in the database of this format dd/MM/yy in Grails? I know I can customized the format in the views but I need the values to be returned as json and also return the values of dates in json in that format. Any suggestion will be appreciated.
Try this on your presentation layer, don't save time in that format in database. use following code to format the time according to your need.
Date date = new Date( );
SimpleDateFormat simpleFormat = new SimpleDateFormat ("dd/MM/yy");
System.out.println("Date: " + simpleFormat .format(date));
But if you want to save the data in this format in databse, then remember it returns a string and you will have to save the date in String format in database. Which I wouldn't recommend because of many reason.
You should not store the date as a formatted string because you lose the ability to do many things with that field, such as sort it or compare it. Always use the database's native date format for storage. If you want to change the format there are many places to do it, including the presentation layer (as others have suggested) and the database query layer. Format the date in the query if you want to do minimal processing in Java/Javascript.
Agreed with others you should not save in the database in String format. To format a Date using Groovy you can use the String.format() method.
String.format('%tY-%<tm-%<td', new Date())
See the Groovy dates documentation for further examples.
I have a JSON file which has 40k documents, each document contains a date field. I need to query within Java with dates to retrieve data, so I stored the date in numberLong format.
Date dt = new Date();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
dt = format.parse(nextLine[j]);
document.put(ColumnNameAsKey[j], dt.getTime());
where the above code is done in for loop in an API to store data to mongo.
But after entering all those data, and then when I queried numberlong changes automatically for the same date, so that I am unable to retrieve all data for the required date. My query to retrieve is
querygraph.put("Complaint Date (MM/DD/YYYY)", new
BasicDBObject("$gte",startdate.getTime()).append("$lte",EndDate.getTime()));
for eg : if the date 08/01/2012 contains large number of document, the correct numberlong for the date 08/01/2012 is replaced in the date field in mongo. this will continue, but aftr some number of documents, the numberlong keep on changing.. ie if NumberLong is 134353300000 for the date 08/01/2012, then after 6 or more document the numberlong will be different from the former one.. causing unable to retrieve exact data for the date 08/01/2012..
What makes the difference here?
I think see your problem, because you store the millisecond precision of time you are actually getting problems with the long ints representing parts of a day making it impossible to query past, say, midnight.
This is because MongoDB querying does not take this sort of contextual querying into account.
First off a hint, don't store as number longs, store as the $date BSON type using only the lines:
Date dt = new Date();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
You will get more functionality from using the specified BSON date type and the querying ability is the same across the board.
You have the right idea about querying your records:
querygraph.put("Complaint Date (MM/DD/YYYY)", new
BasicDBObject("$gte",startdate.getTime()).append("$lte",EndDate.getTime()));
But I got a feeling you are doing something wrong. When you create the start date and the end date you are actually looking for the 00:00:00 time of the start date and the 23:59:59 time of the end date. This is due to your getTime() function, UNIX timestamp does not return partial times as such it will just return the default which is effectively now().
One way around that could make your life easier is to standardise times on this field so that you specify a time of 00:00:00 for all dates allowing you pick out ranges correctly.