List of usage examples for java.util Calendar clone
@Override
public Object clone()
From source file:net.siveo.virtualization.vmware.Main.java
public void getResourcePoolPerf() throws VMwareException { // ========================================================================================= // Cration de la connexion au VirtualCenter // ========================================================================================= //String hostName="192.168.101.79"; //VMware.create(hostName,443,"svc-siveo","S1v30d6m3",false,-1L,1000L); //String hostName="vsphere-4-1"; //VMware.create(hostName,443,"Administrateur","Gd@jJ2009$",false,-1L); String hostName = "10.251.5.14"; VMware.create(hostName, 443, "SIVEO\\Administrateur", "Clmdp@09", false, -1L); //String hostName="192.168.101.179"; //VMware.create(hostName,443,"svc-siveodev","KzT$g#h4",false,-1L); WSManager wsManager = WSManager.getInstance(); WS ws = wsManager.getWS(hostName);//w ww . ja va 2 s .c om // ========================================================================================= // // ========================================================================================= SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSSS"); Calendar curTime = null; try { curTime = ws.getVIMService().getService().currentTime(ws.getVIMService().getServiceInstance()); } catch (RuntimeFault e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (RemoteException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //System.out.println("curTime: "+curTime); //System.out.println("curTime: "+curTime.getTime()); //System.out.println("curTime timeZone: "+curTime.getTimeZone()); //System.out.println("curTime timeZone: "+curTime.getTimeZone().getDisplayName()); //System.out.println("curTime timeZone id: "+curTime.getTimeZone().getID()); //System.out.println("curTime lenient: "+curTime.isLenient()); Calendar startTime = (Calendar) curTime.clone(); //startTime.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY)-1); startTime.set(Calendar.HOUR_OF_DAY, 23); startTime.set(Calendar.MINUTE, 30); startTime.set(Calendar.SECOND, 00); startTime.set(Calendar.MILLISECOND, 00); startTime.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY) - 1); Calendar endTime = (Calendar) curTime.clone(); endTime.set(Calendar.HOUR_OF_DAY, 23); endTime.set(Calendar.MINUTE, 59); endTime.set(Calendar.SECOND, 59); endTime.set(Calendar.MILLISECOND, 00); endTime.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY) - 1); startTime.roll(Calendar.DATE, -2); endTime.roll(Calendar.DATE, -1); System.out.println("StartTime: " + dateFormat.format(startTime.getTime())); System.out.println("EndTime: " + dateFormat.format(endTime.getTime())); // ========================================================================================= // // ========================================================================================= ArrayList<TreeNode> listOfTreeNodes = new ArrayList<TreeNode>(); ArrayList<ResourcePool> listOfResourcePools = new ArrayList<ResourcePool>(); ResourcePoolManager resourcePoolManager = ResourcePoolManager.getInstance(); ResourcePool resourcePool = resourcePoolManager.getResourcePoolByName(hostName, "ResourcePool1_1"); //listOfResourcePools.add(resourcePool); listOfTreeNodes.add(resourcePool); System.out.println("ResourcePool: " + resourcePool.getMor().get_value()); // Initialize the Performance Engine PerformanceManager performanceManager = PerformanceManager.getInstance(); performanceManager.init(hostName); // ========================================================================================= // // ========================================================================================= PerfMetricId[] arrOfPerfMetricIds = null; arrOfPerfMetricIds = performanceManager.getAvailablePerfCounter(resourcePool, null, null, 300); // Get the Performance Counters PerfMetricId[] arrayOfPerfMetricIds = null; PerfQuerySpec[] arrayOfPerfQuerySpecs = null; HashMap<Integer, PerfCounterInfo> mapOfPerfCounterInfoByID = performanceManager .getMapOfPerfCounterInfoByIdByVC().get(hostName); arrayOfPerfMetricIds = performanceManager .getPerfMetricFromCounterNames(this.getResourcePoolPerformanceCounters(), false, hostName); ////arrayOfPerfQuerySpecs=performanceManager.getResourcePoolQueryPerfSpec(listOfResourcePools,arrayOfPerfMetricIds,startTime,endTime); //arrayOfPerfQuerySpecs=performanceManager.getQueryPerfSpec(listOfTreeNodes,arrayOfPerfMetricIds,startTime,endTime,1800); arrayOfPerfQuerySpecs = performanceManager.getQueryPerfSpec(listOfTreeNodes, arrayOfPerfMetricIds, null, null, 1800); PerfCounterInfo perfCounterInfo = null; for (PerfMetricId perfMetricId : arrayOfPerfMetricIds) { if (mapOfPerfCounterInfoByID.containsKey(perfMetricId.getCounterId())) { perfCounterInfo = mapOfPerfCounterInfoByID.get(perfMetricId.getCounterId()); System.out.println("Metric: " + perfMetricId.getCounterId() + "/" + perfCounterInfo.getGroupInfo().getKey() + "." + perfCounterInfo.getNameInfo().getKey() + "." + perfCounterInfo.getRollupType().name()); } } performanceManager.getPerf(hostName, arrayOfPerfQuerySpecs); // ========================================================================================= // Destruction de la connexion au VirtualCenter // ========================================================================================= VMware.delete(hostName); }
From source file:net.siveo.virtualization.vmware.Main.java
public void performanceCounter() throws VMwareException { //String hostName="vsphere-4-1"; //VMware.create(hostName,443,"Administrateur","Gd@jJ2009$",false,-1L,1000L); String hostName = "10.0.12.6"; VMware.create(hostName, 443, "siveo_admin", "s1v3oP0C", false, -1L, 1000L); //String hostName="10.251.5.14"; //VMware.create(hostName,443,"Administrateur","Clmdp@09",false,-1L,1000L); WSManager wsManager = WSManager.getInstance(); WS ws = wsManager.getWS(hostName);/* w w w . j a v a 2s . com*/ PerformanceManager performanceManager = PerformanceManager.getInstance(); Calendar curTime = null; try { curTime = ws.getVIMService().getService().currentTime(ws.getVIMService().getServiceInstance()); } catch (RuntimeFault e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (RemoteException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //System.out.println("curTime: "+curTime); //System.out.println("curTime: "+curTime.getTime()); //System.out.println("curTime timeZone: "+curTime.getTimeZone()); //System.out.println("curTime timeZone: "+curTime.getTimeZone().getDisplayName()); //System.out.println("curTime timeZone id: "+curTime.getTimeZone().getID()); //System.out.println("curTime lenient: "+curTime.isLenient()); Calendar startTime = (Calendar) curTime.clone(); //startTime.roll(Calendar.DATE, -1); //startTime.roll(Calendar.DATE, -4); //startTime.add(Calendar.HOUR_OF_DAY, -30); //startTime.add(Calendar.MINUTE, -70); //startTime.add(Calendar.MINUTE, -60); startTime.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY) - 1); /* startTime.set(Calendar.HOUR_OF_DAY, startTime.get(Calendar.HOUR_OF_DAY)-1); startTime.set(Calendar.MINUTE, 00); startTime.set(Calendar.SECOND, 00); startTime.set(Calendar.MILLISECOND, 00); //startTime.s //startTime.set(Calendar.DAY_OF_MONTH,6); //startTime.set(Calendar.MONTH,5); */ //System.out.println("StartTime timezone: "+startTime.getTimeZone()); //System.out.println("StartTime timezone: "+startTime.getTimeZone().getDisplayName()); //System.out.println("StartTime timezone id: "+startTime.getTimeZone().getID()); //System.out.println("StartTime lenient: "+startTime.isLenient()); Calendar endTime = (Calendar) curTime.clone(); //endTime.roll(Calendar.DATE, -1); //endTime.roll(Calendar.DATE, -3); /* //endTime.set(Calendar.HOUR_OF_DAY, endTime.get(Calendar.HOUR_OF_DAY)-1); endTime.set(Calendar.MINUTE, 00); endTime.set(Calendar.SECOND, 00); endTime.set(Calendar.MILLISECOND, 00); //endTime.set(Calendar.DAY_OF_MONTH,6); //endTime.set(Calendar.MONTH,5); */ //System.out.println("StartTime: "+startTime.toString()); //System.out.println("EndTime: "+endTime); ArrayList<TreeNode> listOfTreeNode = new ArrayList<TreeNode>(); HostManager hostManager = HostManager.getInstance(); Host host = hostManager.getHostByName("tc3-cli-esx01.hosts.vdc", hostName); //listOfTreeNode.add(host); //host=hostManager.getHostByName("tc3-cli-esx02.hosts.vdc", hostName); //listOfTreeNode.add(host); DataStoreManager dataStoreManager = DataStoreManager.getInstance(); //Datastore ds=dataStoreManager.getDatastoreByID("datastore-1194", hostName); Datastore ds = dataStoreManager.getDatastoreByName("ds-EMC_TC3-siveo_poc-Grodaf", hostName); DatacenterManager datacenterManager = DatacenterManager.getInstance(); Datacenter dc = datacenterManager.getDatacenterByName("Siveo", hostName); VMManager vmManager = VMManager.getInstance(); //VirtualMachine vm=vmManager.getVmByName(hostName, "Tevst_VM_Grodaf"); VirtualMachine vm = vmManager.getVmByName(hostName, "TestVM"); startTime.roll(Calendar.DATE, -1); //endTime.roll(Calendar.DATE, -1); System.out.println( "Ecart entre dbut et fin: " + (endTime.getTime().getTime() - startTime.getTime().getTime())); ResourceManager resourceManager = ResourceManager.getInstance(); ClusterComputeResource cluster = resourceManager.getClusterComputeResourceByName("POC-Siveo", hostName); ManagedObjectReference mor = new ManagedObjectReference(); mor.setType("ClusterComputeResource"); mor.set_value("domain-c1066"); ResourcePoolManager resourcePoolManager = ResourcePoolManager.getInstance(); ResourcePool rs = resourcePoolManager.getResourcePoolByName(hostName, "TestResourcePool"); PerfProviderSummary perfProviderSummary = performanceManager.buildPerformance(rs); System.out.println("Historical Statistics: " + perfProviderSummary.isSummarySupported()); System.out.println("Realtime Statistics: " + perfProviderSummary.isCurrentSupported()); System.out.println("Refresh Rate: " + perfProviderSummary.getRefreshRate()); ArrayList<String> listOfClusterCounters = new ArrayList<String>(); listOfClusterCounters.add("clusterServices.effectivecpu.average"); listOfClusterCounters.add("clusterServices.effectivemem.average"); listOfClusterCounters.add("clusterServices.failover.latest"); listOfTreeNode.add(cluster); //performanceManager.getPerformanceDS(listOfTreeNode, PerformanceMetric.CLUSTER_COUNTERS, startTime, null,300); performanceManager.getPerformance(listOfTreeNode, listOfClusterCounters, null, null, 300); //performanceManager.getPerformance(ds,null, null, null, null,300); performanceManager.getPerformance(rs, null, null, null, null, 300); //performanceManager.getPerformance(cluster,"clusterServices.effectivecpu.average", null, null, null,300); //performanceManager.getPerformance(cluster,"clusterServices.effectivemem.average", null, null, null,300); //performanceManager.getPerformance(cluster,"clusterServices.cpufairness.latest", null, null, null,300); //performanceManager.getPerformance(cluster,"clusterServices.failover.latest", null, null, null,300); //performanceManager.getPerformance(cluster,"clusterServices.memfairness.latest", null, null, null,300); //performanceManager.getPerformance(ds,"disk.used.latest", null, startTime, null,300); //performanceManager.getPerformance(ds,"disk.capacity.latest", null, startTime, null); //performanceManager.getPerformance(ds,null, null, startTime, endTime); /* HashMap<Integer,Counter> listOfCounters=performanceManager.getAllCounters(hostName); String counterName=null; String counterUpperName=null; for (Map.Entry<Integer,Counter> counterbyId:listOfCounters.entrySet()) { counterName=counterbyId.getValue().getGroupKey()+"."+counterbyId.getValue().getNameKey()+"."+counterbyId.getValue().getRollupType(); counterUpperName=counterName.toUpperCase(); counterUpperName=counterUpperName.replace(".", "_"); System.out.println("public static String "+ counterUpperName+"="+"\""+counterName+"\";"); } */ /* ArrayList<Counter> listOfCounters=performanceManager.getCountersByLevel(hostName, 4); for (Counter counter:listOfCounters) { System.out.println(counter.getGroupKey()+"."+counter.getNameKey()+"."+counter.getRollupType()+";"+ counter.getGroupKey()+";"+counter.getGroupName()+";"+ counter.getId()+";"+counter.getNameKey()+";"+counter.getName()+";"+counter.getDescription()+ ";"+counter.getRollupType()+";"+counter.getStatisticsType()+";"+counter.getUnitKey()+";"+counter.getUnit()+ ";"+counter.getUnitDescription()); } HashMap<String, HistoricalInterval> mapOfHistoInterval=performanceManager.getMapOfHistoricalIntervalByName(); for (Map.Entry<String, HistoricalInterval> histo:mapOfHistoInterval.entrySet()) { System.out.println("- Name: "+histo.getKey()+" / Sampling Period: "+histo.getValue().getSamplingPeriod()); } */ //VMManager vmManager=VMManager.getInstance(); //VirtualMachine vm=vmManager.getVmByName(hostName, "Test_VM_Grodaf"); //ResourceManager resourceManager=ResourceManager.getInstance(); //ClusterComputeResource clusterComputeResource=resourceManager.g //HostManager hostManager=HostManager.getInstance(); //Host host=hostManager.getHostByName("esx01.siveo.net", hostName); //Host host=hostManager.getHostByName("tc3-cli-esx01.hosts.vdc", hostName); //tc3-cli-esx01.hosts.vdc ////performanceManager.getPerformance(host, null, null,startTime,endTime); //performanceManager.getPerformance(host, null, null,null,null); //performanceManager.getPerformance(vm, null, null,null,null); //performanceManager.getPerformance(host, null, null); //performanceManager.getPerformance(host, "cpu.usagemhz.maximum", null); //performanceManager.getPerformance(host, "disk.usage.maximum", null); /* PerformanceDescription performanceDescription=null; performanceDescription=(PerformanceDescription)MorManager.getMorPorperty(ws, ws.getPerformanceManager(), new String[] {"description"}); if (performanceDescription!=null) { System.out.println("***** Counter Type *****"); for (ElementDescription elementDescription:performanceDescription.getCounterType()) { System.out.println("- Key: "+elementDescription.getKey()); System.out.println("- Label: "+elementDescription.getLabel()); System.out.println("- Summary: "+elementDescription.getSummary()); System.out.println(""); } System.out.println("***** Statistic Type *****"); for (ElementDescription elementDescription:performanceDescription.getStatsType()) { System.out.println("- Key: "+elementDescription.getKey()); System.out.println("- Label: "+elementDescription.getLabel()); System.out.println("- Summary: "+elementDescription.getSummary()); System.out.println(""); } } */ /* ArrayOfPerfInterval arrayOfPerfInterval=null; arrayOfPerfInterval=(ArrayOfPerfInterval)MorManager.getMorPorperty(ws, ws.getPerformanceManager(), new String[] {"historicalInterval"}); if (arrayOfPerfInterval!=null) { System.out.println("***** Performance Interval *****"); for (PerfInterval perfInterval:arrayOfPerfInterval.getPerfInterval()) { System.out.println("-Key: "+perfInterval.getKey()); System.out.println("-Name: "+perfInterval.getName()); System.out.println("-Level: "+perfInterval.getLevel()); System.out.println("-Length: "+perfInterval.getLength()); System.out.println("-Sampling Period: "+perfInterval.getSamplingPeriod()); System.out.println(""); } } */ /* HashMap<Integer, Counter> mapOfAllCounters=performanceManager.getAllCounters(hostName); Counter counter=null; for (Map.Entry<Integer, Counter> counterbyID:mapOfAllCounters.entrySet()) { counter=counterbyID.getValue(); System.out.println(counterbyID.getKey()+";"+counter.getGroupKey()+"."+counter.getNameKey()+"."+counter.getRollupType()+";"+counter.getLevel()+";"+counter.getName()+";"+counter.getDescription()+";"+counter.getStatisticsType()+";"+counter.getUnitKey()+";"+counter.getUnit()+";"+counter.getUnitDescription()); } */ // HostManager hostManager=HostManager.getInstance(); // Host host=hostManager.getHostByName("tc3-cli-esx01.hosts.vdc", hostName); // VMManager vmManager=VMManager.getInstance(); // VirtualMachine vm=vmManager.getVmByName(hostName, "ESSAI2"); // HashMap<Integer, Counter> mapOfCOunterByID=null; // mapOfCOunterByID=performanceManager.getAllCounters(hostName); /* System.out.println("***** Performance Counters *****\n"); for (Map.Entry<Integer, Counter> counterByID:mapOfCOunterByID.entrySet()) { System.out.println("-ID: "+counterByID.getValue().getId()); System.out.println("-Group Key: "+counterByID.getValue().getGroupKey()); System.out.println("-Group Name: "+counterByID.getValue().getGroupName()); System.out.println("-Level: "+counterByID.getValue().getLevel()); System.out.println("-Name Key: "+counterByID.getValue().getNameKey()); System.out.println("-Name: "+counterByID.getValue().getName()); System.out.println("-Description: "+counterByID.getValue().getDescription()); System.out.println("-Rollup Type: "+counterByID.getValue().getRollupType()); System.out.println("-Statistics Type: "+counterByID.getValue().getStatisticsType()); System.out.println("-Unit Key: "+counterByID.getValue().getUnitKey()); System.out.println("-Unit: "+counterByID.getValue().getUnit()); System.out.println("-Unit Description: "+counterByID.getValue().getUnitDescription()); System.out.println(""); } */ // System.out.println("***** Performance Provider *****\n"); // performanceManager.getPerformanceProvider(vm); //performanceManager.getPerformanceProvider(vm); // System.out.println("***** Available Performance Counters *****\n"); // performanceManager.getAvailablePerfCounter(vm); //System.out.println("***** Available Performance Counters *****\n"); //performanceManager.getAvailablePerfCounter(host); //System.out.println("\n***** Performance *****\n"); //performanceManager.getPerformance(host); /* ArrayOfPerfCounterInfo arrOfPerfCounterInfo=null; PerfCounterInfo[] counterInfoArr=null; arrOfPerfCounterInfo=(ArrayOfPerfCounterInfo)MorManager.getMorPorperty(ws, ws.getPerformanceManager(), new String[] {"perfCounter"}); if (arrOfPerfCounterInfo!=null && arrOfPerfCounterInfo.getPerfCounterInfo() !=null && arrOfPerfCounterInfo.getPerfCounterInfo().length>0) { counterInfoArr=arrOfPerfCounterInfo.getPerfCounterInfo(); int nb=1; String level=null; for (PerfCounterInfo perfcounterInfo:counterInfoArr) { if (perfcounterInfo.getLevel()!=null) { level=String.valueOf(perfcounterInfo.getLevel()); //System.out.println("Level: "); //System.out.println("- value: "+perfcounterInfo.getLevel()); } else level="N/A"; System.out.println(perfcounterInfo.getGroupInfo().getKey()+";"+perfcounterInfo.getGroupInfo().getLabel()+";"+perfcounterInfo.getGroupInfo().getSummary()+";"+ perfcounterInfo.getKey()+";"+level+";"+perfcounterInfo.getNameInfo().getKey()+";"+perfcounterInfo.getNameInfo().getLabel()+";"+perfcounterInfo.getNameInfo().getSummary()+ ";"+perfcounterInfo.getRollupType().name()+";"+perfcounterInfo.getStatsType().name()+";"+perfcounterInfo.getUnitInfo().getKey()+";"+perfcounterInfo.getUnitInfo().getLabel()+ ";"+perfcounterInfo.getUnitInfo().getSummary() ); // System.out.println("\nN"+nb++); // System.out.println("GroupInfo"); // System.out.println("- Key: "+perfcounterInfo.getGroupInfo().getKey()); // System.out.println("- Label : "+perfcounterInfo.getGroupInfo().getLabel()); // System.out.println("- Summary: "+perfcounterInfo.getGroupInfo().getSummary()); // System.out.println("Key: "); // System.out.println("- value: "+perfcounterInfo.getKey()); // if (perfcounterInfo.getLevel()!=null) // { // level=String.valueOf(perfcounterInfo.getLevel()); // System.out.println("Level: "); // System.out.println("- value: "+perfcounterInfo.getLevel()); // } // // System.out.println("NameInfo:"); // System.out.println("- Key: "+perfcounterInfo.getNameInfo().getKey()); // System.out.println("- Label : "+perfcounterInfo.getNameInfo().getLabel()); // System.out.println("- Summary: "+perfcounterInfo.getNameInfo().getSummary()); // // System.out.println("RollupType: "); // System.out.println("- value: "+perfcounterInfo.getRollupType().toString()); // // System.out.println("StatsType: "); // System.out.println("- value: "+perfcounterInfo.getStatsType().toString()); // // System.out.println("unitInfo: "); // System.out.println("- Key: "+perfcounterInfo.getUnitInfo().getKey()); // System.out.println("- Label : "+perfcounterInfo.getUnitInfo().getLabel()); // System.out.println("- Summary: "+perfcounterInfo.getUnitInfo().getSummary()); } } */ //PerformanceManagerUnit VMware.delete(hostName); }
From source file:com.clark.func.Functions.java
/** * <p>//from ww w . j ava 2 s.co m * Ceil this date, leaving the field specified as the most significant * field. * </p> * * <p> * For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you * passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was * passed with MONTH, it would return 1 Mar 2002 0:00:00.000. * </p> * * @param date * the date to work with * @param field * the field from <code>Calendar</code> or * <code>SEMI_MONTH</code> * @return the rounded date (a different object) * @throws IllegalArgumentException * if the date is <code>null</code> * @throws ArithmeticException * if the year is over 280 million * @since 2.5 */ public static Calendar ceilingCalendar(Calendar date, int field) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar ceiled = (Calendar) date.clone(); modifyDate(ceiled, field, MODIFY_CEILING); return ceiled; }
From source file:com.clark.func.Functions.java
/** * <p>//from w ww.j a v a 2 s . c o m * Round this date, leaving the field specified as the most significant * field. * </p> * * <p> * For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if this * was passed with HOUR, it would return 28 Mar 2002 14:00:00.000. If this * was passed with MONTH, it would return 1 April 2002 0:00:00.000. * </p> * * <p> * For a date in a timezone that handles the change to daylight saving time, * rounding to Calendar.HOUR_OF_DAY will behave as follows. Suppose daylight * saving time begins at 02:00 on March 30. Rounding a date that crosses * this time would produce the following values: * <ul> * <li>March 30, 2003 01:10 rounds to March 30, 2003 01:00</li> * <li>March 30, 2003 01:40 rounds to March 30, 2003 03:00</li> * <li>March 30, 2003 02:10 rounds to March 30, 2003 03:00</li> * <li>March 30, 2003 02:40 rounds to March 30, 2003 04:00</li> * </ul> * </p> * * @param date * the date to work with * @param field * the field from <code>Calendar</code> or * <code>SEMI_MONTH</code> * @return the rounded date (a different object) * @throws IllegalArgumentException * if the date is <code>null</code> * @throws ArithmeticException * if the year is over 280 million */ public static Calendar roundCalendar(Calendar date, int field) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar rounded = (Calendar) date.clone(); modifyDate(rounded, field, MODIFY_ROUND); return rounded; }
From source file:com.clark.func.Functions.java
/** * <p>/*from ww w .j a v a 2s .co m*/ * Truncate this date, leaving the field specified as the most significant * field. * </p> * * <p> * For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you * passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was * passed with MONTH, it would return 1 Mar 2002 0:00:00.000. * </p> * * @param date * the date to work with * @param field * the field from <code>Calendar</code> or * <code>SEMI_MONTH</code> * @return the rounded date (a different object) * @throws IllegalArgumentException * if the date is <code>null</code> * @throws ArithmeticException * if the year is over 280 million */ public static Calendar truncateCalendar(Calendar date, int field) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar truncated = (Calendar) date.clone(); modifyDate(truncated, field, MODIFY_TRUNCATE); return truncated; }
From source file:com.clark.func.Functions.java
/** * <p>/*from w ww. j av a2s . c o m*/ * This constructs an <code>Iterator</code> over each day in a date range * defined by a focus date and range style. * </p> * * <p> * For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> that * starts with Sunday, June 30, 2002 and ends with Saturday, August 3, 2002, * returning a Calendar instance for each intermediate day. * </p> * * <p> * This method provides an iterator that returns Calendar objects. The days * are progressed using {@link Calendar#add(int, int)}. * </p> * * @param focus * the date to work with * @param rangeStyle * the style constant to use. Must be one of * {@link DateUtils#RANGE_MONTH_SUNDAY}, * {@link DateUtils#RANGE_MONTH_MONDAY}, * {@link DateUtils#RANGE_WEEK_SUNDAY}, * {@link DateUtils#RANGE_WEEK_MONDAY}, * {@link DateUtils#RANGE_WEEK_RELATIVE}, * {@link DateUtils#RANGE_WEEK_CENTER} * @return the date iterator * @throws IllegalArgumentException * if the date is <code>null</code> * @throws IllegalArgumentException * if the rangeStyle is invalid */ public static Iterator<Calendar> iteratorCalendar(Calendar focus, int rangeStyle) { if (focus == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar start = null; Calendar end = null; int startCutoff = Calendar.SUNDAY; int endCutoff = Calendar.SATURDAY; switch (rangeStyle) { case RANGE_MONTH_SUNDAY: case RANGE_MONTH_MONDAY: // Set start to the first of the month start = truncateCalendar(focus, Calendar.MONTH); // Set end to the last of the month end = (Calendar) start.clone(); end.add(Calendar.MONTH, 1); end.add(Calendar.DATE, -1); // Loop start back to the previous sunday or monday if (rangeStyle == RANGE_MONTH_MONDAY) { startCutoff = Calendar.MONDAY; endCutoff = Calendar.SUNDAY; } break; case RANGE_WEEK_SUNDAY: case RANGE_WEEK_MONDAY: case RANGE_WEEK_RELATIVE: case RANGE_WEEK_CENTER: // Set start and end to the current date start = truncateCalendar(focus, Calendar.DATE); end = truncateCalendar(focus, Calendar.DATE); switch (rangeStyle) { case RANGE_WEEK_SUNDAY: // already set by default break; case RANGE_WEEK_MONDAY: startCutoff = Calendar.MONDAY; endCutoff = Calendar.SUNDAY; break; case RANGE_WEEK_RELATIVE: startCutoff = focus.get(Calendar.DAY_OF_WEEK); endCutoff = startCutoff - 1; break; case RANGE_WEEK_CENTER: startCutoff = focus.get(Calendar.DAY_OF_WEEK) - 3; endCutoff = focus.get(Calendar.DAY_OF_WEEK) + 3; break; } break; default: throw new IllegalArgumentException("The range style " + rangeStyle + " is not valid."); } if (startCutoff < Calendar.SUNDAY) { startCutoff += 7; } if (startCutoff > Calendar.SATURDAY) { startCutoff -= 7; } if (endCutoff < Calendar.SUNDAY) { endCutoff += 7; } if (endCutoff > Calendar.SATURDAY) { endCutoff -= 7; } while (start.get(Calendar.DAY_OF_WEEK) != startCutoff) { start.add(Calendar.DATE, -1); } while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) { end.add(Calendar.DATE, 1); } return new DateIterator(start, end); }