{"id":4879,"date":"2022-08-24T23:02:52","date_gmt":"2022-08-24T17:32:52","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/"},"modified":"2022-08-24T23:02:52","modified_gmt":"2022-08-24T17:32:52","slug":"solved-java-lang-numberformatexception-for-input-string-2019-11-27","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/","title":{"rendered":"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-59332306\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"59332306\" data-parentid=\"59328722\" data-score=\"2\" 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<h2>LocalDate and ThreeTenABP<\/h2>\n<pre><code>    String dateString = \"2019-11-27\";\n    LocalDate date = LocalDate.parse(dateString);\n    int epochDay = (int) date.toEpochDay();\n\n    System.out.println(epochDay);\n<\/code><\/pre>\n<p>This snippet outputs:<\/p>\n<blockquote>\n<p>18227<\/p>\n<\/blockquote>\n<p>The documentation explains:<\/p>\n<blockquote>\n<p>The Epoch Day count is a simple incrementing count of days where day 0<br \/>\n  is 1970-01-01 (ISO).<\/p>\n<\/blockquote>\n<p>So my suggestion is that this number is fine for feeding into your <code>BarEntry<\/code> constructor.<\/p>\n<p><code>toEpochDay()<\/code> returns a <code>long<\/code>. If your constructor doesn\u2019t accept a <code>long<\/code>, convert to <code>int<\/code>. In the code above I did a simple cast. The risk is that we will get a very wrong result in case of <code>int<\/code> overflow for dates in the far future or the far past. I prefer to do a range check to avoid that:<\/p>\n<pre><code>    long epochDayLong = date.toEpochDay();\n    if (epochDayLong &lt; Integer.MIN_VALUE || epochDayLong &gt; Integer.MAX_VALUE) {\n        throw new IllegalStateException(\"Date \" + date + \" is out of range\");\n    }\n    int epochDay = (int) epochDayLong;\n<\/code><\/pre>\n<p>The result is the same as before. This check is the same check that the <code>Math.toIntExact<\/code> method I mentioned in a comment does (available from Android API level 24).<\/p>\n<blockquote>\n<p>I had converted this value <strong>18227<\/strong> to normal date and it gives date<br \/>\n  of this year <strong>1970\/01\/01<\/strong> and in JSON it&#8217;s <strong>2019-11-27<\/strong> why? and<br \/>\n  how should i correct it?<\/p>\n<\/blockquote>\n<p>Let me guess, you effectively did <code>new Date(18227)<\/code>. My suggestion is that you avoid the <code>Date<\/code> class completely and stick to java.time, the modern Java date and time API. Why you got 1970 is: 18227 is a count of <em>days<\/em> since the epoch, and <code>Date<\/code> counts <em>milliseconds<\/em> (since 00:00 UTC on the epoch day). So you got 00:00:18.227 UTC on that day. We already have a <code>LocalDate<\/code> in the above code, so just use that.<\/p>\n<pre><code>    System.out.println(date);\n<\/code><\/pre>\n<blockquote>\n<p>2019-11-27<\/p>\n<\/blockquote>\n<p>Should you need to convert the opposite way, it\u2019s easy when you know how:<\/p>\n<pre><code>    LocalDate convertedBack = LocalDate.ofEpochDay(epochDay);\n<\/code><\/pre>\n<p>The result is a <code>LocalDate<\/code> with the same value.<\/p>\n<h2>Question: Doesn\u2019t java.time require Android API level 26?<\/h2>\n<p>java.time works nicely on both older and newer Android devices. It just requires at least <strong>Java 6<\/strong>.<\/p>\n<ul>\n<li>In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.<\/li>\n<li>In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).<\/li>\n<li>On (older) Android use the Android edition of ThreeTen Backport. It\u2019s called ThreeTenABP. And make sure you import the date and time classes from <code>org.threeten.bp<\/code> with subpackages.<\/li>\n<\/ul>\n<h2>Links<\/h2>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.threeten.org\/threetenbp\/apidocs\/org\/threeten\/bp\/LocalDate.html#toEpochDay()\">Documentation of <code>LocalDate.toEpochDay()<\/code><\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/datetime\/\">Oracle tutorial: Date Time<\/a> explaining how to use java.time.<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=310\">Java Specification Request (JSR) 310<\/a>, where <code>java.time<\/code> was first described.<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.threeten.org\/threetenbp\/\">ThreeTen Backport project<\/a>, the backport of <code>java.time<\/code> to Java 6 and 7 (ThreeTen for JSR-310).<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/JakeWharton\/ThreeTenABP\">ThreeTenABP<\/a>, Android edition of ThreeTen Backport<\/li>\n<li>Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.<\/li>\n<\/ul><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] LocalDate and ThreeTenABP String dateString = &#8220;2019-11-27&#8221;; LocalDate date = LocalDate.parse(dateString); int epochDay = (int) date.toEpochDay(); System.out.println(epochDay); This snippet outputs: 18227 The documentation explains: The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO). So my suggestion is that this number is fine for feeding into your &#8230; <a title=\"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\" aria-label=\"More on [Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d\">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":[452,433,1131,323,356],"class_list":["post-4879","post","type-post","status-publish","format-standard","hentry","category-solved","tag-android","tag-date","tag-date-parsing","tag-java","tag-json"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d - 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-java-lang-numberformatexception-for-input-string-2019-11-27\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] LocalDate and ThreeTenABP String dateString = &quot;2019-11-27&quot;; LocalDate date = LocalDate.parse(dateString); int epochDay = (int) date.toEpochDay(); System.out.println(epochDay); This snippet outputs: 18227 The documentation explains: The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO). So my suggestion is that this number is fine for feeding into your ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-24T17:32:52+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d\",\"datePublished\":\"2022-08-24T17:32:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\"},\"wordCount\":408,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"android\",\"date\",\"date-parsing\",\"java\",\"json\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\",\"name\":\"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-24T17:32:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d\"}]},{\"@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=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d - 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-java-lang-numberformatexception-for-input-string-2019-11-27\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d - JassWeb","og_description":"[ad_1] LocalDate and ThreeTenABP String dateString = \"2019-11-27\"; LocalDate date = LocalDate.parse(dateString); int epochDay = (int) date.toEpochDay(); System.out.println(epochDay); This snippet outputs: 18227 The documentation explains: The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO). So my suggestion is that this number is fine for feeding into your ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/","og_site_name":"JassWeb","article_published_time":"2022-08-24T17:32:52+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d","datePublished":"2022-08-24T17:32:52+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/"},"wordCount":408,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["android","date","date-parsing","java","json"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/","url":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/","name":"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-24T17:32:52+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-java-lang-numberformatexception-for-input-string-2019-11-27\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] java.lang.NumberFormatException: For input string: \u201c2019-11-27\u201d"}]},{"@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=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","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\/4879","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=4879"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4879\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}