In the following snippet, I want to change the time-zone used by String.format()
long mls = System.currentTimeMillis();
String time = String.format("%tT %tZ", mls, mls);
System.out.println("TIME: " + time + " \n");
If you take a look at javadoc, you'll see that the Z pattern uses your JVM's default timezone:
'Z' A string representing the abbreviation for the time zone. This value will be adjusted as necessary for Daylight Saving Time. For long, Long, and Date the time zone used is the default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any).
So, the easiest way is to change the default timezone, using the java.util.TimeZone class. Example with some random timezone:
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
long mls = System.currentTimeMillis();
String time = String.format("%tT %tZ", mls, mls);
System.out.println("TIME: " + time + " \n");
Output:
TIME: 14:03:54 BST
I don't think that's the best way to format a date, specially because it depends on the system's default timezone (which can be changed at runtime and break your code), and it uses the old API (TimeZone class).
Now Java has a new API which makes things much easier.
If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
The code below works for both.
The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.
As you want just the time (hour/minute/second), you can use the LocalTime class, and a DateTimeFormatter to customize the output format. I also used the ZoneId class, to specify the timezone:
// get current time in London
LocalTime now = LocalTime.now(ZoneId.of("Europe/London"));
// formatter for HH:mm:ss output
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(fmt.format(now)); // 14:09:22
The output is:
14:09:22
Note that I used Europe/London as timezone. Prefer to use these long names, because the short names (like CST or IST) are ambiguous and not standard.
You can get a list of all available timezones with ZoneId.getAvailableZoneIds().
If you want to output the timezone short name (like the pattern %tZ does), you'll have to use a ZonedDateTime class, because a LocalTime has no timezone information on it:
// get current date/time in London
ZonedDateTime z = ZonedDateTime.now(ZoneId.of("Europe/London"));
// formatter for "HH:mm:ss zone" output
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss z");
System.out.println(fmt.format(z)); // 14:09:22 BST
In this case, the output will be:
14:09:22 BST
You can take a look at DateTimeFormatter javadoc to see all possible formats.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With