<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: date code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 12 May 2008 01:09:16 GMT</pubDate>
    <description>DZone Snippets: date code</description>
    <item>
      <title>date_select conversion</title>
      <link>http://snippets.dzone.com/posts/show/5376</link>
      <description>function to convert a value from a date_select into a more sql-friendly value&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%=date_select(:date,'',:start_year =&gt; 1950,:include_blank =&gt; false, :default =&gt; { :year =&gt; '1970' })%&gt;&lt;br /&gt;&lt;br /&gt;def convert_date(obj) &lt;br /&gt;  return &#8220;#{obj[&#8216;(1i)&#8217;]}-#{obj[&#8216;(2i)&#8217;]}-#{obj[&#8216;(3i)&#8217;]}&#8221; &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Apr 2008 13:20:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5376</guid>
      <author>indiehead (John)</author>
    </item>
    <item>
      <title>Convert Java date to GMT</title>
      <link>http://snippets.dzone.com/posts/show/5288</link>
      <description>This function converts a local date to GMT.  This version corrects the bug common to this type of conversion where the date is incorrectly converted when the time is close to the DST crossover.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private static Date cvtToGmt( Date date )&lt;br /&gt;{&lt;br /&gt;   TimeZone tz = TimeZone.getDefault();&lt;br /&gt;   Date ret = new Date( date.getTime() - tz.getRawOffset() );&lt;br /&gt;&lt;br /&gt;   // if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.&lt;br /&gt;   if ( tz.inDaylightTime( ret ))&lt;br /&gt;   {&lt;br /&gt;      Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() );&lt;br /&gt;&lt;br /&gt;      // check to make sure we have not crossed back into standard time&lt;br /&gt;      // this happens when we are on the cusp of DST (7pm the day before the change for PDT)&lt;br /&gt;      if ( tz.inDaylightTime( dstDate ))&lt;br /&gt;      {&lt;br /&gt;         ret = dstDate;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   return ret;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Mar 2008 20:56:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5288</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Days since January 1st, 2008</title>
      <link>http://snippets.dzone.com/posts/show/5275</link>
      <description>// I'm in a challenge to do one pushup and one crunch for each day of the new year.  This little Ruby snippet will tell me how many I have to do!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(DateTime.now - DateTime.new(2008,1,1)).to_i&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 23 Mar 2008 22:22:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5275</guid>
      <author>mm53bar (Mike)</author>
    </item>
    <item>
      <title>Rails Date Formats</title>
      <link>http://snippets.dzone.com/posts/show/5231</link>
      <description>// cribbed from http://wiki.rubyonrails.org/rails/pages/HowToDefineYourOwnDateFormat&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;my_formats = {&lt;br /&gt;  :my_format_1 =&gt; '%l %p, %b %d, %Y',&lt;br /&gt;  :my_format_2  =&gt; '%l:%M %p, %B %d, %Y'&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_formats)&lt;br /&gt;ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_formats)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 14 Mar 2008 14:32:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5231</guid>
      <author>willcodeforfoo ()</author>
    </item>
    <item>
      <title>Increment a date using Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5200</link>
      <description>This Ruby code converts a string into a date and increments the day, week, month, quarter or year.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def date_add(sdate='', unit='',i=0)&lt;br /&gt;&lt;br /&gt;  sdate[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]&lt;br /&gt;  iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i&lt;br /&gt;  &lt;br /&gt;  case  unit&lt;br /&gt;    when 'days'&lt;br /&gt;      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)&lt;br /&gt;      t1 += (60 * 60 * 24 * i)&lt;br /&gt;    when 'weeks'&lt;br /&gt;      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)&lt;br /&gt;      t1 += (60 * 60 * 24 * 7 * i) &lt;br /&gt;    when 'months'&lt;br /&gt;      imonth += i&lt;br /&gt;      if imonth &lt; 12 then&lt;br /&gt;        t1 = Time.local(iyear,imonth+i,iday,ihour,imin,isec)&lt;br /&gt;      else&lt;br /&gt;        t1 = Time.local(iyear+=1,imonth -12,iday,ihour,imin,isec)&lt;br /&gt;      end&lt;br /&gt;    when 'quarter'&lt;br /&gt;      imonth += 3&lt;br /&gt;      if imonth &lt;= 12 then&lt;br /&gt;        t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)&lt;br /&gt;      else&lt;br /&gt;        t1 = Time.local(iyear+=1,imonth - 12,iday,ihour,imin,isec)&lt;br /&gt;      end    &lt;br /&gt;    when 'years'&lt;br /&gt;      t1 = Time.local(iyear+i,imonth,iday,ihour,imin,isec)&lt;br /&gt;    else&lt;br /&gt;      raise 'not a valid date unit'&lt;br /&gt;  end&lt;br /&gt;  t1&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;date_add("17/03/2008 17:48:00",'months',2)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output: Sat May 17 17:48:00 +0100 2008&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 05 Mar 2008 13:43:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5200</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Creating a DateTime object with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5190</link>
      <description>This example creates a date and time variable which represents 22nd March 2008 4:30pm and 12 seconds.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;d2 = DateTime.new(y=200,m=3,d=22, h=16,min=30,s=12)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or convert a date string into a DataTime object:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"17/03/2009 17:48:00"[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]&lt;br /&gt;d2 = DateTime.new(y=$3.to_i,m=$2.to_i,d=$1.to_i, h=$4.to_i,min=$5.to_i,s=$6.to_i)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or convert a date string into a Time object:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"22/03/2008 17:48:00"[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]&lt;br /&gt;iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i&lt;br /&gt;twork = Time.local(iyear,imonth,iday,ihour,imin,isec) &lt;br /&gt;puts 'we still have time to party' if Time.now &lt; twork &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference:&lt;br /&gt;  &lt;a href="http://www.ruby-doc.org/core/classes/DateTime.html"&gt;Class: DateTime&lt;/a&gt; [ruby-doc.org]&lt;br /&gt;  &lt;a href="http://www.ruby-doc.org/core/classes/Time.html"&gt;Class: Time&lt;/a&gt; [ruby-doc.org]</description>
      <pubDate>Sat, 01 Mar 2008 21:53:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5190</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Calculate last day of current Month</title>
      <link>http://snippets.dzone.com/posts/show/5183</link>
      <description>'Determines what the next month is based on today and subtracts 1 day from first day of next month. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;'VB.NET&lt;br /&gt;Dim NextMonth As Integer&lt;br /&gt;Dim RptYear As Integer&lt;br /&gt;'Determine next month&lt;br /&gt;NextMonth = DatePart(DateInterval.Month, DateAdd(DateInterval.Month, +1, today))&lt;br /&gt;'Determine the year of the next month, in case you are going from Dec to Jan&lt;br /&gt;RptYear = DatePart(DateInterval.Year, DateAdd(DateInterval.Month, +1, today))&lt;br /&gt;'Subtract 1 day from the first day of next month to get this months last day&lt;br /&gt;Return DateAdd(DateInterval.Day, -1, DateValue(NextMonth.ToString &amp; "/1/" &amp; RptYear.ToString))&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 27 Feb 2008 19:13:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5183</guid>
      <author>bmowbray (Brian Mowbray)</author>
    </item>
    <item>
      <title>isWorkingDay?</title>
      <link>http://snippets.dzone.com/posts/show/5135</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  public boolean isWorkingDay(Date date) {&lt;br /&gt;        Calendar cal = GregorianCalendar.getInstance();&lt;br /&gt;        cal.setTime(date);&lt;br /&gt;        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); &lt;br /&gt;        if ((dayOfWeek  Calendar.SATURDAY) || (dayOfWeek  Calendar.SUNDAY)) {&lt;br /&gt;            return false;&lt;br /&gt;        } &lt;br /&gt;        return true;&lt;br /&gt;   }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Feb 2008 02:45:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5135</guid>
      <author>caffo ()</author>
    </item>
    <item>
      <title>Calculate Last Day of Last Month</title>
      <link>http://snippets.dzone.com/posts/show/5076</link>
      <description>VB/VBA/VB.NET one-liner to calculate the end of last month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("d", -1.0 * DatePart("d", Today), Today)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:51:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5076</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Calculate First Day of Current Month</title>
      <link>http://snippets.dzone.com/posts/show/5075</link>
      <description>VB/VBA/VB.NET one-liner to calculate the start of the current month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:48:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5075</guid>
      <author>jokeyxero (xero)</author>
    </item>
  </channel>
</rss>
