about summary refs log tree commit diff
path: root/html/blog/java-timezones/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'html/blog/java-timezones/index.html')
-rw-r--r--html/blog/java-timezones/index.html98
1 files changed, 98 insertions, 0 deletions
diff --git a/html/blog/java-timezones/index.html b/html/blog/java-timezones/index.html
new file mode 100644
index 0000000..2e83010
--- /dev/null
+++ b/html/blog/java-timezones/index.html
@@ -0,0 +1,98 @@
+<!doctype html>
+<html lang=en-US dir=ltr>
+<head>
+	<title>Timezones in Java</title>
+	<meta charset=UTF-8>
+	<meta name=robots content="noindex, nofollow">
+	<meta name=viewport content="width=device-width, initial-scale=1">
+	<link rel=stylesheet href=/css/terminal.css>
+	
+</head>
+
+<body>
+<!-- todo color picker -->
+
+<header>
+	<h1>Timezones in Java</h1>
+	<nav aria-label=primary>
+		
+		<ul>
+			<li>
+				<a href="/">Home</a>
+			</li>
+			<li>
+				<a href="/blog/" class="active">Blog</a>
+			</li>
+			<li>
+				<a href="https://git.starfall.systems">Git</a>
+			</li>
+		</ul>
+	</nav>
+</header>
+
+<article>
+2023-08-15<p>Recently ran into an issue at work that we couldn't find a direct answer to anywhere on the Internet (thanks to the terrible state of search in the modern day after Search Engine Optimization and Large Language Models have screwed it over, but that's another topic...) relating to three-letter abbreviations for timezones.</p>
+<p>Long story short, use canonical timezone names <a href="http://web.cs.ucla.edu/~eggert/tz/tz-link.htm">from tzdb</a> like &quot;America/New_York&quot; instead of abbreviations like &quot;ET&quot;.</p>
+<p>The abbreviations like EST, CDT, CET, BST... mostly don't work any more, and for good reasons. Is MST Malaysian Standard Time (UTC+8) or North America Mountain Standard Time (UTC-7)? They might be standardized within a given nation's borders, but not worldwide. So, if you run across an error that looks like <em>this</em> when trying to parse a date:</p>
+<pre><code class="language-java">java.time.format.DateTimeParseException: Text '01/01/1999 - 00:00:00 EDT' could not be parsed: null
+    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
+    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
+    at java.base/java.time.format.LocalDateTime.parse(LocalDateTime.java:492)
+    at [ REDACTED ]
+
+    Caused by:
+    java.lang.NullPointerException
+        at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.prefixLength(DateTimeFormatterBuilder.java:4527)
+        at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add0(DateTimeFormatterBuilder.java:4396)
+        at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add(DateTimeFormatterBuilder.java:4391)
+        at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.getTree(DateTimeFormatterBuilder.java:4138)
+        at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.parse(DateTimeFormatterBuilder.java:4249)
+        at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.parse(DateTimeFormatterBuilder.java:2370)
+        at java.base/java.time.format.DateTimeFormatter.parseUnresolved0(DateTimeFormatter.java:2107)
+        at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2036)
+        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
+        ... 3 more
+</code></pre>
+<p>... it's likely that you're trying to use a three letter abbreviation for a timezone (here, &quot;EDT&quot; being used instead of &quot;America/New_York&quot;).</p>
+<p>Confusingly, <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html">the documentation for DateTimeFormatter</a> actually includes an example of a zone-name that's a three-letter acronym! ZoneId <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZoneId.html#SHORT_IDS">has a list of them included for backwards compatibility</a> but I couldn't figure out if they're actually parseable (leaning towards no).</p>
+<p>And as an extra layer, this behavior relies on the underlying system. The abbrevations worked just fine on our work MacBook but not on the Jenkins build nodes. I don't have an answer for exactly why, but my guess is that Mac tooling happily responds with UTC as a default time zone when it doesn't know what you're asking, while GNU ones error. You can see the same kind of difference on the <code>date</code> program for each:</p>
+<pre><code class="language-zsh">[starfall@mac:~] % TZ=unknown date
+Tue Aug 15 15:28:39 UTC 2023
+
+[starfall@arch:~] % TZ=unknown date
+Tue Aug 15 03:28:39 PM unknown 2023
+</code></pre>
+<p>One solution is to just use times with offsets, but there are valid reasons to choose to use a time with timezone instead. Here's a few ways you can parse them properly.</p>
+<h3>Example 1</h3>
+<pre><code class="language-java">String stringWithTz = &quot;2023-08-15 10:28:39 America/Chicago&quot;;
+DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss VV&quot;, Locale.US);
+Instant instant = Instant.from(formatter.parse(stringWithTz));
+</code></pre>
+<p>Usually you will be able to use an Instant. These are stored without any time zone or offset, just as a moment in ... something that's close enough to UTC for most work. If the details matter, read <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Instant.html">the documentation</a>.</p>
+<p>The above Instant is 2023-08-15T15:28:39Z.</p>
+<h3>Example 2</h3>
+<pre><code class="language-java">String stringWithTz = &quot;2023-08-15 10:28:39 America/Chicago&quot;;
+DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss VV&quot;, Locale.US);
+ZonedDateTime zdt = ZonedDateTime.parse(stringWithTz, formatter);
+</code></pre>
+<p>This gets you a ZonedDateTime, which keeps the time zone information around. Usually an Instant will be fine instead, unless you really need to keep track of which datetime came from which timezone.</p>
+<p>The above ZonedDateTime is 2023-08-15T10:28:39-05:00 (the same time as the Instant in example 1).</p>
+<h3>Example 3</h3>
+<pre><code class="language-java">String isoString = &quot;2023-08-15T10:28:39&quot;;
+ZoneId timezone = ZoneId.of(&quot;America/Chicago&quot;);
+ZonedDateTime zdt = LocalDateTime.parse(isoString).atZone(timezone);
+Instant instant = Instant.from(zdt);
+</code></pre>
+<p>If you don't have time zones in your strings, you can hydrate them with one like this. Keeping the LocalDateTime without a timezone is not recommended unless you have a very good reason, like they're historical dates from one location that won't be compared across timezones.</p>
+<p>The Instant and ZonedDateTime here are the same as the previous two examples.</p>
+
+</article>
+
+<footer>
+<section>
+	<p>This site is 100% <a href=https://git.starfall.systems/web>source-available</a>. © 2020-2023 Starfall. See <a href=https://git.starfall.systems/web/tree/COPYING.md rel=license>COPYING.md</a>.
+</section>
+<div style=text-align:center>⋁/⋀</div>
+</footer>
+
+</body>