cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for round off the time

former_member192238
Participant
0 Kudos

Hi Experts ,

I have requirement like seconds should truncate from source time if time is 09:08:36 in that 36 sec should be truncated it

should add 1 min 09:09

from source time if time is 09:08:28 in that 36 sec should be truncated it should add 1 min 09:08.

if it is above 30 sec it should round to next min.

if it is less than 30 sec it should round to previous min.

Regards

Praveen

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Praveen,

This can be achieved with java UDF.

You have not mentioned of the case case when it is 30 seconds

Regards,

Aravind

former_member192238
Participant
0 Kudos

Hi Ajnayak,

Up to 29 sec it should round off tp previous min from 30th sec onwards it should round off to next min.

Regards

Praveen

rajasekhar_reddy14
Active Contributor
0 Kudos

Even you can achive this requirement using standard functions also but I prefer UDF.

data---substring(6,2)--------->
                                                       Greater----->ifthen else(use substring function,add function write logic)
                            constant(29)-->

Regards,

Raj

former_member192238
Participant
0 Kudos

Thanks Jagadishwar&Raj,

From source i am getting only time date is not coming

sorce format dd:mm:ss target for required is dd:mm

Regards

Praveen

jagdishwar_b
Active Participant
0 Kudos

check:


public String roundTime(String inputTime, Container container) throws StreamTransformationException{
	String out = "";
	try {
		Date date =new SimpleDateFormat("HH:mm:ss").parse(inputTime); 

		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		int seconds = cal.get(Calendar.SECOND);
		if (seconds >=30) {
			cal.add(Calendar.MINUTE, 1);
		}
		cal.add(Calendar.SECOND, -(seconds));

		out = new SimpleDateFormat("HH:mm").format(cal.getTime());
	} catch (Exception e)
	{
	}
	return out;
 
}

regards,

BJagdishwar.

Former Member
0 Kudos

u can refer this udf also:

execution type: single value

input: var1


String a="";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss"); 
try{
Date dt =sdf.parse(var1); 
Calendar c1=Calendar.getInstance();
c1.setTime(dt);
int hour = c1.get(Calendar.HOUR);
int minute = c1.get(Calendar.MINUTE);
int second = c1.get(Calendar.SECOND);
if (c1.get(Calendar.SECOND) > 30)       
{
a= new Integer(hour).toString() + ":" + new Integer(minute+1).toString();
}
else
{  
a= new Integer(hour).toString() + ":" + new Integer(minute).toString();
}
}
 catch (Exception e)
{	}
return a;

jagdishwar_b
Active Participant
0 Kudos

in the udf given by AmitSri, its not sure how the udf will behave if the input is 10:59:45

Former Member
0 Kudos

>>in the udf given by AmitSri, its not sure how the udf will behave if the input is 10:59:45

valid point...but one more chk (on minutes) will result the desired output


String a="";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss"); 
try{
Date dt =sdf.parse(var1); 
Calendar c1=Calendar.getInstance();
c1.setTime(dt);
int hour = c1.get(Calendar.HOUR);
int minute = c1.get(Calendar.MINUTE);
int second = c1.get(Calendar.SECOND);
if (c1.get(Calendar.SECOND) > 30)       
{
if(c1.get(Calendar.MINUTE)<=58)
a= new Integer(hour).toString() + ":" + new Integer(minute+1).toString();
else
a= new Integer(hour+1).toString() + ":" + "00";
}
else
{  
a= new Integer(hour).toString() + ":" + new Integer(minute).toString();
}
}
 catch (Exception e)
	{
	}
return a;

former_member192238
Participant
0 Kudos

Thanks Jagadish,

My requirement is solved with your UDF.

Regards

Praveen

Answers (1)

Answers (1)

jagdishwar_b
Active Participant
0 Kudos

one of the solution:


public String truncateTime(String inputDateTime, Container container) throws StreamTransformationException{
	String out = "";
	SimpleDateFormat sdf =  new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	try {
		Date date =sdf.parse(inputDateTime); 
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		int seconds = cal.get(Calendar.SECOND);

		if (seconds >=30) {//if required, increment the minute
			cal.add(Calendar.MINUTE, 1);
		}
		cal.add(Calendar.SECOND, -(seconds)); //make seconds back to zero.
		 
		out = sdf.format(cal.getTime());
	}catch (Exception e)
	{
	}
	return out;
}

regards,

BJagdishwar.