|
Groovy example source code file (DateGroovyMethods.java)
The Groovy DateGroovyMethods.java source code/* * Copyright 2003-2011 the original author or authors. * * Licensed 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.codehaus.groovy.runtime; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * This class defines new groovy methods which appear on normal JDK * Date and Calendar classes inside the Groovy environment. */ public class DateGroovyMethods { /** * Support the subscript operator for a Date. * * @param self a Date * @param field a Calendar field, e.g. MONTH * @return the value for the given field, e.g. FEBRUARY * @see java.util.Calendar * @since 1.5.5 */ public static int getAt(Date self, int field) { Calendar cal = Calendar.getInstance(); cal.setTime(self); return cal.get(field); } /** * Convert a Date to a Calendar. * * @param self a Date * @return a Calendar corresponding to the given Date * @since 1.7.6 */ public static Calendar toCalendar(Date self) { Calendar cal = Calendar.getInstance(); cal.setTime(self); return cal; } /** * Support the subscript operator for a Calendar. * * @param self a Calendar * @param field a Calendar field, e.g. MONTH * @return the value for the given field, e.g. FEBRUARY * @see java.util.Calendar * @since 1.7.3 */ public static int getAt(Calendar self, int field) { return self.get(field); } /** * Support the subscript operator for mutating a Calendar. * Example usage: * <pre> * import static java.util.Calendar.* * def cal = Calendar.instance * cal[DAY_OF_WEEK] = MONDAY * cal[MONTH] = MARCH * println cal.time // A Monday in March * </pre> * * @param self A Calendar * @param field A Calendar field, e.g. MONTH * @param value The value for the given field, e.g. FEBRUARY * @see java.util.Calendar#set(int, int) * @since 1.7.3 */ public static void putAt(Calendar self, int field, int value) { self.set(field, value); } /** * Support the subscript operator for mutating a Date. * * @param self A Date * @param field A Calendar field, e.g. MONTH * @param value The value for the given field, e.g. FEBRUARY * @see #putAt(java.util.Calendar, int, int) * @see java.util.Calendar#set(int, int) * @since 1.7.3 */ public static void putAt(Date self, int field, int value) { Calendar cal = Calendar.getInstance(); cal.setTime(self); putAt(cal, field, value); self.setTime(cal.getTimeInMillis()); } /** * Support mutating a Calendar with a Map. * <p/> * The map values are the normal values provided as the * second parameter to <code>java.util.Calendar#set(int, int). * The keys can either be the normal fields values provided as * the first parameter to that method or one of the following Strings: * <table border="1" cellpadding="4"> * <tr> | month | Calendar.MONTH | * <tr>date | Calendar.DATE | * <tr>hourOfDay | Calendar.HOUR_OF_DAY | * <tr>minute | Calendar.MINUTE | * <tr>second | Calendar.SECOND | * </table> * Example usage: * <pre> * import static java.util.Calendar.* * def cal = Calendar.instance * def m = [:] * m[YEAR] = 2010 * m[MONTH] = DECEMBER * m[DATE] = 25 * cal.set(m) * println cal.time // Christmas 2010 * * cal.set(year:2011, month:DECEMBER, date:25) * println cal.time // Christmas 2010 * </pre> * * @param self A Calendar * @param updates A Map of Calendar keys and values * @see java.util.Calendar#set(int, int) * @see java.util.Calendar#set(int, int, int, int, int, int) * @since 1.7.3 */ public static void set(Calendar self, Map<Object, Integer> updates) { for (Map.Entry<Object, Integer> entry : updates.entrySet()) { Object key = entry.getKey(); if (key instanceof String) key = CAL_MAP.get(key); if (key instanceof Integer) self.set((Integer) key, entry.getValue()); } } /** * Support creating a new Date having similar properties to * an existing Date (which remains unaltered) but with * some fields updated according to a Map of changes. * <p/> * Example usage: * <pre> * import static java.util.Calendar.YEAR * def now = Calendar.instance * def nextYear = now[YEAR] + 1 * def oneYearFromNow = now.updated(year: nextYear) * println now.time * println oneYearFromNow.time * </pre> * * @param self A Calendar * @param updates A Map of Calendar keys and values * @return The newly created Calendar * @see java.util.Calendar#set(int, int) * @see java.util.Calendar#set(int, int, int, int, int, int) * @see #set(java.util.Calendar, java.util.Map) * @since 1.7.3 */ public static Calendar updated(Calendar self, Map<Object, Integer> updates) { Calendar result = (Calendar) self.clone(); for (Map.Entry<Object, Integer> entry : updates.entrySet()) { Object key = entry.getKey(); if (key instanceof String) key = CAL_MAP.get(key); if (key instanceof Integer) result.set((Integer) key, entry.getValue()); } return result; } /** * Support mutating a Date with a Map. * <p/> * Example usage: * <pre> * import static java.util.Calendar.YEAR * def date = new Date() * def nextYear = date[YEAR] + 1 * date.set(year: nextYear) * println date * </pre> * * @param self A Date * @param updates A Map of Calendar keys and values * @see java.util.Calendar#set(int, int) * @see #set(java.util.Calendar, java.util.Map) * @since 1.7.3 */ public static void set(Date self, Map<Object, Integer> updates) { Calendar cal = Calendar.getInstance(); cal.setTime(self); set(cal, updates); self.setTime(cal.getTimeInMillis()); } /** * Support creating a new Date having similar properties to * an existing Date (which remains unaltered) but with * some fields updated according to a Map of changes. * <p/> * Example usage: * <pre> * import static java.util.Calendar.YEAR * def today = new Date() * def nextYear = today[YEAR] + 1 * def oneYearFromNow = today.updated(year: nextYear) * println today * println oneYearFromNow * </pre> * * @param self A Date * @param updates A Map of Calendar keys and values * @return The newly created Date * @see java.util.Calendar#set(int, int) * @see #set(java.util.Date, java.util.Map) * @see #set(java.util.Calendar, java.util.Map) * @see #updated(java.util.Calendar, java.util.Map) * @since 1.7.3 */ public static Date updated(Date self, Map<Object, Integer> updates) { Calendar cal = Calendar.getInstance(); cal.setTime(self); set(cal, updates); return cal.getTime(); } private static final Map<String, Integer> CAL_MAP = new HashMap
... this post is sponsored by my books ... | |
![]() #1 New Release! |
![]() FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.