on 02-27-2012 7:21 AM
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
Hi Praveen,
This can be achieved with java UDF.
You have not mentioned of the case case when it is 30 seconds
Regards,
Aravind
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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;
>>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;
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.