Difference in Timestamp in PHP and Java - java

I try to generate timestamp in PHP and Java(Android). Both needs to be same. But I am getting a difference of 3000+ between the time stamps. Please check the code below!
JAVA :
long unixTime = System.currentTimeMillis() / 1000L;
Php :
$time2 = time();

System.currentTimeMillis() will get me the current time in mobile and java.util.Date(); gets me based on GMT. Solved it.

Related

Difference in Server UNIX time and device current time

I get Unix timestamp from server, it can be some order creation time:
1531740385
I need to show some data, 20 minutes after this time from server.
I tried this approach:
currentUnixTime = (int) (System.currentTimeMillis() / 1000L);
timeDifference = currentUnixTime - orderaddedtime;
if(timeDifference<1160){
show();
}
But it doesn't work if i change the time in android settings or change the time zone. How to get exact time difference?

Convert Java Timestamp in TOD Format (Time of Day) on z/os

As the Search Method "readSearch" of the ZLogstream class needs the Timestamp in TOD format, I've to convert the JAVA Timestamp in TOD (Time of Date) format.
I couldn't find any class (wrapper), which does that for me. Only the getTodClock of the ZUtil class give me the current TOD (Time of Date),but I couldn't convert any timestamp in the right format.
Could anybody help me?
Thanks in advance.
This works for me in a Rexx procedure:
Tod = (epoc*4096000000)+9048018124800000000
TOD (Time-of-Day in S360, S370, S390 & z/Arch IBM's archs) is a 64bit counter whose Bit position 31 is incremented every 1.048576 seconds, starting at 1900-01-01.
So, 2**32/1.048576 = 4096000000 = 1 sec.
9048... is the difference between 1900-01-01 and epoch 1970-01-01 in TOD units.

Freemarker get current timestamp

How can I get the current timestamp (number of seconds since 01/01/1970) in a freemarker file? I know the .now var but I didn't figure how to get a basic timestamp format.
Use the ?long operator:
.now?long
for milliseconds, or
${.now?long / 1000}
for seconds

Android unix timestamp - Converter [duplicate]

This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 8 years ago.
In my android app I'm creating a timestamp this way:
final BackupInfo backupInfo = new BackupInfo(description, System.currentTimeMillis(), backupContacts.size());
eg, using System.currentTimeMillis()
Now I convert it back to date format using:
public static String getDate(long time)
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy HH:mm:ss", cal).toString();
return date;
}
And it works fine.
But now I'm receiving a timestamp from a server and the date String I receive from getDate is not the correct date.
Practical case:
My app generates this timestamp: 1403022230766
getDate returns this date: 17-06-2014 05:23:50 which is correct to my eyes.
Now the problem comes in, I get this timestamp from the server: 1403022360
getdate returns this date: 16-01-1970 18:43:42 which is totally wrong, it should be close to the timestamp generated by my app.
The timestamp returned by the server is 3 digits less in size. But if I go to an online converter, like this one and I put 1403022360 (the TS generated by the server) I get a correct date.
Can anyone explain me why this difference and what am I doing wrong in my getDate method that I can't decode the timestamp received from the server?
Your server is returning your timestamp in seconds, so multiply by 1000 to get milliseconds.
The online converters work properly because they assume that the that if the number is large enough, then it is in milliseconds and if it's short then it is in seconds.
Java/Android dates are all long types so they can hold milliseconds for additional precision.

Modify date without modifying time

With JodaTime, without using the 'plus' or 'minus' functions and using the least lines of code, how can I set a new date without modifying the time?
My first attempt was to store the 'time' parts of the DateTime in separate ints using getHoursOfDay() and getMinutesOfHour() etc - then create a new DateTime with the required date and set the hours, minutes, and seconds back again. But this method is pretty clunky, and I was wondering if there was a less verbose method for doing this - ideally with just one line of code.
For example:
22/05/2013 13:40:02 >>>> 30/08/2014 13:40:02
Is JodaTime a must? Basic way to do this is
1. extract just time from timestamp.
2. add this to just date
long timestamp = System.currentTimeMillis(); //OK we have some timestamp
long justTime = timestamp % 1000 * 60 * 60 * 24;// just tiem contains just time part
long newTimestamp = getDateFromSomeSource();//now we have date from some source
justNewDate = newTimestamp - (newTimestamp % 1000 * 60 * 60 * 24);//extract just date
result = justNewDate + justTime;
Something like this.
Previously accepted answer were removed by moderator, as it contains only link to javadoc.
Here is edited version.
You could do it like this
DateTime myDate = ...
myDate.withDate(desiredYear, desiredMonth, desiredDayOfMonth);
JavaDoc is here: DateTime.withDate(int year, int month, int dayOfMonth)
use withFields like this:
new DateTime().withFields(new LocalDate(2000,1,1))
This will set all date time fields of the DateTime to those that are contained in the LocalDate - year, month and day in this case. This will work with any ReadablePartial implementation like YearMonth for example.

Categories