I would like to store dates in mongodb in epoch format (Unix time in secs or millisec, eg : "1433323417000") and have an object mapping in java java.util.Date Format (or joda.time.DateTime).
Can you let me know if this is possible ?
Thanks in advance
From MongoDb documentation :
Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.
https://docs.mongodb.org/manual/reference/method/Date/
You can find some examples on how to insert using the Java driver here
On a read, there is nothing stopping you from converting the date to any format you want.
Related
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 have a date string of the form: "Fri Jun 20 16:14:00 GMT+0530 2014"
As you can see there is TimeZone information in the date (GMT+0530).
But once we store this in MongoDB using mongodb 'date' datatype.
I see it is stored in a format like this:'2014-06-20 10:44:00.470Z'
How can i extract the 'original' Time Zone from this date??
(I am using Java for storing/extracting data from mongo)
The BSON Date data type that MongoDB uses is simply a 64-bit integer count of milliseconds since UTC Jan 1, 1970. So if you want to track a time stamp's original time zone you'd have to store that separately as it's lost when converted to Date.
MongoDB stores dates in ISO 8601 format. There's no straight forward way to convert to it from the Java date format, but here's a similar question, look at the accepted answer, it should give you a clue on how to do this.
I have a problem with converting NUMBER field to ORACLE date.
The problem is field holds a value 1285666505575 which consists of 13 digits.
I thought that this is standard timestamp value but current timestamp time consists of 10 digits (check it here).
The field is set from JAVA code.
I would like to convert this number to dd-mm-yyyy human format.
Could you give some suitable advises?
Thanks in advance!
With help of #Jesper i found this solution.
select TO_DATE('01/01/1970 00:00:00','DD/MM/YYYY HH24:MI:SS') + (1285666505575 /1000/60/60/24) from dual
The Unix timestamp (that your link refers to) counts the number of seconds since 01-01-1970.
Java counts the time using a number of milliseconds since 01-01-1970. So it's not a surprise that this has three digits more than the Unix timestamp.
You can pass that number to the constructor of java.util.Date. Example:
Date date = new Date(1285666505575L);
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(date));
I have a PHP web service sending JSON responses back to my Java client. One of the fields is a DateTime value. However, I'm having problems translating the serialized PHP Date/Time to a Java Date.
For example, here is a date stored in my database:
2011-12-07 15:03:01
Here is how it's encoded in the JSON response:
1323288181
I suspected this would be the milliseconds since the Unix epoch, but when I construct a Java Date with that given value, the date turns out to be the following:
Fri Jan 16 01:34:48 CST 1970
Obviously it's not milliseconds since January 1, 1970 at midnight.
How do I go about doing this?
Looks like that's seconds since the Unix epoch - so just multiply your value by 1000 when passing it to the Date constructor.
Note that Date.toString() will always use the system time zone, but a Date really represents an instant in time, so it doesn't have a time zone.
If you're doing anything significant with dates and times, I'd thoroughly recommend using Joda Time instead of the classes in java.util.
I think it is a unixtimestamp. use this online convertor: http://www.onlineconversion.com/unix_time.htm
and here are examples how to convert it (in java):
http://www.epochconverter.com/
I am using
1970-01-01T00:00:00Z
as date time format in JSON,
then I make sure both sides parse it correctly
I have a java file that write records to the DB and time stamps
I have another php file that reads that records..
unfortunately After converting the time stamp to dates I got a wrong dates ??
what is the problem !!!
Java uses a timestamp which is milliseconds from the epoch. PHP uses the standard unix timestamp which is seconds from the epoch.
I believe both use the same epoch of Jan. 1, 1970 00:00:00 UTC
PHP uses the UNIX epoch, I suspect Java uses a different epoch.
EDIT: I was way off, turns out PHP uses seconds, java uses miliseconds. So multiply by 1000 or divide by 1000 depending on which way you're converting.
I think the problem is you're retrieving a DATETIME or TIMESTAMP column or such that has been stored and screwing up the conversion. Try this:
$phpdate = strtotime( $dateFromDb );
echo date("F j, Y, g:i a", $phpdate);