You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2008/04/16 02:21:55 UTC
svn commit: r648464 - in /commons/sandbox/me/trunk:
src/org/apache/commons/me/util/DateHelper.java
test-src/org/apache/commons/me/util/TestDateHelper.java
Author: nick
Date: Tue Apr 15 17:21:53 2008
New Revision: 648464
URL: http://svn.apache.org/viewvc?rev=648464&view=rev
Log:
Date related helpers
Added:
commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java (with props)
commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java (with props)
Added: commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java?rev=648464&view=auto
==============================================================================
--- commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java (added)
+++ commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java Tue Apr 15 17:21:53 2008
@@ -0,0 +1,200 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.commons.me.util;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+/**
+ * Various date related helper functions.
+ * In the interests of sanity, will normally work just in UTC,
+ * but that can be tweaked if required.
+ */
+public class DateHelper {
+ private static TimeZone timeZone = TimeZone.getTimeZone("UTC");
+ /**
+ * Sets the TimeZone to work in, when converting Date
+ * objects into Calendars, and on into Strings
+ */
+ public static void setTimeZone(TimeZone tz) {
+ timeZone = tz;
+ }
+
+ public static Calendar getCalendar() {
+ Calendar c = Calendar.getInstance();
+ c.setTimeZone(timeZone);
+ return c;
+ }
+ public static Calendar getCalendar(Date date) {
+ Calendar c = getCalendar();
+ c.setTime(date);
+ return c;
+ }
+
+ public static Calendar fromYMD(int[] ymd) {
+ if(ymd.length != 3) {
+ throw new IllegalArgumentException("ymd must be of the form {year,month,day}");
+ }
+ return fromYMD(ymd[0], ymd[1], ymd[2]);
+ }
+ public static Calendar fromYMD(int year, int month, int day) {
+ Calendar c = getCalendar();
+ c.set(Calendar.YEAR, year);
+ c.set(Calendar.MONTH, (month-1)); // 0 based!
+ c.set(Calendar.DAY_OF_MONTH, day);
+ // Set the rest to zero
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ return c;
+ }
+
+ public static int[] toYMD(Calendar c) {
+ return new int[] {
+ c.get(Calendar.YEAR),
+ c.get(Calendar.MONTH) + 1, // 0 based!
+ c.get(Calendar.DAY_OF_MONTH)
+ };
+ }
+
+ /**
+ * Convenience function for getting yy/mm/dd relative
+ * to today, offset by 'days'.
+ * @param daysHence Number of days hence to return YMD for
+ */
+ public static int[] toYMD(int daysHence) {
+ // Calculate the time at that point, staying as a long
+ // so don't get any int wrapping problems
+ long then = System.currentTimeMillis() + (daysHence*24l*60*60*1000);
+ // Now turn that into a calendar, then into YMD
+ Calendar c = getCalendar( new Date(then) );
+ return toYMD(c);
+ }
+
+
+ public static String asISODate(Date d) {
+ Calendar c = getCalendar();
+ c.setTime(d);
+ return asISODate(c);
+ }
+ private static String asISODate(Calendar c) {
+ StringBuffer date = new StringBuffer();
+
+ date.append( c.get(Calendar.YEAR) );
+
+ date.append("-");
+ int month = c.get(Calendar.MONTH)+1; // 0 based!
+ if(month < 10) { date.append("0"); }
+ date.append(month);
+
+ date.append("-");
+ int day = c.get(Calendar.DAY_OF_MONTH);
+ if(day < 10) { date.append("0"); }
+ date.append(day);
+
+ return date.toString();
+ }
+ public static String asISODateTime(Date d) {
+ Calendar c = getCalendar();
+ c.setTime(d);
+
+ StringBuffer dt = new StringBuffer();
+ dt.append( asISODate(c) );
+ dt.append(" ");
+
+ int hour = c.get(Calendar.HOUR_OF_DAY);
+ int minute = c.get(Calendar.MINUTE);
+ if(hour < 10) { dt.append("0"); }
+ dt.append(hour);
+ dt.append(":");
+ if(minute < 10) { dt.append("0"); }
+ dt.append(minute);
+
+ return dt.toString();
+ }
+
+ /**
+ * Gets the date specified, in ISO date format
+ */
+ public static String asIsoDate(int[] ymd) {
+ if(ymd.length != 3) {
+ throw new IllegalArgumentException("Must be of the form {year,month,day}");
+ }
+ return asIsoDate(ymd[0], ymd[1], ymd[2]);
+ }
+ /**
+ * Gets the date specified, in ISO date format
+ */
+ public static String asIsoDate(int year, int month, int day) {
+ StringBuffer d = new StringBuffer();
+ d.append(year);
+ d.append("-");
+ if(month < 10) {
+ d.append("0");
+ }
+ d.append(month);
+ d.append("-");
+ if(day < 10) {
+ d.append("0");
+ }
+ d.append(day);
+
+ return d.toString();
+ }
+ /**
+ * Turns an ISO date formatted date string into
+ * [year,month,day]
+ */
+ public static int[] fromIsoString(String s) {
+ String yS = s.substring(0, s.indexOf("-"));
+ String mdS = s.substring(s.indexOf("-") + 1);
+ String mS = mdS.substring(0, mdS.indexOf("-"));
+ String dS = mdS.substring(mdS.indexOf("-")+1);
+ return new int[] {
+ Integer.parseInt(yS),
+ Integer.parseInt(mS),
+ Integer.parseInt(dS)
+ };
+ }
+
+ public static String asHhMm(Date d) {
+ Calendar c = getCalendar(d);
+ StringBuffer dt = new StringBuffer();
+
+ int hour = c.get(Calendar.HOUR_OF_DAY);
+ int minute = c.get(Calendar.MINUTE);
+
+ // Turn into 12 hour clock no matter what
+ int h = hour;
+ if(hour > 12)
+ h -= 12;
+ dt.append(h);
+ dt.append(":");
+
+ if(minute < 10) { dt.append("0"); }
+ dt.append(minute);
+
+ if(c.get(Calendar.AM_PM) == Calendar.AM) {
+ dt.append("am");
+ } else {
+ dt.append("pm");
+ }
+ return dt.toString();
+ }
+}
Propchange: commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java?rev=648464&view=auto
==============================================================================
--- commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java (added)
+++ commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java Tue Apr 15 17:21:53 2008
@@ -0,0 +1,166 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.commons.me.util;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for the Date Helper functions
+ */
+public class TestDateHelper extends TestCase {
+ public void testIsoDate() throws Exception {
+ Date d;
+
+ // 2009-02-13 23:31:30
+ d = new Date(1234567890*1000l);
+ assertEquals("2009-02-13", DateHelper.asISODate(d));
+ assertEquals("2009-02-13 23:31", DateHelper.asISODateTime(d));
+
+ // 2012-04-16 09:18:10
+ d = new Date(1334567890*1000l);
+ assertEquals("2012-04-16", DateHelper.asISODate(d));
+ assertEquals("2012-04-16 09:18", DateHelper.asISODateTime(d));
+
+ // 2012-04-04 19:31:30
+ d = new Date(1333567890*1000l);
+ assertEquals("2012-04-04", DateHelper.asISODate(d));
+ assertEquals("2012-04-04 19:31", DateHelper.asISODateTime(d));
+
+ // 2012-04-02 02:03:40
+ d = new Date(1333332220*1000l);
+ assertEquals("2012-04-02", DateHelper.asISODate(d));
+ assertEquals("2012-04-02 02:03", DateHelper.asISODateTime(d));
+ }
+
+ public void testIsoDateYMD() throws Exception {
+ int[] ymd;
+
+ assertEquals("2007-01-02", DateHelper.asIsoDate(2007,01,02));
+ assertEquals("2007-01-02", DateHelper.asIsoDate(new int[]{2007,01,02}));
+
+ assertEquals("2007-11-02", DateHelper.asIsoDate(2007,11,02));
+ assertEquals("2007-11-02", DateHelper.asIsoDate(new int[]{2007,11,02}));
+
+ assertEquals("2007-11-30", DateHelper.asIsoDate(2007,11,30));
+ assertEquals("2007-11-30", DateHelper.asIsoDate(new int[]{2007,11,30}));
+
+ ymd = DateHelper.fromIsoString("2007-11-30");
+ assertEquals(2007, ymd[0]);
+ assertEquals(11, ymd[1]);
+ assertEquals(30, ymd[2]);
+
+ ymd = DateHelper.fromIsoString("2001-01-04");
+ assertEquals(2001, ymd[0]);
+ assertEquals(1, ymd[1]);
+ assertEquals(4, ymd[2]);
+
+ ymd = DateHelper.fromIsoString("2001-1-4");
+ assertEquals(2001, ymd[0]);
+ assertEquals(1, ymd[1]);
+ assertEquals(4, ymd[2]);
+ }
+
+ /**
+ * Test these in python:
+ * import datetime
+ * datetime.datetime.fromtimestamp(1234569600)
+ */
+ public void testYMDToCal() throws Exception {
+ Calendar c;
+
+ // 1234567890 = 2009-02-13 23:31:30
+ c = DateHelper.fromYMD(new int[] {2009,2,14});
+ assertEquals(1234569600 * 1000l, c.getTimeInMillis());
+
+ // 1334567890 = 2012-04-16 09:18:10
+ c = DateHelper.fromYMD(new int[] {2012,4,17});
+ assertEquals(1334620800 * 1000l, c.getTimeInMillis());
+
+ // 1333567890 = 2012-04-04 19:31:30
+ c = DateHelper.fromYMD(new int[] {2012,4,5});
+ assertEquals(1333584000 * 1000l, c.getTimeInMillis());
+
+ // 1333332220 = 2012-04-02 02:03:40
+ c = DateHelper.fromYMD(new int[] {2012,4,3});
+ assertEquals(1333411200 * 1000l, c.getTimeInMillis());
+ }
+
+ public void testCalToYMD() throws Exception {
+ Calendar c = Calendar.getInstance();
+ c.setTimeZone(TimeZone.getTimeZone("UTC"));
+ int[] ymd;
+
+ // 2009-02-13 23:31:30
+ c.setTimeInMillis(1234567890*1000l);
+ ymd = DateHelper.toYMD(c);
+ assertEquals(2009, ymd[0]);
+ assertEquals(2, ymd[1]);
+ assertEquals(13, ymd[2]);
+
+ // 2012-04-16 09:18:10
+ c.setTimeInMillis(1334567890*1000l);
+ ymd = DateHelper.toYMD(c);
+ assertEquals(2012, ymd[0]);
+ assertEquals(4, ymd[1]);
+ assertEquals(16, ymd[2]);
+
+ // 2012-04-04 19:31:30
+ c.setTimeInMillis(1333567890*1000l);
+ ymd = DateHelper.toYMD(c);
+ assertEquals(2012, ymd[0]);
+ assertEquals(4, ymd[1]);
+ assertEquals(4, ymd[2]);
+
+ // 2012-04-02 02:03:40
+ c.setTimeInMillis(1333332220*1000l);
+ ymd = DateHelper.toYMD(c);
+ assertEquals(2012, ymd[0]);
+ assertEquals(4, ymd[1]);
+ assertEquals(2, ymd[2]);
+
+ // Check via days hence
+ int[] alt_ymd = DateHelper.toYMD(0);
+ ymd = DateHelper.toYMD(DateHelper.getCalendar());
+ assertEquals(ymd[0], alt_ymd[0]);
+ assertEquals(ymd[1], alt_ymd[1]);
+ assertEquals(ymd[2], alt_ymd[2]);
+ }
+
+ public void testFormatTime() throws Exception {
+ Calendar c = DateHelper.getCalendar();
+
+ // 2009-02-13 23:31:30
+ c.setTimeInMillis(1234567890*1000l);
+ assertEquals("11:31pm", DateHelper.asHhMm(c.getTime()));
+
+ // 2012-04-16 09:18:10
+ c.setTimeInMillis(1334567890*1000l);
+ assertEquals("9:18am", DateHelper.asHhMm(c.getTime()));
+
+ // 2012-04-04 19:31:30
+ c.setTimeInMillis(1333567890*1000l);
+ assertEquals("7:31pm", DateHelper.asHhMm(c.getTime()));
+
+ // 2012-04-02 02:03:40
+ c.setTimeInMillis(1333332220*1000l);
+ assertEquals("2:03am", DateHelper.asHhMm(c.getTime()));
+ }
+}
Propchange: commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java
------------------------------------------------------------------------------
svn:eol-style = native