{"id":12462,"date":"2022-09-30T20:22:49","date_gmt":"2022-09-30T14:52:49","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/"},"modified":"2022-09-30T20:22:49","modified_gmt":"2022-09-30T14:52:49","slug":"solved-how-can-i-add-one-month-to-change-into-the-milliseconds","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/","title":{"rendered":"[Solved] How can I add one month to change into the milliseconds?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48843782\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48843782\" data-parentid=\"48768874\" 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=\"js-endorsements\" data-for-answer=\"48843782\">\n<\/div>\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I suggest: First, instead of your variables (fields?) <code>year<\/code>, <code>month<\/code>, <code>day<\/code>, <code>hours<\/code> and <code>minutes<\/code> just declare<\/p>\n<pre><code>private LocalDate date;\nprivate LocalTime time;\nprivate long milliseconds;\n<\/code><\/pre>\n<p>(Keep the <code>milliseconds<\/code> variable since you will want to have your result here.)<\/p>\n<p>In <code>onDateSet<\/code> assign a value to <code>date<\/code> in this way:<\/p>\n<pre><code>        date = LocalDate.of(selectedYear, selectedMonth + 1, selectedDate);\n<\/code><\/pre>\n<p>This is where we are taking into account, as you said, that the date picker\u2019s <code>selectedMonth<\/code> is 0-based, while <code>LocalDate<\/code> numbers months the way humans do, from 1.<\/p>\n<p>Now your <code>timePicker<\/code> method becomes<\/p>\n<pre><code>private void timePicker(){\n    \/\/ Get Current Time\n    time = LocalTime.now(ZoneId.systemDefault());\n    \/\/ Launch Time Picker Dialog\n    TimePickerDialog timePickerDialog = new TimePickerDialog(this,\n            android.R.style.Theme_Holo_Light_Dialog,\n            new TimePickerDialog.OnTimeSetListener() {\n\n                @Override\n                public void onTimeSet(TimePicker view, int hourOfDay,int minute) {\n\n                    time = LocalTime.of(hourOfDay, minute);\n\n                    milliseconds = date.atTime(time)\n                            .atZone(ZoneId.systemDefault())\n                            .toInstant()\n                            .toEpochMilli();\n                    Log.e(\"LONGGGGGGG\", String.valueOf(milliseconds));\n                }\n            },\n            time.getHour(),\n            time.getMinute(),\n            true);\n    timePickerDialog.show();\n\n}\n<\/code><\/pre>\n<p>You were greatly overcomplicating things. I recommend:<\/p>\n<ol>\n<li>Don\u2019t use strings for dates or times in your business logic, use date and time objects. This also means you\u2019ve got no need for a formatter like <code>SimpleDateFormat<\/code> (which, I might add, is notoriously troublesome, so it is good that you can do without it).<\/li>\n<li>Use <code>java.time<\/code>, the modern Java date and time API. It is so much nicer to work with than the old-fashioned date and time classes like <code>Calendar<\/code>.<\/li>\n<\/ol>\n<p>The two points go hand in hand in the above snippets. Also as far as I can see you have no use for the <code>Calendar<\/code> object (<code>c<\/code>) in <code>setDateField<\/code>, just remove it and all use of it.<\/p>\n<h2>What went wrong in your code?<\/h2>\n<p>The culprit was your <code>SimpleDateFormat<\/code>. Try using it like this:<\/p>\n<pre><code>    SimpleDateFormat f = new SimpleDateFormat(\"yyyy\/mm\/dd HH:mm\");\n    System.out.println(f.parse(\"2018\/04\/17 12:45\"));\n<\/code><\/pre>\n<p>On my computer this prints<\/p>\n<pre><code>Wed Jan 17 12:45:00 CET 2018\n<\/code><\/pre>\n<p>It prints a date in January no matter what is in the string. This is because you tried lowercase <code>mm<\/code> for month. <code>mm<\/code> is for minutes (which you also used correctly), month is uppercase <code>MM<\/code>. So your formatter cannot parse a month at all and assigns your date the default month, which is the first month of the year, January. And this date in January was of course also what went into your millesecond value.<\/p>\n<h2>Question: Can I use <code>java.time<\/code> on Android?<\/h2>\n<p>Yes, you can use <code>java.time<\/code> on Android. Using it just requires Java <strong>6<\/strong>.<\/p>\n<ul>\n<li>In Java 8 and later and newer Android versions the new API comes built-in.<\/li>\n<li>In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310, where the modern API was first described).<\/li>\n<li>On older Android, use the Android edition of ThreeTen Backport. It\u2019s called ThreeTenABP. Make sure you import the date and time classes from package <code>org.threeten.bp<\/code> and subpackages.<\/li>\n<\/ul>\n<h2>Links<\/h2>\n<ul>\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 <code>java.time<\/code>.<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.threeten.org\/threetenbp\/\">ThreeTen Backport project<\/a><\/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<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=310\">Java Specification Request (JSR) 310<\/a>.<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How can I add one month to change into the milliseconds? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I suggest: First, instead of your variables (fields?) year, month, day, hours and minutes just declare private LocalDate date; private LocalTime time; private long milliseconds; (Keep the milliseconds variable since you will want to have your result here.) In onDateSet assign a value to date in this way: date = LocalDate.of(selectedYear, selectedMonth + 1, &#8230; <a title=\"[Solved] How can I add one month to change into the milliseconds?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\" aria-label=\"More on [Solved] How can I add one month to change into the milliseconds?\">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,980,323,3394],"class_list":["post-12462","post","type-post","status-publish","format-standard","hentry","category-solved","tag-android","tag-date","tag-firebase","tag-java","tag-milliseconds"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How can I add one month to change into the milliseconds? - 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-can-i-add-one-month-to-change-into-the-milliseconds\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How can I add one month to change into the milliseconds? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I suggest: First, instead of your variables (fields?) year, month, day, hours and minutes just declare private LocalDate date; private LocalTime time; private long milliseconds; (Keep the milliseconds variable since you will want to have your result here.) In onDateSet assign a value to date in this way: date = LocalDate.of(selectedYear, selectedMonth + 1, ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-30T14:52:49+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-can-i-add-one-month-to-change-into-the-milliseconds\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How can I add one month to change into the milliseconds?\",\"datePublished\":\"2022-09-30T14:52:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\"},\"wordCount\":405,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"android\",\"date\",\"firebase\",\"java\",\"milliseconds\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\",\"name\":\"[Solved] How can I add one month to change into the milliseconds? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-30T14:52:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How can I add one month to change into the milliseconds?\"}]},{\"@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] How can I add one month to change into the milliseconds? - 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-can-i-add-one-month-to-change-into-the-milliseconds\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How can I add one month to change into the milliseconds? - JassWeb","og_description":"[ad_1] I suggest: First, instead of your variables (fields?) year, month, day, hours and minutes just declare private LocalDate date; private LocalTime time; private long milliseconds; (Keep the milliseconds variable since you will want to have your result here.) In onDateSet assign a value to date in this way: date = LocalDate.of(selectedYear, selectedMonth + 1, ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/","og_site_name":"JassWeb","article_published_time":"2022-09-30T14:52:49+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-can-i-add-one-month-to-change-into-the-milliseconds\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How can I add one month to change into the milliseconds?","datePublished":"2022-09-30T14:52:49+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/"},"wordCount":405,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["android","date","firebase","java","milliseconds"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/","url":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/","name":"[Solved] How can I add one month to change into the milliseconds? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-30T14:52:49+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-can-i-add-one-month-to-change-into-the-milliseconds\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How can I add one month to change into the milliseconds?"}]},{"@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\/12462","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=12462"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12462\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=12462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=12462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=12462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}