javadatetimeyahoo-messenger

Convertion of DateTimeFormat in java!


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?


Solution

  • 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.