我想在电子邮件发件人的时区收到收到的日期。用javax邮件可以吗?
message.getReceivedDate()
会在我服务器的时区中返回日期。
答案 0 :(得分:3)
据我所知,至少在Java 7中,Date
类(通过itsel)没有原始时区的概念。因此,如果您想要访问邮件的原始时区,则应直接获取Date
标题:message.getHeader("Date")
。
根据RFC2822,此标头必须始终存在,必须包含时区,并且应以本地时间表示。因此,您必须解析该值并提取发件人时区。接下来,您只需将getReceivedDate
转换为该时区。
RFC2822的摘录(用于解析Date标头):
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