1

I just need a small help regarding the DateTime format in java.I am writing a simple chat application based on yahoo messanger,in which I will read the packet of yahoo messanger and display the chat messages.Now I want to display the time from the given header.In a particular article it is said the "timestamp" will be 0x477BBA61(decimal 1199290977) which means "Wed, 2 Jan 2008 16:22:57 GMT" . I am trying to reveal how that decimal is converted to that particular date.I tried to write a simple java application to convert that and its giving some other time.

  public static void main(String[] arg)
        {
             Calendar  obj = Calendar.getInstance();
          obj.setTimeZone(TimeZone.getTimeZone("GMT"));
            obj.setTimeInMillis(1199290977l);
          System.out.println( obj.get(Calendar.HOUR)+":"+obj.get(Calendar.MINUTE));
        }

output:9:8

Can anybody help me with this?

kiddo
  • 1,486
  • 6
  • 28
  • 60

4 Answers4

4

Your value of 1199290977L is wrong. That's measuring in seconds since the Unix epoch (midnight on January 1st 1970 UTC) - you need to multiply it by 1000 to get milliseconds since the epoch.

You're also using Calendar.HOUR which is the 12-hour clock instead of Calendar.HOUR_OF_DAY which is the 24-hour clock. This code:

Calendar  obj = Calendar.getInstance();
obj.setTimeZone(TimeZone.getTimeZone("GMT"));
obj.setTimeInMillis(1199290977000L);
System.out.println(obj.get(Calendar.HOUR_OF_DAY) + ":" + 
                   obj.get(Calendar.MINUTE));

... prints 16:22.

However, you should definitely use the java.text.DateTimeFormat class instead of doing this yourself - or, ideally, use Joda Time instead.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
0

Imho to proceed you need to know:

whether or not that number is milliseconds or not
what is the starting point (in java is  January 1, 1970, 00:00:00 GMT)
Riccardo Cossu
  • 2,609
  • 1
  • 26
  • 43
0

The timezone is probably in seconds; try to multiply the value by 1000 to get the milliseconds which Calendar expects.

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
-2

You need to use a SimpleDateFormat - look at the documentation, it is pretty easy to understand, plus it includes a lot of examples (so you don't need to search for "date format tutorial" or something like that :))

EDIT: Ooops, I missed the part where you are passing the time in seconds instead of milliseconds and the result time is wrong, I misinterpreted your question and thought that you want to just parse the time, Jon's answer is better :)

qwerty
  • 1
  • 1
  • 1
    I didn't downvote, but it doesn't really answer the question. It doesn't explain why the current code gives the result it does. – Jon Skeet Apr 14 '11 at 08:51
  • @Jon - yes, I read your answer and saw what is wrong here, I didn't saw what exactly he wants (and I edited my answer). I guess I shouldn't answer on SO when I am toasted. – qwerty Apr 14 '11 at 08:54
  • @The toasted as in made a mistake because I am tired, apologized for it and really don't feel like explaining myself that much over it – qwerty Apr 14 '11 at 09:41