1

I want to convert the dates which are available in the mail headers in timezones like PST or PDT to the UTC format using java.

For example:

Tue, 21 Apr 2015 07:12:18 -0700 (PDT). This is the date which will be in the string variable. I want to convert this date into the UTC format.This date is not the current system date. Please suggest me the solution of the problem in java.

  • Please show, what you have so far! ..also consider reading [this](http://stackoverflow.com/a/308689/592355) and [this](http://stackoverflow.com/a/20238531/592355) answers.. – xerx593 Apr 21 '15 at 15:36
  • possible duplicate of [How can I get the current date and time in UTC or GMT in Java?](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java) – Basil Bourque Apr 21 '15 at 17:03

3 Answers3

1

Assuming you're using the MimeMessage getSentDate or getReceivedDate methods, you'll get a Date object that's already properly converted. You just need to format and display the date however you want, e.g., using SimpleDateFormat.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
1

Obsolete format

Your example data is odd. As I recall the old email standards use RFC 1123 or RFC 822. Your format is similar, but missing the colon between hour and minute of the offset, and has extraneous parens around the pseudo-zone.

Be aware that such formats are obsolete. They are difficult to parse by machine. And they assume English language and particular cultural norms. Modern protocols adopted ISO 8601 formats instead. The ISO 8601 formats are used by default in the java.time classes.

java.time

If you really must parse text in that specific format, define a DateTimeFormatter to match its formatting pattern.

String input = "Tue, 21 Apr 2015 07:12:18 -0700 (PDT)";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE, dd MMM uuuu HH:mm:ss XXXX '('z')'" ).withLocale( Locale.US );

Parse as a ZonedDateTime.

ZonedDateTime zdt = ZonedDateTime.parse( input , f );

zdt.toString(): 2015-04-21T07:12:18-07:00[America/Los_Angeles]

Note that PDT is not a real time zone names. Never use these 2-4 letter pseudo-zones. Real time zones have a name in Continent/Region format such as America/Los_Angeles.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I think I’d consider the offset more reliable than the three-letter time zone abbreviaiton and therefore parse into an `OffsetDateTIme` rather then a `ZonedDateTime`. There are pros and cons. – Ole V.V. Oct 28 '19 at 04:22
0

For that conversion, you will need a good time library that takes into consideration daylight saving time, etc. A great one for java is Joda. The library permits a variety of formatting options, for both inputs and outputs, to solve your problem. Look at the documentation for samples.

Daniel Langdon
  • 5,899
  • 4
  • 28
  • 48
  • FYI: The [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), its creator, [Stephen Colebourne](https://stackoverflow.com/users/38896/jodastephen), having gone on to lead [JSR 310](https://jcp.org/en/jsr/detail?id=310) defining the [*java.time*](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) classes built into Java 8+. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Oct 27 '19 at 21:50