TL;DR: To support "UTC"
prefix, use ZoneId
. See end of answer.
If you read the documentation, i.e. the javadoc of TimeZone.getTimeZone(String ID)
, it says:
Gets the TimeZone
for the given ID.
Parameters:
ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.
Returns:
the specified TimeZone, or the GMT zone if the given ID cannot be understood.
So lets test that:
System.out.println(TimeZone.getTimeZone("UTC-1"));
Output
sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
The javadoc did say that GMT-8:00
is valid, so lets test that:
System.out.println(TimeZone.getTimeZone("GMT-1"));
Output
sun.util.calendar.ZoneInfo[id="GMT-01:00",offset=-3600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
If you read the javadoc of TimeZone
itself:
If the time zone you want is not represented by one of the supported IDs, then a custom time zone ID can be specified to produce a TimeZone. The syntax of a custom time zone ID is:
CustomID:
GMT Sign Hours : Minutes
GMT Sign Hours Minutes
GMT Sign Hours
Sign: one of
+ -
Hours:
Digit
Digit Digit
Minutes:
Digit Digit
Digit: one of
0 1 2 3 4 5 6 7 8 9
As you can see, custom time zones must start with GMT
.
The new Java 8 Time API supports UTC-1
, e.g.
System.out.println(ZoneId.of("UTC-1"));
Output
UTC-01:00
Which means that if your actualDate
value is a java.util.Date
, then you can format it like this:
Date actualDate = new Date();
System.out.println(actualDate);
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss z")
.withZone(ZoneId.of("UTC-1"));
System.out.println(dateFormat.format(actualDate.toInstant()));
Output
Mon Dec 30 02:29:41 EST 2019
2019-12-30 06:29:41 UTC-01:00
As you can see, the time was adjusted to the UTC-01:00
time zone.