Convert time as a string containing time zone names ('z') for UTC time

I want to convert strings like "20000603163334 GST"or "20000603163334 -0300"to UTC. The problem is that the time zones in my lines can be "time zones", I mean, they can be lines like CET, GST, etc. And I do not know how to convert them.

Because of these string time zones, I cannot use Joda Time DateTimeFormat.forPattern("yyyyMMddhhmmss z").withZone(DateTimeZone.UTC);, because according to the documentation: Time zone names ('z') cannot be parsed .

So, I have one question, if you know a method to get around this limitation in Joda Time? I would prefer to use Joda Time, if possible, instead of the standard Java API.

Another in which I thought I could solve this problem with timezone names is using Java SimpleDateFormat. So I am doing something like:

SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss z");
//f.setTimeZone(TimeZone.getTimeZone("UTC"));
f.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("UTC")));
Date time = f.parse("20000603163334 GST");

SimpleDateFormatanalyzes String(I don’t care about the problem that there are several time zones with the same name - that this class analyzes it well for me).

The problem is that I do not know how to convert it from UTC. How can i do this?

The fact that I set the time zone f'sto UTC (as in two ways from above) does not help. I hope someone can help me fix this, I read a lot of questions and answers on this topic here in stackoverflow, but have not yet found a solution.

+3
source share
1 answer

. UTC:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

, .

, , SimpleDateFormat .

SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss z");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
Date time = f.parse("20000603163334 GST");
System.out.println(time);
System.out.println("(yyyyMMddhhmmss z):  " + f.format(time));

SimpleDateFormat utc = new SimpleDateFormat("yyyyMMddhhmmss z");
utc.setTimeZone(TimeZone.getTimeZone("UTC"));

System.out.println("(yyyyMMddhhmmss z):  " + utc.format(time));

SimpleDateFormat UTC Time. :

03 08:33:34 EDT 2000

(yyyyMMddhhmmss z): 20000603043334 GST

(yyyyMMddhhmmss z): 20000603123334 UTC

, 3- . TimeZone (http://download.oracle.com/javase/6/docs/api/java/util/TimeZone.html) JavaDoc. , , .

. JDK 1.1.x, ( "PST", "CTT", "AST" ) . , (, "" . " " " " ), Java .

+6

Source: https://habr.com/ru/post/1794918/


All Articles