<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; Date</title>
	<atom:link href="http://www.yukun.info/blog/tag/date/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java, JSP: 現在時刻が指定期間内か判定する &#8211; GregorianCalendar#before、after</title>
		<link>http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html</link>
		<comments>http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html#comments</comments>
		<pubDate>Mon, 23 Feb 2009 12:10:09 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JSP]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1390</guid>
		<description><![CDATA[日時を扱うクラスはDateとGregorianCalendarがありますが、Sunは後者を推奨しているようです。実際GregorianCalendarのほうが扱いやすいです。下のサンプルは、現在時刻が指定した期間内か否か &#8230; <a href="http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html">Java, JSP: 現在時刻が指定期間内か判定する &#8211; GregorianCalendar#before、after</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>日時を扱うクラスはDateとGregorianCalendarがありますが、Sunは後者を推奨しているようです。実際GregorianCalendarのほうが扱いやすいです。下のサンプルは、現在時刻が指定した期間内か否かを判定するものです。</p>
<h2>ソースコード</h2>
<pre>
&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@page import=&quot;java.util.*&quot; %&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
  &lt;%!
    final int MONTH_OFFSET = 1;

    String showTime(Calendar cal) {
      String str;
      str = cal.get(Calendar.YEAR) + &quot;/&quot;
        + (cal.get(Calendar.MONTH) + MONTH_OFFSET)  + &quot;/&quot;
        + cal.get(Calendar.DATE) + &quot; &quot;
        + cal.get(Calendar.HOUR) + &quot;:&quot;
        + cal.get(Calendar.MINUTE);
      return str;
    }

    boolean isPeriod(Calendar start, Calendar end) {
      Calendar cur = Calendar.getInstance();
      return cur.after(start) &amp;&amp; cur.before(end);
    }
  %&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;JSP Date Comparison&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;JSP Date Comparison&lt;/h1&gt;
    &lt;%
      // (year, month, date, hour, minute) monthの範囲は0-11で1月は0
      Calendar startTime = new GregorianCalendar(2009, 2 - MONTH_OFFSET, 23, 21, 0);
      Calendar endTime = new GregorianCalendar(2009, 2 - MONTH_OFFSET, 23, 21, 10);

      out.println(&quot;開始時刻: &quot; + showTime(startTime) + &quot;&lt;br /&gt;&quot;);
      out.println(&quot;終了時刻: &quot; + showTime(endTime) + &quot;&lt;br /&gt;&quot;);

      if (isPeriod(startTime, endTime)) {
        out.println(&quot;現時刻は指定期間「内」&quot;);
      } else {
        out.println(&quot;現時刻は指定期間「外」&quot;);
      }
    %&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>日時の比較はGregorianCalendar#after、beforeメソッド以外にint compareTo(Calendar cal)等もあります。<br />
実際に使う際、期間を指定するGregorianCalendarのコンストラクタの引数データは他の入力から受け取るようにします。</p>
<h2>実行結果例</h2>
<p>本日の21:05に計ると…</p>
<pre>
JSP Date Comparison
開始時刻: 2009/2/23 9:0
終了時刻: 2009/2/23 9:10
現時刻は指定期間「内」
</pre>
<h2>追記: SimpleDateFormatによる出力の整形</h2>
<p>d_kamiさんに改良していただきました。ありがとうございます(^-^)</p>
<blockquote><p>String showTime(Calendar cal) {<br />
  Date date = cal.getTime();<br />
  SimpleDateFormat format = new SimpleDateFormat(&#8220;yyyy/MM/dd HH:mm&#8221;);<br />
  return format.format(date);<br />
}<br />
と書けばすっきりするよ、import java.text.SimpleDateFormatを忘れずに &#8211; <a href="http://d.hatena.ne.jp/d-kami/20090223/1235400686" title="SimpleDateFormat - マイペースなプログラミング日記" target="_blank">SimpleDateFormat &#8211; マイペースなプログラミング日記</a></p></blockquote>
<p>showTimeを修正して計りなおした実行結果は、</p>
<pre>
開始時刻: 2009/02/23 21:00
終了時刻: 2009/02/24 21:10
現時刻は指定期間「内」
</pre>
<p>確かに出力はyyyy/MM/dd HH:mmの形式になっていて見栄えも良いです。</p>
<h3>参考サイト</h3>
<ul>
<li><a href="http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/util/GregorianCalendar.html" title="GregorianCalendar (Java 2 Platform SE 5.0)" target="_blank">GregorianCalendar (Java 2 Platform SE 5.0)</a></li>
<li><a href="http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/text/SimpleDateFormat.html" title="SimpleDateFormat (Java 2 Platform SE 5.0)" target="_blank">SimpleDateFormat (Java 2 Platform SE 5.0)</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/python-date.html" rel="bookmark" title="2008年6月6日">Python: 現在の日付・時刻の取得と出力 &#8211; datetimeクラスの属性、today()、strftime()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2010/03/java-servlet-jsp-request-forward.html" rel="bookmark" title="2010年3月26日">Java: ServletからJSPへリクエストをフォワード</a></li>
<li><a href="http://www.yukun.info/blog/2008/11/php-form-submit-action-post.html" rel="bookmark" title="2008年11月8日">PHP: フォーム情報の送信・受信 &#8211; POSTメソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/arraylist-capacity.html" rel="bookmark" title="2008年3月6日">ArrayListのコンストラクタに初期容量を指定することで要素の追加処理を高速化</a></li>
<li><a href="http://www.yukun.info/blog/2008/05/out-of-memory-error.html" rel="bookmark" title="2008年5月8日">java.lang.OutOfMemoryErrorが発生する原因とその解決法の一例</a></li>
</ul>
<p><!-- Similar Posts took 20.179 ms --></p>
<p><a href="http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html">Java, JSP: 現在時刻が指定期間内か判定する &#8211; GregorianCalendar#before、after</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python: 現在の日付・時刻の取得と出力 &#8211; datetimeクラスの属性、today()、strftime()メソッド</title>
		<link>http://www.yukun.info/blog/2008/06/python-date.html</link>
		<comments>http://www.yukun.info/blog/2008/06/python-date.html#comments</comments>
		<pubDate>Thu, 05 Jun 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Time]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/python-%e7%8f%be%e5%9c%a8%e3%81%ae%e6%97%a5%e4%bb%98%e3%83%bb%e6%99%82%e5%88%bb%e3%81%ae%e5%8f%96%e5%be%97%e3%81%a8%e5%87%ba%e5%8a%9b-datetime%e3%82%af%e3%83%a9%e3%82%b9%e3%81%ae%e5%b1%9e%e6%80%a7</guid>
		<description><![CDATA[ソースコード #!/usr/bin/python # coding: UTF-8 # 現在の日付・時刻の取得と出力 &#124; datetimeクラスの属性、today()、strftime()メソッドの使い方 import d &#8230; <a href="http://www.yukun.info/blog/2008/06/python-date.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/06/python-date.html">Python: 現在の日付・時刻の取得と出力 &#8211; datetimeクラスの属性、today()、strftime()メソッド</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<h2>ソースコード</h2>
<pre>
#!/usr/bin/python
# coding: UTF-8

# 現在の日付・時刻の取得と出力 | datetimeクラスの属性、today()、strftime()メソッドの使い方

import datetime # datetimeモジュールのインポート
import locale   # import文はどこに書いてもOK(可読性などの為、慣例でコードの始めの方)

# today()メソッドで現在日付・時刻のdatetime型データの変数を取得
d = datetime.datetime.today()
#   ↑モジュール名.クラス名.メソッド名

print 'd == %s : %s\n' % (d, type(d)) # Microsecond(10^-6sec)まで取得

# datetime型の各属性へのアクセス
# year, month, day
print '%s年%s月%s日\n' % (d.year, d.month, d.day)

# hour, minute, second, microsecond
print '%s時%s分%s.%s秒n' % (d.hour, d.minute, d.second, d.microsecond)

# strftime()メソッドで日付時刻の書式を指定して出力
print d.strftime("%Y-%m-%d %H:%M:%S"), '\n'

# 地域の設定
locale.setlocale(locale.LC_ALL, 'ja') # 属性の出力に影響(曜日とか)
print locale.getlocale(), '\n'

print d.strftime("%B%d日%A") # locale.setlocale()でlocale指定してないと"Fri"と表示(デフォルトの設定地域)
print d.strftime("%p")       # 指定の地域でのAM/PMの対応する文字列
print d.strftime("%x %X")    #               日付と時刻に対応する文字列
</pre>
<h2>実行結果</h2>
<pre>
d == 2008-06-06 11:50:25.964000 : &#60;type &#39;datetime.datetime&#39;&#62;

2008年6月6日

11時50分25.964000秒

2008-06-06 11:50:25

(&#39;Japanese_Japan&#39;, &#39;932&#39;)

6月06日金曜日
午前
2008/06/06 11:50:25
</pre>
<h3>リファレンス</h3>
<ul>
<li><a href="http://docs.python.org/lib/module-datetime.html" target="_blank">5.1 datetime &#8212; Basic date and time types</a>
<ul>
<li><a href="http://docs.python.org/lib/datetime-datetime.html" target="_blank">5.1.4 datetime Objects</a></li>
</ul>
</li>
<li><a href="http://docs.python.org/lib/module-locale.html" target="_blank">21.2 locale &#8212; Internationalization services</a></li>
</ul>
<h3>チュートリアル</h3>
<ul>
<li><a href="http://docs.python.org/tut/node12.html" target="_blank">10. Brief Tour of the Standard Library</a>
<ul>
<li><a href="http://docs.python.org/tut/node12.html#SECTION0012800000000000000000" target="_blank">10.8 Dates and Times</a>の部分</li>
</ul>
</li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2009/02/java-jsp-gregoriancalendar-period.html" rel="bookmark" title="2009年2月23日">Java, JSP: 現在時刻が指定期間内か判定する &#8211; GregorianCalendar#before、after</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-random.html" rel="bookmark" title="2008年6月10日">Python: 乱数の生成 &#8211; random()、randint()、uniform()、seed()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-dict2.html" rel="bookmark" title="2008年6月4日">Python: 辞書の全てのキーと値をたどる &#8211; items(), keys(), values()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2009/03/java-observe-event-model-view.html" rel="bookmark" title="2009年3月17日">Java: イベント駆動によるModelとViewの分離 &#8211; Observer パターン</a></li>
<li><a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html" rel="bookmark" title="2008年7月26日">Python: コマンドライン引数の取得 &#8211; sys.argv変数</a></li>
</ul>
<p><!-- Similar Posts took 12.469 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/06/python-date.html">Python: 現在の日付・時刻の取得と出力 &#8211; datetimeクラスの属性、today()、strftime()メソッド</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/06/python-date.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

