2

So I'm kinda new to the Calendar/Time/Date stuff in Java but I've read a lot about them on the net.

Here is what I have to do:

I have an external device which sends an Avl Data Packet to my Communication Server and I'm on the parsing process of the Data part.

Somewhere in the Data Packet the device sends a timestamp of 32 bits which I have to parse/translate into the time the Record of the point from the GPS was saved.

The timestamp gives me seconds from 2007.01.01 00:00 UTC

Now here is a sample code that I felt that was the closest one I tried of the rest of the experiments..

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");

    long now = (long)(TimeStampSeconds.longValue() * 1000);

    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2007, 0, 1);
    calendar.setTimeInMillis(now);

    System.out.println(now + " = " + formatter.format(calendar.getTime()));

After that I found out that the calendar.set doesnt make a new epoch and so the .setTimeInMills doesnt work.Though I get some crazy results like:

Binary Timestamp is : 0000101011000001010110001111011100001111

SECONDS: 46193506063

46193506063000 = 25/10/3433 04:27:43.000

Shouldn't I just be missing just the 37 years between 1970 and 2007??

I want to find a way of finding the time from the seconds I get from the device but they have epoch 1/1/2007 and java has epoch 1/1/1970..

EDIT: What I want is to have time:1/1/2007 PLUS the timestamp's time. Hope I clarified the question a bit..

Someone any ideas?? Thx in Advance

CipherDarkness
  • 214
  • 1
  • 8
  • 18

4 Answers4

1

It seems what you want to do is just to add your now value to the Calendar. This is easily done:

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");

long now = (long)(TimeStampSeconds.longValue() * 1000);

Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(2007, 0, 1);
calendar.setTimeInMillis(calendar.getTimeInMillis() + now);

System.out.println(now + " = " + formatter.format(calendar.getTime()));

EDIT

Something is weird regarding what your now variable holds. 46193506063 seconds corresponds to 1464.786468258 years according to this time converter.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • @CipherDarkness: I misunderstood the question. Updated to (hopefully) answer the real question. – Keppil Sep 20 '12 at 14:06
  • Doing that gives me the the current time plus the timestamp's time. What I want is to have time:1/1/2007 plus the timestamp's time. Hope I clarified the question a bit.. – CipherDarkness Sep 20 '12 at 14:09
  • 1
    @CipherDarkness: My code does exactly that. Are you sure the `now` variable contains what you think it does? – Keppil Sep 20 '12 at 14:14
  • @CipherDarkness: Added more info. – Keppil Sep 20 '12 at 14:20
  • This code is wrong, see my answer for the shortest (and correct) answer. – Tim Lamballais Sep 20 '12 at 14:24
  • I have an example from the Device's manual which is: 1101111111001110110110000 = 29334960 sec from 2007.01.01 00:00 29334960 sec = 2007.12.06 12:36:00 UTC I tried the number given and gives me indeed the right date it should! So i'm taking tha right bits from the data..I'm confused :S – CipherDarkness Sep 20 '12 at 14:30
  • @TimLamballais: There is nothing wrong with this code. There might be a better way to write it, but this is efficient and works. – Keppil Sep 20 '12 at 14:31
  • @TimLamballais `This code is wrong, see my answer...`, this is not cool. How is it wrong? If so, what should be done to make it right? – UmNyobe Sep 20 '12 at 14:33
  • Guys indeed there was a bug in the other code given,it was reading a byte more than it should! The new number is 180443383 and Keppil's code worked indeed!Thx a lot! Do you guys have any idea how can i fix the local time issue though? – CipherDarkness Sep 20 '12 at 14:42
  • @CipherDarkness: Not sure what you mean with local time issue. Maybe you should post that as a new question? – Keppil Sep 20 '12 at 16:32
  • Sorry guys I read the code the wrong way. My apologies. It can still be written a lot more elegantly though. – Tim Lamballais Sep 20 '12 at 17:45
  • @Keppil: I mean that Java gives me the time in UTC-GMT but I need it to be in local time (GMT +2).I could just add +2 to the hours but DST messes all up since for some months of the year I have to add +3 instead of +2...Do you know any way I can apply the local time zone on the result here?? – CipherDarkness Sep 21 '12 at 07:17
  • @CipherDarkness: [This question](http://stackoverflow.com/q/230126/1343161) has a couple of useful tips on converting between timezones. – Keppil Sep 21 '12 at 07:47
  • @Keppil: Ok I found it.Thx a lot for all the help! – CipherDarkness Sep 21 '12 at 11:16
1

Assuming you read the input in a timestamp long variable, I'd do something like:

Calendar theirEpoch = Calendar.getInstance();
theirEpoch.set(2007, 0, 1);
Calendar myEpoch = Calendar.getInstance();
myEpoch.set(1970, 0, 1);
long difference = myEpoch.getTimeInMillis() - theirEpoch.getTimeInMillis();
Calendar result = Calendar.getInstance();
result.setTimeInMillis(timestamp + difference);

I didn't test it, but it should give you the idea. Note also that I didn't take time zones into account.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
0

The easiest thing to do is to use the roll() method of Calendar after setting the time in milliseconds:

calendar.roll(Calendar.YEAR, 37) --> just adds 37 years to your date ;-)

But I think your input data is wrong : if you take the number of seconds that have past since 01/01/2007 until now , it should be about 200 million and not 40 billion like in your example ...

jeroen_de_schutter
  • 1,843
  • 1
  • 18
  • 21
  • I have an example from the Device's manual which is: 1101111111001110110110000 = 29334960 sec from 2007.01.01 00:00 29334960 sec = 2007.12.06 12:36:00 UTC I tried the number given and gives me indeed the right date it should! So i'm taking tha right bits from the data.. – CipherDarkness Sep 20 '12 at 14:30
  • OK, my bad. But nevertheless: the amount of seconds in the manual's example is 1000 less than the amount in your input data example. Your manual says you're getting the time in seconds, but I think it is actually milliseconds ... – jeroen_de_schutter Sep 20 '12 at 14:38
  • There was indeed a bug on the code,the new number is 180443383 and it worked.Thanks for your time! :) – CipherDarkness Sep 20 '12 at 14:39
0

This is the correct way to do what you want:

Calendar cal = Calendar.getInstance();
cal.set(2007, 1, 1, 0 ,0 ,0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.MILLISECOND, theirEpoch);

If their epoch is in seconds, change the last MILISECOND to SECOND.

Tim Lamballais
  • 1,056
  • 5
  • 10
  • This code is correct too with a change though.You got to set the month to 0 because that's the first months of the year. I indeed changed to SECOND the last and I believe cal.set(Calendar.MILLISECOND, 0); Is not needed since I don't need milliseconds in the timestamp anyways. All in all indeed a faster way to do it and with an int instead of a long.Thanks for your time! – CipherDarkness Sep 21 '12 at 07:26