Sometimes we need to create scripts that checks if the current date is the first monday of the month, or something like this.

See here some code tricks to operate with Date/Calendar classes quickly

import java.text.SimpleDateFormat
import java.sql.Timestamp
import java.text.DateFormat
import java.util.Date

//To get the actual DATE
def today = new Date().format('yyyy-MM-dd');
def date = new Date();
def day = new Date().format('dd');
def year = new Date().format('yyyy');
def month = new Date().format('MM');

//To set a fixed DATE
def fecha = '2016-02-01';
def today = new Date().parse('yyyy-MM-dd', fecha).format('yyyy-MM-dd');
def date = new Date().parse('yyyy-MM-dd', fecha);
def day = new Date().parse('yyyy-MM-dd', fecha).format('dd');
def year = new Date().parse('yyyy-MM-dd', fecha).format('yyyy');
def month = new Date().parse('yyyy-MM-dd', fecha).format('MM');

//To get the WEEKDAY
def weekday = Date.parse('yyyy-MM-dd', today)[Calendar.DAY_OF_WEEK];

//To get in a variable the DATE_OF_THE_FIRST_MONDAY_OF_THE_CURRENT_MONTH
Calendar cacheCalendar = Calendar.instance
cacheCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cacheCalendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
cacheCalendar.set(Calendar.MONTH, (Integer.parseInt(month)-1));
cacheCalendar.set(Calendar.YEAR, Integer.parseInt(year));
def fecha_primer_lunes = cacheCalendar.time.format("yyyy-MM-dd")

//If Today =  DATE_OF_THE_FIRST_MONDAY_OF_THE_CURRENT_MONTH
if ( today == fecha_primer_lunes) {
//We can do estrange things, for example
//To get the DATE_OF_THE_FIRST_DAY_OF_THE_PREVIOS_MONTH
def datefrom = (date - 7).format('yyyy-MM-01');
...
}

//To get the WEEK_OF_THE_YEAR
Calendar cal = Calendar.getInstance(new Locale("es", "ES"));
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);

//Function to calculate the working days between two dates
int calculate_working_days (Date date_start, Date date_end ) {
	def date_limit = date_end.format('yyyy-MM-dd');
	def date_aux = date_start.format('yyyy-MM-dd');
    def date = date_start;
	def i = 0;
    def days = 0;
    def weekday = null;
	while ( date_aux != date_limit ) {
        weekday = Date.parse('yyyy-MM-dd', date_aux)[Calendar.DAY_OF_WEEK];
        if (!( weekday == Calendar.SUNDAY || weekday == Calendar.SATURDAY )) {
        	days++;
        }
    	date = date + 1;
        date_aux = date.format('yyyy-MM-dd');
        i++;
    }
    return days;
}

//to get the working days of the previous week if it's sunday
if ( weekday == Calendar.SUNDAY ) {
calculate_working_days ( Date.parse('yyyy-MM-dd', (date - 7).format('yyyy-MM-dd')),  Date.parse('yyyy-MM-dd', (date - 0).format('yyyy-MM-dd')))
}

//to get the working days of the previous month if it's day 1 of the month
if ( day == '1' || day == '01') {
calculate_working_days ( Date.parse('yyyy-MM-dd', (date - 1).format('yyyy-MM-01')),  Date.parse('yyyy-MM-dd', (date - 1).format('yyyy-MM-dd')))
}
Advertisements
Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s