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);
Related
I need to convert what I think is a Julian timestamp to a regular time stamp with Java.
The application that generates this timestamp is a proprietary payment system (Base24-EPS from ACI). I need to be able to pull and parse the value from the database with a Java application.
A sample timestamp value in decimal is 18 digits:
137955731472778910
With DALCI (internal tool provided by Base24-EPS), I can see this is equivalent of:
3: convert 137955731472778910 to datetime(yyyy/mm/dd hh:mm:ss);
2019/12/14 12:39:07
I found an answer here which seems to be related. But 137955731472778910 is smaller than 210866803200000000, which is the Julian timestamp for 01-JAN-1970 (epoch for unix time).
All the other Julian timestamp online converter I see, for example http://www.onlineconversion.com/julian_date.htm, have Julian date format as double 2458806.52903.
18 digits seem too long.
Do you know how can I parse this timestamp format with Java?
Many thanks.
Assuming you are in the UTC timezone (you probably aren't, but you haven't told me what timezone you are in), I have a formula:
long timestampFromAci = ...;
long timestampJava = (timestamp - 122192460002790000L) / 10000;
Instant.ofEpochMilli(timestampJava);
new Date(timestampJava); // Old, deprecated - use java.time classes
This assumes that the conversion is linear.
Your product timestamp has 10000 units per millisecond, since there are 2145052000 milliseconds between 2019/11/19 16:48:15 and 2019/12/14 12:39:07, and the difference in your product's timestamp is 21450514084700.
If you divide these two, that's almost exactly 10000 - the difference is because your tool doesn't display fractional seconds.
Extrapolating from that, I can derive that value that your product timestamp would have for the Unix epoch op 1/1/1970 - 122192460002790000.
However, as I said, I made the assumption that you are in the UTC timezone. For every hour that your timezone is off from UTC, you need to adjust that number by 3600 seconds times 10,000,000 units product timestamp units per second.
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.
I have JSON as in the picture below which I use in my android application. I have problems with parse of sunrise and sunset which in unix, UTC format. My question is how to show only local hour and minute?
Thats what I tried to sunrise but it was wrong: sunrise.setText(json.getJSONObject("sys").getString("sunrise"));
Thanks for any help!
I believe that you have a couple of issues.
The first is how to convert a Unix timestamp into a Java Date.
Like Unix timestamps, Java dates are stored internally as a long offset from a specific instant, known as the "epoch". Fortunately, they both use the same epoch. However, Unix timestamps have higher resolution.
To get a long that represents a Java date from a Unix timestamp, you need to multiply it by 1000. Then you can use the result to initialize a Java Date.
The essential thing here is to make sure that your timestamps are longs, not ints (or doubles, or any other numeric type).
So, essentially:
long unixTimestamp = 1427607706;
long javaTimestamp = unixTimestamp * 1000L;
Date date = new Date(javaTimestamp);
Next, you'll need to get the numeric value from the Json, as a long, instead of a String, so use getLong instead of getString.
Finally, you'll want to format the date. Since you want just the hours and minutes, you can use:
String sunrise = new SimpleDateFormat("hh:mm").format(date);
and then use that value to set the value of your TextView.
I'm using the epoch time format to save date. My problem is Java Long is enough to handle this or should I consider Java BigInteger to handle the epoch time?
Assuming you mean UNIX epoch, Java long is more then enough. UNIX epoch is number of seconds since January 1, 1970 and is stored (in UNIX) as a 32-bit int.
Yes, a long is sufficient. But in terms of the best way, consider using native types.
In Java <= 7, java.util.Date is designed for this purpose. It has millisecond precision.
In Java >= 8, java.time.Instant is designed for this purpose. It has nanosecond precision.
In Java you can get the milliseconds since the UNIX Epoch with System.currentTimeMillis() which returns a long, so there's no reason to consider something else.
If by epoch time, you mean seconds since 1970, long will of course do the job, as it can represent millis as well up until end of time ;-)
My point is that you can might integer instead. it will represent time in secs since 1970 up to year 2038.
If you don't need to represent time before now, consider using a special format like stated here. This will help you represent a wider future range.
Another option for representing time only after now, is starting the measure since 2021, by subtracting the seconds: nowSecs - 2021Secs.
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