{"id":17584,"date":"2022-10-26T00:08:08","date_gmt":"2022-10-25T18:38:08","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/"},"modified":"2022-10-26T00:08:08","modified_gmt":"2022-10-25T18:38:08","slug":"solved-how-to-format-java-util-date-with-datetimeformatter-portable","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/","title":{"rendered":"[Solved] How to format java.util.Date with DateTimeFormatter portable?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-43556088\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"43556088\" data-parentid=\"43527477\" data-score=\"36\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>TL;DR: You&#8217;re right to be concerned about the use of the system local time zone, but you should have been concerned earlier in the process, when you used the system local time zone to construct a <code>Date<\/code> in the first place.<\/p>\n<p><em>If<\/em> you just want the formatted string to have the same components that <code>Date.getDate()<\/code>, <code>Date.getMonth()<\/code>, <code>Date.getYear()<\/code> etc return then your original code is appropriate:<\/p>\n<pre><code>LocalDateTime ldt = LocalDateTime.ofInstant(in.toInstant(), ZoneId.systemDefault());\n<\/code><\/pre>\n<p>You say you&#8217;re &#8220;afraid that usage of <code>ZoneId.systemDefault()<\/code> can introduce some changes&#8221; &#8211; but that&#8217;s precisely what <code>Date.getDate()<\/code> etc use.<\/p>\n<p><code>Date<\/code> doesn&#8217;t have any kind of &#8220;dual contract&#8221; that lets you view it as a time-zone-less representation. It <em>is<\/em> just an instant in time. Almost every single method that lets you construct or deconstruct it into components is clearly documented to use the system default time zone, just like your use of <code>ZoneId.systemDefault()<\/code>. (One notable exception is the <code>UTC<\/code> method.)<\/p>\n<p>Implicitly using the system default time zone is <em>not<\/em> the same as <code>Date<\/code> being a valid time-zone-less representation, and it&#8217;s easy to demonstrate why: it can lose data, very easily. Consider the time-zone-free date and time of &#8220;March 26th 2017, 1:30am&#8221;. You may well want to be able to take a text representation of that, parse it, and then later reformat it. If you do that in the Europe\/London time zone, you&#8217;ll have problems, as demonstrated below:<\/p>\n<pre><code>import java.util.*;\nimport java.time.*;\nimport java.time.format.*;\n\npublic class Test {\n\n    public static void main(String[] args) {\n        TimeZone.setDefault(TimeZone.getTimeZone(\"Europe\/London\"));\n        Date date = new Date(2017 - 1900, 3 - 1, 26, 1, 30);\n\n        Instant instant = date.toInstant();\n        ZoneId zone = ZoneId.systemDefault();\n        LocalDateTime ldt = LocalDateTime.ofInstant(instant, zone);\n        System.out.println(ldt); \/\/ Use ISO-8601 by default\n    }\n}\n<\/code><\/pre>\n<p>The output is 2017-03-26T02:30. It&#8217;s not that there&#8217;s an off-by-one error in the code &#8211; if you change it to display 9:30am, that will work just fine.<\/p>\n<p>The problem is that 2017-03-26T01:30 <em>didn&#8217;t exist in the Europe\/London time zone<\/em> due to DST &#8211; at 1am, the clock skipped forward to 2am.<\/p>\n<p>So if you&#8217;re happy with that sort of brokenness, then sure, use <code>Date<\/code> and the system local time zone. Otherwise, don&#8217;t try to use <code>Date<\/code> for this purpose.<\/p>\n<p>If you absolutely <em>have<\/em> to use <code>Date<\/code> in this broken way, using methods that have been deprecated for about 20 years because they&#8217;re misleading, but you&#8217;re able to change the system time zone, then change it to something that doesn&#8217;t have &#8211; and never has had &#8211; DST. UTC is the obvious choice here. At that point, you can convert between a local date\/time and <code>Date<\/code> without losing data. It&#8217;s still a bad use of <code>Date<\/code>, which <em>is<\/em> just an instant in time like <code>Instant<\/code>, but at least you won&#8217;t lose data.<\/p>\n<p>Or you could make sure that whenever you construct a <code>Date<\/code> from a local date\/time, you use UTC to do the conversion, of course, instead of the system local time zone&#8230; whether that&#8217;s via the <code>Date.UTC<\/code> method, or by parsing text using a <code>SimpleDateFormat<\/code> that&#8217;s in UTC, or whatever it is. Unfortunately you haven&#8217;t told us anything about where your <code>Date<\/code> value is coming from to start with&#8230;<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">14<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to format java.util.Date with DateTimeFormatter portable? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] TL;DR: You&#8217;re right to be concerned about the use of the system local time zone, but you should have been concerned earlier in the process, when you used the system local time zone to construct a Date in the first place. If you just want the formatted string to have the same components that &#8230; <a title=\"[Solved] How to format java.util.Date with DateTimeFormatter portable?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\" aria-label=\"More on [Solved] How to format java.util.Date with DateTimeFormatter portable?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[433,323,4351],"class_list":["post-17584","post","type-post","status-publish","format-standard","hentry","category-solved","tag-date","tag-java","tag-localdate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to format java.util.Date with DateTimeFormatter portable? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to format java.util.Date with DateTimeFormatter portable? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] TL;DR: You&#8217;re right to be concerned about the use of the system local time zone, but you should have been concerned earlier in the process, when you used the system local time zone to construct a Date in the first place. If you just want the formatted string to have the same components that ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-25T18:38:08+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to format java.util.Date with DateTimeFormatter portable?\",\"datePublished\":\"2022-10-25T18:38:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\"},\"wordCount\":489,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"date\",\"java\",\"localdate\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\",\"name\":\"[Solved] How to format java.util.Date with DateTimeFormatter portable? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-25T18:38:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to format java.util.Date with DateTimeFormatter portable?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to format java.util.Date with DateTimeFormatter portable? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to format java.util.Date with DateTimeFormatter portable? - JassWeb","og_description":"[ad_1] TL;DR: You&#8217;re right to be concerned about the use of the system local time zone, but you should have been concerned earlier in the process, when you used the system local time zone to construct a Date in the first place. If you just want the formatted string to have the same components that ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/","og_site_name":"JassWeb","article_published_time":"2022-10-25T18:38:08+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to format java.util.Date with DateTimeFormatter portable?","datePublished":"2022-10-25T18:38:08+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/"},"wordCount":489,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["date","java","localdate"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/","name":"[Solved] How to format java.util.Date with DateTimeFormatter portable? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-25T18:38:08+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-format-java-util-date-with-datetimeformatter-portable\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to format java.util.Date with DateTimeFormatter portable?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17584"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17584\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}