• R/O
  • SSH
  • HTTPS

manjyucalendar: Commit


Commit MetaInfo

Revision3 (tree)
Time2012-11-29 23:20:37
Authoriga

Log Message

New project.

Change Summary

Incremental Difference

--- manjyu-calendar/trunk/test/src/org/manjyu/calendar/ManjyuDateUtilTest.java (nonexistent)
+++ manjyu-calendar/trunk/test/src/org/manjyu/calendar/ManjyuDateUtilTest.java (revision 3)
@@ -0,0 +1,72 @@
1+/*******************************************************************************
2+ * ManjyuCalendar
3+ * Copyright (C) 2012 Toshiki IGA
4+ *
5+ * This library is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU Lesser General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU Lesser General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU Lesser General Public License
16+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
17+ *******************************************************************************/
18+/*******************************************************************************
19+ * Copyright (c) 2012 Toshiki IGA and others.
20+ * All rights reserved. This program and the accompanying materials
21+ * are made available under the terms of the Eclipse Public License v1.0
22+ * which accompanies this distribution, and is available at
23+ * http://www.eclipse.org/legal/epl-v10.html
24+ *
25+ * Contributors:
26+ * Toshiki IGA - initial API and implementation
27+ *******************************************************************************/
28+/*******************************************************************************
29+ * Copyright 2012 Toshiki IGA and others.
30+ *
31+ * Licensed under the Apache License, Version 2.0 (the "License");
32+ * you may not use this file except in compliance with the License.
33+ * You may obtain a copy of the License at
34+ *
35+ * http://www.apache.org/licenses/LICENSE-2.0
36+ *
37+ * Unless required by applicable law or agreed to in writing, software
38+ * distributed under the License is distributed on an "AS IS" BASIS,
39+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40+ * See the License for the specific language governing permissions and
41+ * limitations under the License.
42+ *******************************************************************************/
43+package org.manjyu.calendar;
44+
45+import static org.junit.Assert.assertEquals;
46+
47+import java.text.SimpleDateFormat;
48+import java.util.Date;
49+
50+import org.junit.Test;
51+
52+public class ManjyuDateUtilTest {
53+
54+ @Test
55+ public void testGetNow() {
56+ final Date date = ManjyuDateUtil.getNow();
57+ // System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date));
58+ }
59+
60+ @Test
61+ public void testGetDate() {
62+ final Date date = ManjyuDateUtil.getDate(2012, 11, 15);
63+ assertEquals("check", "2012-11-15 00:00:00.000", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date));
64+ }
65+
66+ @Test
67+ public void testClearHhmmss() {
68+ final Date date = ManjyuDateUtil.getNow();
69+ ManjyuDateUtil.clearHhmmss(date);
70+ assertEquals("check", "00:00:00.000", new SimpleDateFormat("HH:mm:ss.SSS").format(date));
71+ }
72+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- manjyu-calendar/trunk/test/src/org/manjyu/calendar/ManjyuCalendarUtilTest.java (nonexistent)
+++ manjyu-calendar/trunk/test/src/org/manjyu/calendar/ManjyuCalendarUtilTest.java (revision 3)
@@ -0,0 +1,153 @@
1+/*******************************************************************************
2+ * ManjyuCalendar
3+ * Copyright (C) 2012 Toshiki IGA
4+ *
5+ * This library is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU Lesser General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU Lesser General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU Lesser General Public License
16+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
17+ *******************************************************************************/
18+/*******************************************************************************
19+ * Copyright (c) 2012 Toshiki IGA and others.
20+ * All rights reserved. This program and the accompanying materials
21+ * are made available under the terms of the Eclipse Public License v1.0
22+ * which accompanies this distribution, and is available at
23+ * http://www.eclipse.org/legal/epl-v10.html
24+ *
25+ * Contributors:
26+ * Toshiki IGA - initial API and implementation
27+ *******************************************************************************/
28+/*******************************************************************************
29+ * Copyright 2012 Toshiki IGA and others.
30+ *
31+ * Licensed under the Apache License, Version 2.0 (the "License");
32+ * you may not use this file except in compliance with the License.
33+ * You may obtain a copy of the License at
34+ *
35+ * http://www.apache.org/licenses/LICENSE-2.0
36+ *
37+ * Unless required by applicable law or agreed to in writing, software
38+ * distributed under the License is distributed on an "AS IS" BASIS,
39+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40+ * See the License for the specific language governing permissions and
41+ * limitations under the License.
42+ *******************************************************************************/
43+package org.manjyu.calendar;
44+
45+import static org.junit.Assert.assertEquals;
46+
47+import java.text.SimpleDateFormat;
48+import java.util.Calendar;
49+import java.util.Date;
50+
51+import org.junit.Test;
52+
53+public class ManjyuCalendarUtilTest {
54+
55+ @Test
56+ public void testGetCalendar() {
57+ //for (int index = 0; index < 100; index++) {
58+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
59+ //System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(cal.getTime()));
60+ //}
61+ }
62+
63+ @Test
64+ public void testGetCalendarIntIntInt() {
65+ final Calendar cal = ManjyuCalendarUtil.getCalendar(2012, 11, 30);
66+ assertEquals("year part", 2012, ManjyuCalendarUtil.getYear(cal));
67+ assertEquals("month part", 11, ManjyuCalendarUtil.getMonth(cal));
68+ assertEquals("month part", 10, cal.get(Calendar.MONTH));
69+ assertEquals("day part", 30, ManjyuCalendarUtil.getDay(cal));
70+ assertEquals("hour part", 0, cal.get(Calendar.HOUR_OF_DAY));
71+ assertEquals("minute part", 0, cal.get(Calendar.MINUTE));
72+ assertEquals("second part", 0, cal.get(Calendar.SECOND));
73+ assertEquals("millisecond part", 0, cal.get(Calendar.MILLISECOND));
74+ assertEquals("full", "2012-11-30 00:00:00.000",
75+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(cal.getTime()));
76+ }
77+
78+ @Test
79+ public void testGetCalendarDate() {
80+ final Calendar cal = ManjyuCalendarUtil.getCalendar(new Date());
81+ }
82+
83+ @Test
84+ public void testClearHhmmss() {
85+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
86+ ManjyuCalendarUtil.clearHhmmss(cal);
87+ assertEquals("hour part", 0, cal.get(Calendar.HOUR_OF_DAY));
88+ assertEquals("minute part", 0, cal.get(Calendar.MINUTE));
89+ assertEquals("second part", 0, cal.get(Calendar.SECOND));
90+ assertEquals("millisecond part", 0, cal.get(Calendar.MILLISECOND));
91+ }
92+
93+ @Test
94+ public void testSetYear() {
95+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
96+ ManjyuCalendarUtil.setYear(cal, 2000);
97+ assertEquals("check", 2000, cal.get(Calendar.YEAR));
98+ }
99+
100+ @Test
101+ public void testGetYear() {
102+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
103+ ManjyuCalendarUtil.setYear(cal, 2000);
104+ assertEquals("check", 2000, ManjyuCalendarUtil.getYear(cal));
105+ }
106+
107+ @Test
108+ public void testSetMonth() {
109+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
110+ ManjyuCalendarUtil.setMonth(cal, 1);
111+ assertEquals("check", 0, cal.get(Calendar.MONTH));
112+ }
113+
114+ @Test
115+ public void testGetMonth() {
116+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
117+ ManjyuCalendarUtil.setMonth(cal, 1);
118+ assertEquals("check", 1, ManjyuCalendarUtil.getMonth(cal));
119+ }
120+
121+ @Test
122+ public void testSetDay() {
123+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
124+ ManjyuCalendarUtil.setDay(cal, 1);
125+ assertEquals("check", 1, cal.get(Calendar.DAY_OF_MONTH));
126+ }
127+
128+ @Test
129+ public void testGetDay() {
130+ final Calendar cal = ManjyuCalendarUtil.getCalendar();
131+ ManjyuCalendarUtil.setDay(cal, 1);
132+ assertEquals("check", 1, ManjyuCalendarUtil.getDay(cal));
133+ }
134+
135+ @Test
136+ public void testValidateYmd() {
137+ assertEquals("check", true, ManjyuCalendarUtil.validateYmd(2012, 1, 1));
138+ assertEquals("check", true, ManjyuCalendarUtil.validateYmd(2012, 1, 31));
139+ assertEquals("check", false, ManjyuCalendarUtil.validateYmd(2012, 1, 32));
140+ assertEquals("check", true, ManjyuCalendarUtil.validateYmd(2012, 2, 28));
141+ assertEquals("check", true, ManjyuCalendarUtil.validateYmd(2012, 2, 29));
142+ assertEquals("check", true, ManjyuCalendarUtil.validateYmd(2013, 2, 28));
143+ assertEquals("check", false, ManjyuCalendarUtil.validateYmd(2013, 2, 29));
144+ }
145+
146+ @Test
147+ public void testMoveToLastDayOfMonth() {
148+ final Calendar cal = ManjyuCalendarUtil.getCalendar(2012, 2, 15);
149+ ManjyuCalendarUtil.moveToLastDayOfMonth(cal);
150+ assertEquals("check", "2012-02-29 00:00:00.000",
151+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(cal.getTime()));
152+ }
153+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- manjyu-calendar/trunk/src/org/manjyu/calendar/ManjyuDateUtil.java (nonexistent)
+++ manjyu-calendar/trunk/src/org/manjyu/calendar/ManjyuDateUtil.java (revision 3)
@@ -0,0 +1,103 @@
1+/*******************************************************************************
2+ * ManjyuCalendar
3+ * Copyright (C) 2012 Toshiki IGA
4+ *
5+ * This library is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU Lesser General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU Lesser General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU Lesser General Public License
16+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
17+ *******************************************************************************/
18+/*******************************************************************************
19+ * Copyright (c) 2012 Toshiki IGA and others.
20+ * All rights reserved. This program and the accompanying materials
21+ * are made available under the terms of the Eclipse Public License v1.0
22+ * which accompanies this distribution, and is available at
23+ * http://www.eclipse.org/legal/epl-v10.html
24+ *
25+ * Contributors:
26+ * Toshiki IGA - initial API and implementation
27+ *******************************************************************************/
28+/*******************************************************************************
29+ * Copyright 2012 Toshiki IGA and others.
30+ *
31+ * Licensed under the Apache License, Version 2.0 (the "License");
32+ * you may not use this file except in compliance with the License.
33+ * You may obtain a copy of the License at
34+ *
35+ * http://www.apache.org/licenses/LICENSE-2.0
36+ *
37+ * Unless required by applicable law or agreed to in writing, software
38+ * distributed under the License is distributed on an "AS IS" BASIS,
39+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40+ * See the License for the specific language governing permissions and
41+ * limitations under the License.
42+ *******************************************************************************/
43+package org.manjyu.calendar;
44+
45+import java.util.Date;
46+
47+/**
48+ * Date Utility for Java.
49+ *
50+ * @author Toshiki Iga
51+ */
52+public class ManjyuDateUtil {
53+ /**
54+ * Hide Constructor.
55+ */
56+ private ManjyuDateUtil() {
57+ }
58+
59+ /**
60+ * Get current date.
61+ *
62+ * @return current date object.
63+ */
64+ public static Date getNow() {
65+ return new Date();
66+ }
67+
68+ /**
69+ * Get date with specified condition.
70+ *
71+ * @param year Year value. 1 origin.
72+ * @param month Month value. 1 origin.
73+ * @param day Day value. 1 origin.
74+ * @return Date object of specified value.
75+ */
76+ public static Date getDate(final int year, final int month, final int day) {
77+ return ManjyuCalendarUtil.getCalendar(year, month, day).getTime();
78+ }
79+
80+ /**
81+ * Clear hour, minute, second part.
82+ *
83+ * @param date Target date object to clear.
84+ */
85+ @SuppressWarnings("deprecation")
86+ public static void clearHhmmss(final Date date) {
87+ final int year = date.getYear();
88+ final int month = date.getMonth();
89+ final int day = date.getDate();
90+
91+ // Clear date fields for milliseconds value.
92+ date.setTime(0);
93+
94+ // Set year, month, date (day of month), and hours.
95+ date.setYear(year);
96+ date.setMonth(month);
97+ // Date means day of month.
98+ date.setDate(day);
99+ date.setHours(0);
100+ date.setMinutes(0);
101+ date.setSeconds(0);
102+ }
103+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- manjyu-calendar/trunk/src/org/manjyu/calendar/ManjyuCalendarUtil.java (nonexistent)
+++ manjyu-calendar/trunk/src/org/manjyu/calendar/ManjyuCalendarUtil.java (revision 3)
@@ -0,0 +1,214 @@
1+/*******************************************************************************
2+ * ManjyuCalendar
3+ * Copyright (C) 2012 Toshiki IGA
4+ *
5+ * This library is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU Lesser General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU Lesser General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU Lesser General Public License
16+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
17+ *******************************************************************************/
18+/*******************************************************************************
19+ * Copyright (c) 2012 Toshiki IGA and others.
20+ * All rights reserved. This program and the accompanying materials
21+ * are made available under the terms of the Eclipse Public License v1.0
22+ * which accompanies this distribution, and is available at
23+ * http://www.eclipse.org/legal/epl-v10.html
24+ *
25+ * Contributors:
26+ * Toshiki IGA - initial API and implementation
27+ *******************************************************************************/
28+/*******************************************************************************
29+ * Copyright 2012 Toshiki IGA and others.
30+ *
31+ * Licensed under the Apache License, Version 2.0 (the "License");
32+ * you may not use this file except in compliance with the License.
33+ * You may obtain a copy of the License at
34+ *
35+ * http://www.apache.org/licenses/LICENSE-2.0
36+ *
37+ * Unless required by applicable law or agreed to in writing, software
38+ * distributed under the License is distributed on an "AS IS" BASIS,
39+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40+ * See the License for the specific language governing permissions and
41+ * limitations under the License.
42+ *******************************************************************************/
43+package org.manjyu.calendar;
44+
45+import java.util.Calendar;
46+import java.util.Date;
47+
48+/**
49+ * Calendar Utility for Java.
50+ *
51+ * @author Toshiki Iga
52+ */
53+public class ManjyuCalendarUtil {
54+ /**
55+ * Hide Constructor.
56+ */
57+ private ManjyuCalendarUtil() {
58+ }
59+
60+ /**
61+ * Create new calendar.
62+ *
63+ * @return Calendar object of just now.
64+ */
65+ public static Calendar getCalendar() {
66+ return Calendar.getInstance();
67+ }
68+
69+ /**
70+ * Create new calendar with specified condition.
71+ *
72+ * @param Year value. 1 origin. ie: 2012.
73+ * @param Month value. 1 origin. 1 to 12.
74+ * @param Day value. 1 origin. 1 to 31.
75+ * @return Specified day's calendar object. Hour, minute, second is 0.
76+ */
77+ public static Calendar getCalendar(final int year, final int month, final int day) {
78+ final Calendar cal = Calendar.getInstance();
79+ clearHhmmss(cal);
80+
81+ setYear(cal, year);
82+ setMonth(cal, month);
83+ setDay(cal, day);
84+
85+ return cal;
86+ }
87+
88+ /**
89+ * Create new calendar with specified date object.
90+ *
91+ * @param date New calendar's date.
92+ * @return Specified day's calendar object. Hour, minute, second is 0.
93+ */
94+ public static Calendar getCalendar(final Date date) {
95+ final Calendar cal = getCalendar();
96+ cal.setTime(date);
97+ return cal;
98+ }
99+
100+ /**
101+ * Clear hour, minute, second part.
102+ *
103+ * @param cal Target calendar object to clear hour, minute, second part.
104+ */
105+ public static void clearHhmmss(final Calendar cal) {
106+ cal.set(Calendar.HOUR_OF_DAY, 0);
107+ cal.set(Calendar.MINUTE, 0);
108+ cal.set(Calendar.SECOND, 0);
109+ cal.set(Calendar.MILLISECOND, 0);
110+ }
111+
112+ /**
113+ * Set year value to Celendar.
114+ *
115+ * @param cal Target calendar object.
116+ * @param Year value. 1 origin. ie: 2012.
117+ */
118+ public static void setYear(final Calendar cal, final int year) {
119+ cal.set(Calendar.YEAR, year);
120+ }
121+
122+ /**
123+ * Get year value from Celendar.
124+ *
125+ * @param cal Target calendar object.
126+ * @return Year value. 1 origin. ie: 2012
127+ */
128+ public static int getYear(final Calendar cal) {
129+ return cal.get(Calendar.YEAR);
130+ }
131+
132+ /**
133+ * Set month value to Celendar.
134+ *
135+ * @param cal Target calendar object.
136+ * @param Month value. 1 origin. 1 to 12.
137+ */
138+ public static void setMonth(final Calendar cal, final int month) {
139+ // java.util.Calendar.MONTH is 0 origin.
140+ cal.set(Calendar.MONTH, month - 1);
141+ }
142+
143+ /**
144+ * Get month value from Celendar.
145+ *
146+ * @param cal Target calendar object.
147+ * @return Month value. 1 origin. 1 to 12.
148+ */
149+ public static int getMonth(final Calendar cal) {
150+ // java.util.Calendar.MONTH is 0 origin.
151+ return cal.get(Calendar.MONTH) + 1;
152+ }
153+
154+ /**
155+ * Set day of month value to Celendar.
156+ *
157+ * @param cal Target calendar object.
158+ * @param Day value. 1 origin. 1 to 31.
159+ */
160+ public static void setDay(final Calendar cal, final int day) {
161+ cal.set(Calendar.DAY_OF_MONTH, day);
162+ }
163+
164+ /**
165+ * Get day of month value from Celendar.
166+ *
167+ * @param cal Target calendar object.
168+ * @return Day value. 1 origin. 1 to 31.
169+ */
170+ public static int getDay(final Calendar cal) {
171+ return cal.get(Calendar.DAY_OF_MONTH);
172+ }
173+
174+ /**
175+ * Validate specified year, month, date combination.
176+ *
177+ * Validate is do with Calendar.setLenient(false);
178+ *
179+ * @param year Year value. 1 origin. ie: 2012.
180+ * @param month Month value. 1 origin. 1 to 12.
181+ * @param date Date value. 1 origin. 1 to 31.
182+ * @return If valid true, Invalid false.
183+ */
184+ public static boolean validateYmd(final int year, final int month, final int date) {
185+ try {
186+ final Calendar cal = getCalendar();
187+
188+ // set to lenient check.
189+ cal.setLenient(false);
190+ cal.set(year, month - 1, date);
191+ // Call getTime() to do validate.
192+ cal.getTime();
193+ return true;
194+ } catch (IllegalArgumentException e) {
195+ return false;
196+ }
197+ }
198+
199+ /**
200+ * Move to last day of month.
201+ *
202+ * @param cal Target calendar object to move.
203+ */
204+ public static void moveToLastDayOfMonth(final Calendar cal) {
205+ // Move to next month.
206+ cal.add(Calendar.MONTH, 1);
207+ // Move to first day of month.
208+ cal.set(Calendar.DAY_OF_MONTH, 1);
209+ // Move to previous day.
210+ cal.add(Calendar.DATE, -1);
211+
212+ // Now, cal is moved to last day of month.
213+ }
214+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Show on old repository browser