{"id":687,"date":"2005-06-30T23:17:08","date_gmt":"2005-07-01T03:17:08","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=687"},"modified":"2005-06-30T23:17:08","modified_gmt":"2005-07-01T03:17:08","slug":"fun-with-java-dates","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2005\/06\/30\/fun-with-java-dates\/","title":{"rendered":"Fun With Java dates"},"content":{"rendered":"<p>Sorry for not posting anything other than links recently, the kid is wearing me out almost as much as working with Java dates.  I haven&#8217;t had to work with <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/util\/Date.html\">java.util.Date<\/a> and <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/util\/Calendar.html\">java.util.Calendar<\/a> all that much, most of the time it&#8217;s been pretty simple stuff but for whatever reason this past week has been spent waist deep in the Calendar and Date API&#8217;s trying to fix a bug in a deployed application. The code I was using looked something like this:<br \/>\n<code><br \/>\n\/\/ date entered by user through a combination<br \/>\n\/\/ of drop down lists<br \/>\nString strdate = \"12\/6\/1975 5:30 PM\"<br \/>\nSimpleDateFormat formatter = new SimpleDateFormat(\"M\/d\/yyyy H:m a\");<br \/>\nDate eventdate = formatter.parse(strdate);<br \/>\nSystem.out.println(formatter.format(eventdate));<br \/>\n<\/code><br \/>\nand out of that I expected to see a date of 12\/6\/1975 at 5:30pm, it should in fact have printed back the exact same string I entered right? Instead I got:<br \/>\n<code><br \/>\n12\/6\/1975 5:30 AM<br \/>\n<\/code><br \/>\nI spent awhile trying different things (am I entering the date wrong? is there something wrong with the timezone? etc..) but eventually noticed that &#8216;h&#8217; is not the same thing as &#8216;H&#8217; in the date format string. Using an upper case &#8216;H&#8217; signifies to the SimpleDateFormat parser that you want it to look for a 24 hour time format (ie: instead of 5:30 I should have used 17:30).  In hindsight that&#8217;s somewhat obvious, but the tricky thing is that I included the PM designation, so in theory, shouldn&#8217;t the SimpleDateFormat have picked up the fact that I wanted a time in PM?  I think it should have at least have thrown an exception that says you&#8217;ve entered a time in the morning and an AM PM designation of evening.  True Date class experts would say at this point that I should have set the SimpleDateFormat to be strict in it&#8217;s parsing:<br \/>\n<code><br \/>\nformatter.<a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/text\/DateFormat.html#isLenient()\">setLenient(true)<\/a><br \/>\n<\/code><br \/>\nwhich I did, but it still didn&#8217;t throw an exception.  Should it have?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorry for not posting anything other than links recently, the kid is wearing me out almost as much as working with Java dates. I haven&#8217;t had to work with java.util.Date and java.util.Calendar all that much, most of the time it&#8217;s been pretty simple stuff but for whatever reason this past week has been spent waist &hellip; <a href=\"https:\/\/cephas.net\/blog\/2005\/06\/30\/fun-with-java-dates\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Fun With Java dates<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/687"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=687"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/687\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}