Showing posts with label Calendar. Show all posts
Showing posts with label Calendar. Show all posts

Sunday, October 30, 2011

Find out Number of Days in Month/Year using java

How to find out number of days in a given month and year

Code snippet to find out the numbe rof days in a month.

...
..
/**
 * util method to find the number of day in the given passed month
 * @param month  
 * @return last days of the month
 */
public int getDaysInMonth(Date dt) {

 if (dt == null) dt = new Date();

 Calendar cal = Calendar.getInstance();
 cal.setTime(dt);

 return cal.getActualMaximum(cal.DAY_OF_MONTH);


/**
 * util method to find the last day of the given  year
 * @param month
 * @param year
 * @return last days of the month
 */
public int getDaysInYear(Date dt) {

 if (dt == null) dt = new Date();

 Calendar cal = Calendar.getInstance();
 cal.setTime(dt);

 return cal.getActualMaximum(cal.DAY_OF_YEAR);
}


Main Method to test this


public static void main(String[] args) {
 try {   
   
  System.out.println(" last day os the month..."+ new ReqDXCommonUtil().getDaysInMonth(new Date()));
 } catch (Throwable e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}




Wednesday, June 8, 2011

GMT/IST convertor in java


How to Convert GMT time into IST time using java

 

package com.vels.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


/**
 * @author velmurugan pousel
 * created on Jun 8, 2011 for GMT Convertor
 */


public class GMTConvertor {

 public static void main(String args[]) {

  Calendar cal = Calendar.getInstance();
  cal.set(2011, 06, 8, 12, 0);//year,month,day,hour,minute format


  System.out.println(" IST time" + convertIST(cal.getTime()));


  System.out.println("current GMT time" + convertGMT(new Date()));

 }

 /**
  *  method to covert IST time to GMT
  * @param date is in IST format
  * @return
  */
 public static String convertGMT(Date date) {


 DateFormat gmtFormat = new SimpleDateFormat();
   TimeZone gmtTime = TimeZone.getTimeZone("GMT");
   gmtFormat.setTimeZone(gmtTime);

   return gmtFormat.format(date);

 }

 /**
  * method to convert GMT to IST time
  * @param date is GMT format
  * @return
  */
 public static String convertIST(Date date) {

   DateFormat istFormat = new SimpleDateFormat();
   TimeZone istTime = TimeZone.getTimeZone("Asia/Kolkata");
   istFormat.setTimeZone(istTime);

   return istFormat.format(date);

 }

}
 
 

Thursday, December 2, 2010

Find out Number of Holidays in a month or date range using Java

To findout the number of Holidays in a given date range or in a month

Calendar format should be year, month-1, day

Here is java class to findout the number of holidays of a given data range(two dates)

//HolidayFinder class

package com.hbd.rms.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
* @author velmurugan pousel
* created on Sep 2, 2010 for RMS
* WorkingDaysFinder.java on Sep 2, 2010
*/
public class HolidaysFinder {

/**
* holidaylist holds the list of national/local holidays for the date range
* the format of the calender is (year,month-1,date)
*/
static List holidayList = new ArrayList();

// holiday list
static {
//holidayList.add(new GregorianCalendar(2010, 8, 18));// holiday on the given month
holidayList.add(new GregorianCalendar(2010, 11, 24));// holiday on the given month
}

/**
* check the given date is on holiday list
* @param calendardate
* @return
*/
public static boolean isHoliday(Calendar cal) {
return holidayList.contains(cal);
}

/**
* method to find out number of holidays between the given date range
* @param first
* @param second
* @return
*/
public static int countNumberOfSaturdaysAndSundays(GregorianCalendar first,
GregorianCalendar second) {

int count = 0;
GregorianCalendar currentcalendarday = first;
GregorianCalendar lastcalendarday = second;

while (currentcalendarday.getTime().getTime() < lastcalendarday.getTime().getTime()) {
if (isOnSaturday(currentcalendarday)) {
count++;
} else if (isOnSunday(currentcalendarday)) {
count++;
} else if (isHoliday(currentcalendarday)) {
count++;
}
currentcalendarday.add(Calendar.DATE, 1);
}

if (isSameDay(currentcalendarday, lastcalendarday)) {

if (isOnSaturday(currentcalendarday)) {
count++;
} else if (isOnSunday(currentcalendarday)) {
count++;
} else if (isHoliday(currentcalendarday)) {
count++;
}
}
return count;
}


/**
* check the given date is saturday
* @param calendardate
* @return
*/
public static boolean isOnSaturday(Calendar calendardate) {

if (calendardate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
System.out.println("Debug: "
+ String.valueOf(calendardate.get(Calendar.DATE)) + "-"
+ String.valueOf(calendardate.get(Calendar.MONTH) + 1)
+ "-" + String.valueOf(calendardate.get(Calendar.YEAR))
+ " is a SATURDAY.");
} else {
System.out.println("Debug: "
+ String.valueOf(calendardate.get(Calendar.DATE)) + "-"
+ String.valueOf(calendardate.get(Calendar.MONTH) + 1)
+ "-" + String.valueOf(calendardate.get(Calendar.YEAR))
+ " is not a SATURDAY.");
}

return (calendardate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY);
}


/**
* check the given date is sunday
* @param calendardate
* @return
*/
public static boolean isOnSunday(Calendar calendardate) {

if (calendardate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("Debug: "
+ String.valueOf(calendardate.get(Calendar.DATE)) + "-"
+ String.valueOf(calendardate.get(Calendar.MONTH) + 1)
+ "-" + String.valueOf(calendardate.get(Calendar.YEAR))
+ " is a SUNDAY.");
} else {
System.out.println("Debug: "
+ String.valueOf(calendardate.get(Calendar.DATE)) + "-"
+ String.valueOf(calendardate.get(Calendar.MONTH) + 1)
+ "-" + String.valueOf(calendardate.get(Calendar.YEAR))
+ " is not a SUNDAY.");
}

return (calendardate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
}


public static boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}

public static void main(String args[]) {
// to findout the number of holidays between these two dates
GregorianCalendar firstdate = new GregorianCalendar();
GregorianCalendar seconddate = new GregorianCalendar(2010, 11, 30);
System.out.println(" year..."+ seconddate.get(Calendar.YEAR));
System.out.println("holiday conunt = " + countNumberOfSaturdaysAndSundays(firstdate, seconddate));
}

}

Here is the code to test with two dates(date1,date2)...

To Find out the holidays between these dates

convert the dates into calendar

GregorianCalendar firstdate = new GregorianCalendar();
GregorianCalendar seconddate = new GregorianCalendar();
firstdate.settime(date1);
seconddate .settime(date2);
// call the method

countNumberOfSaturdaysAndSundays(firstdate, seconddate));
...
...