How to get timestamp of incoming xmpp message?

Timestamp is not part of the regular messages. Therefore you cannot extract it.

There is a spec (as mentioned by @Dmitry), but this only applies to specific types of messages. Typically those that are NOT realtime, such as offline and publishing the last pubsub message of a node when a new subscription is created. These are messages with an inherent delay built into them.


It also works for 4.1.9

DelayInformation inf = null;
                    inf = (DelayInformation)message.getExtension(DelayInformation.ELEMENT,DelayInformation.NAMESPACE);
                    if (inf != null){
                        Date date = inf.getStamp();
                        System.out.println("date: "+date);
                    }

Due to specs time is required attribute for XMPP message:

http://xmpp.org/extensions/xep-0203.html#protocol

Check the <delay item of <message:

<delay xmlns='urn:xmpp:delay'
 from='capulet.com'
 stamp='2002-09-10T23:08:25Z'>
Offline Storage
</delay> 

But getting it looks a bit tricky. As soon as aSmack is recompiled Smack with some replaced stuff, so try to get it the way like here:

http://edwin.baculsoft.com/2011/06/how-to-get-offline-messages-timestamp-on-openfire/

DelayInformation inf = null;
try {
    inf = (DelayInformation)packet.getExtension("x","jabber:x:delay");
} catch (Exception e) {
    log.error(e);
}
// get offline message timestamp
if(inf!=null)
    Date date = inf.getStamp();

Problably, you will need to check what server sends with message as extension value and replace "jabber:x:delay" with 'urn:xmpp:delay' as it is shown in XMPP specs example.

But not sure if it works.


Although in part a blend of the other answers already here, I would present the answer as follows...

XMPP is "realtime"... although that term can mean a lot of different things to different people, in general you can safely assume that you receive messages (almost) as soon as they're sent - all XMPP software is designed around fulfilling this goal.

However there is no actual guarantee about the end-to-end latency. In general practice you'll typically always receive a message far less than a second after it was transmitted by the sender, though network conditions can affect this.

In some cases the message will be temporarily delayed, such as when a new server-to-server connection needs to be established. This can take up to a few seconds generally, depending again on network conditions and the method of authentication used between the servers.

A message may also be held up if the recipient is offline - the recipient's server may hold the message in an "offline message" store, until it can deliver it when the the recipient comes online.

Because of the general instant delivery of XMPP messages, timestamps are generally not included in the message itself, as that information is useless (and assumes clocks are all correct).

In cases where the message is knowingly delayed though, such as in the examples above, the entity that delays the message may insert a tag into the message indicating the original time of the message. This is detailed in XEP-0203: Delayed Delivery.