Groovy web console

subscribe to the feed Subscribe
to this
site
Simple Date and Calendar (via #groovywebconsole)
tweet this snippet Tweet
this
script

Simple Date and Calendar

Published 8 months ago by Burcin Oryan
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.Calendar; 
import java.text.SimpleDateFormat;

def i_startdate = '2019-10-01' ;
def i_period = '90' ;

// Period values
// '90'	90 days
// '3'	Three (3) months
// '4'	Four (4) months
// '6'	Six (6) months
// 'NA'	Not applicable

def e_probationenddate;

// convert string to date
SimpleDateFormat formatIn = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatOut = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
Calendar calendar = new GregorianCalendar();
Date dateIn = formatIn.parse(i_startdate); 
  
calendar.setTime(dateIn);

switch (i_period) {
	case '90':
  calendar.add(Calendar.DAY_OF_YEAR,90);
  calendar.add(Calendar.DAY_OF_YEAR,-1);
  dateOut = calendar.getTime();
  e_probationenddate = formatOut.format(dateOut);
  break;
	
	case '3': 
  calendar.add(Calendar.MONTH,3);
  calendar.add(Calendar.DAY_OF_YEAR,-1);
  dateOut = calendar.getTime();	
  e_probationenddate = formatOut.format(dateOut);
  break;
  
	case '4': 
  calendar.add(Calendar.MONTH,4);
  calendar.add(Calendar.DAY_OF_YEAR,-1);  
  dateOut = calendar.getTime();	
  e_probationenddate = formatOut.format(dateOut);
  break;
  
	case '6':
  calendar.add(Calendar.MONTH,6);
  calendar.add(Calendar.DAY_OF_YEAR,-1);  
  dateOut = calendar.getTime();	
  e_probationenddate = formatOut.format(dateOut);
  break;
  
	default: 
  // NA and everything else will return blank
  break;
}