I need to store the timezone an email was sent from. Which is the best way to extract it from the email's 'Date:' header (an RFC822 date)? And what is the recommended format to store it in the database (I'm using hibernate)?
我需要存储发送电子邮件的时区。哪个是从电子邮件的“日期:”标题(RFC822日期)中提取它的最佳方法?什么是将其存储在数据库中的推荐格式(我正在使用hibernate)?
5 个解决方案
#1
1
Probably easiest to parse with JodaTime as it supports ISO8601 see Date and Time Parsing and Formatting in Java with Joda Time.
可能最容易解析JodaTime,因为它支持ISO8601,使用Joda Time查看Java中的日期和时间解析和格式化。
DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
System.out.println(parser2.parseDateTime(your_date_string));
Times must always be stored in UTC (GMT) with a timezone - i.e. after parsing convert from the timezone to GMT and remove daylight savings offset and save the original timezone.
时间必须始终以UTC(GMT)存储时区 - 即解析从时区转换为GMT并删除夏令时偏移并保存原始时区。
You must store the date with the timezone after converting to UTC.
转换为UTC后,您必须将日期与时区一起存储。
If you remove or don't handle the timezone it will cause problems when dealing with data that has come from a different timezone.
如果删除或不处理时区,则在处理来自不同时区的数据时会导致问题。
#2
0
Extract the data from the header using some sort of substring or regular expression. Parse the date with a SimpleDateFormatter to create a Date object.
使用某种子字符串或正则表达式从标头中提取数据。使用SimpleDateFormatter解析日期以创建Date对象。
#3
0
The timezone in the email will not show in which timezone it was send. Some programs use ever UTC or GMT. Of course the time zone is part of the date time value and must also be parse.
电子邮件中的时区不会显示发送的时区。有些程序使用UTC或GMT。当然,时区是日期时间值的一部分,也必须进行解析。
Why do you want know it. - Do you want normalize the timestamp? Then use a DateFormat for parsing it. - Do you want detect the timezome of the user that send the email? This will not correctly work.
你为什么想知道它。 - 你想要标准化时间戳吗?然后使用DateFormat进行解析。 - 您想检测发送电子邮件的用户的时间表吗?这将无法正常工作。
#4
0
It looks like you already mentioned this in one of your comments, but I think it's your best answer. The JavaMail library contains RFC822 Date header parsing code in javax.mail.internet.MailDateFormat
. Unfortunately it doesn't expose the TimeZone parsing directly, so you will need to copy the necessary code directly from javax.mail.internet.MailDateParser
, but it's worth taking advantage of the careful work already done.
看起来你已经在你的一条评论中提到了这一点,但我认为这是你最好的答案。 JavaMail库包含javax.mail.internet.MailDateFormat中的RFC822 Date头解析代码。不幸的是,它没有直接暴露TimeZone解析,因此您需要直接从javax.mail.internet.MailDateParser复制必要的代码,但是值得利用已经完成的仔细工作。
As for storing it, the parser will give you the date as an offset, so you should be able to store it just fine as an int
(letting Hibernate translate that to your database for you).
至于存储它,解析器会给你一个偏移量的日期,所以你应该能够将它作为一个int存储好(让Hibernate为你的数据库转换它)。
#5
0
I recommend you use Mime4J.
我建议你使用Mime4J。
The library is designed for parsing all kinds of email crap. For parsing dates you would use its DateTimeParser.
该库旨在解析各种电子邮件垃圾。对于解析日期,您将使用其DateTimeParser。
int zone = new DateTimeParser(new StringReader("Fri, 27 Jul 2012 09:13:15 -0400")).zone();
After that I usually convert the datetimes to Joda's DateTime. Don't use SimpleDateFormatter as will not cover all the cases for RFC822.
之后我通常将日期时间转换为Joda的DateTime。不要使用SimpleDateFormatter,因为它不会涵盖RFC822的所有情况。
Below will get you the Joda TimeZone (from the int zone above) which is superior to Java's TZ.
下面将为您提供Joda TimeZone(来自上面的int区域),它优于Java的TZ。
// Stupid hack in case the zone is not in [-+]zzzz format
final int hours;
final int minutes;
if (zone > 24 || zone < -24 ) {
hours = zone / 100;
minutes = minutes = Math.abs(zone % 100);
}
else {
hours = zone;
minutes = 0;
}
DateTimeZone.forOffsetHoursMinutes(hours, minutes);
Now the only issue is that the Time Zone you will get always be a numeric time zone which may still not be the correct time zone of the user sending the email (assuming the mail app sent the users TZ and not just UTC).
现在唯一的问题是时区你将永远是一个数字时区,可能仍然不是用户发送电子邮件的正确时区(假设邮件应用程序发送用户TZ而不仅仅是UTC)。
For example -0400 is not EDT (ie America/New_York) because it does not take Daylight savings into account.
例如-0400不是EDT(即America / New_York),因为它不考虑夏令时。
#1
1
Probably easiest to parse with JodaTime as it supports ISO8601 see Date and Time Parsing and Formatting in Java with Joda Time.
可能最容易解析JodaTime,因为它支持ISO8601,使用Joda Time查看Java中的日期和时间解析和格式化。
DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
System.out.println(parser2.parseDateTime(your_date_string));
Times must always be stored in UTC (GMT) with a timezone - i.e. after parsing convert from the timezone to GMT and remove daylight savings offset and save the original timezone.
时间必须始终以UTC(GMT)存储时区 - 即解析从时区转换为GMT并删除夏令时偏移并保存原始时区。
You must store the date with the timezone after converting to UTC.
转换为UTC后,您必须将日期与时区一起存储。
If you remove or don't handle the timezone it will cause problems when dealing with data that has come from a different timezone.
如果删除或不处理时区,则在处理来自不同时区的数据时会导致问题。
#2
0
Extract the data from the header using some sort of substring or regular expression. Parse the date with a SimpleDateFormatter to create a Date object.
使用某种子字符串或正则表达式从标头中提取数据。使用SimpleDateFormatter解析日期以创建Date对象。
#3
0
The timezone in the email will not show in which timezone it was send. Some programs use ever UTC or GMT. Of course the time zone is part of the date time value and must also be parse.
电子邮件中的时区不会显示发送的时区。有些程序使用UTC或GMT。当然,时区是日期时间值的一部分,也必须进行解析。
Why do you want know it. - Do you want normalize the timestamp? Then use a DateFormat for parsing it. - Do you want detect the timezome of the user that send the email? This will not correctly work.
你为什么想知道它。 - 你想要标准化时间戳吗?然后使用DateFormat进行解析。 - 您想检测发送电子邮件的用户的时间表吗?这将无法正常工作。
#4
0
It looks like you already mentioned this in one of your comments, but I think it's your best answer. The JavaMail library contains RFC822 Date header parsing code in javax.mail.internet.MailDateFormat
. Unfortunately it doesn't expose the TimeZone parsing directly, so you will need to copy the necessary code directly from javax.mail.internet.MailDateParser
, but it's worth taking advantage of the careful work already done.
看起来你已经在你的一条评论中提到了这一点,但我认为这是你最好的答案。 JavaMail库包含javax.mail.internet.MailDateFormat中的RFC822 Date头解析代码。不幸的是,它没有直接暴露TimeZone解析,因此您需要直接从javax.mail.internet.MailDateParser复制必要的代码,但是值得利用已经完成的仔细工作。
As for storing it, the parser will give you the date as an offset, so you should be able to store it just fine as an int
(letting Hibernate translate that to your database for you).
至于存储它,解析器会给你一个偏移量的日期,所以你应该能够将它作为一个int存储好(让Hibernate为你的数据库转换它)。
#5
0
I recommend you use Mime4J.
我建议你使用Mime4J。
The library is designed for parsing all kinds of email crap. For parsing dates you would use its DateTimeParser.
该库旨在解析各种电子邮件垃圾。对于解析日期,您将使用其DateTimeParser。
int zone = new DateTimeParser(new StringReader("Fri, 27 Jul 2012 09:13:15 -0400")).zone();
After that I usually convert the datetimes to Joda's DateTime. Don't use SimpleDateFormatter as will not cover all the cases for RFC822.
之后我通常将日期时间转换为Joda的DateTime。不要使用SimpleDateFormatter,因为它不会涵盖RFC822的所有情况。
Below will get you the Joda TimeZone (from the int zone above) which is superior to Java's TZ.
下面将为您提供Joda TimeZone(来自上面的int区域),它优于Java的TZ。
// Stupid hack in case the zone is not in [-+]zzzz format
final int hours;
final int minutes;
if (zone > 24 || zone < -24 ) {
hours = zone / 100;
minutes = minutes = Math.abs(zone % 100);
}
else {
hours = zone;
minutes = 0;
}
DateTimeZone.forOffsetHoursMinutes(hours, minutes);
Now the only issue is that the Time Zone you will get always be a numeric time zone which may still not be the correct time zone of the user sending the email (assuming the mail app sent the users TZ and not just UTC).
现在唯一的问题是时区你将永远是一个数字时区,可能仍然不是用户发送电子邮件的正确时区(假设邮件应用程序发送用户TZ而不仅仅是UTC)。
For example -0400 is not EDT (ie America/New_York) because it does not take Daylight savings into account.
例如-0400不是EDT(即America / New_York),因为它不考虑夏令时。