vishesh
vishesh

Reputation: 2045

javax mail getReceivedDate in sender's time zone

I want to get the received date in email sender's timezone. is that possible with javax mail ? The message.getReceivedDate() returns date in my server's timezone.

Upvotes: 3

Views: 2305

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149145

As far as I know, and at least in Java 7, the Date class has (by itsel) no notion of an original time zone. So if you want to have access to the original time zone of a mail, you should directly get the Date header : message.getHeader("Date").

According to RFC2822, this header must allways be present, must contain a timezone, and should be expressed in local time. So you have to parse the value and extract the sender timezone. Next you only have to convert the getReceivedDate to that time zone.

Extracts from RFC2822 (for the parsing of Date header) :

orig-date       =       "Date:" date-time CRLF
date-time       =       [ day-of-week "," ] date FWS time [CFWS]
day-of-week     =       ([FWS] day-name) / obs-day-of-week
day-name        =       "Mon" / "Tue" / "Wed" / "Thu" /
                        "Fri" / "Sat" / "Sun"
date            =       day month year
year            =       4*DIGIT / obs-year
month           =       (FWS month-name FWS) / obs-month
month-name      =       "Jan" / "Feb" / "Mar" / "Apr" /
                        "May" / "Jun" / "Jul" / "Aug" /
                        "Sep" / "Oct" / "Nov" / "Dec"
day             =       ([FWS] 1*2DIGIT) / obs-day
time            =       time-of-day FWS zone
time-of-day     =       hour ":" minute [ ":" second ]
hour            =       2DIGIT / obs-hour
minute          =       2DIGIT / obs-minute
second          =       2DIGIT / obs-second
zone            =       (( "+" / "-" ) 4DIGIT) / obs-zone

Upvotes: 3

Related Questions