Quantcast
Channel: Gradle Forums - Latest posts
Viewing all articles
Browse latest Browse all 20645

Get duration of a task in java

$
0
0

So since no one had an answer for me, I finally figured it out by myself. In case anyone else needs this, here’s my solution, might be not the best one but it works for me. Just add this Listener to your BuildLauncher:

class Listener implements ProgressListener{
List<String> tasks = null;
Date now;
String task;
String current;
long diff;

@Override
public void statusChanged(ProgressEvent event) {
	if(event.getDescriptor().toString().startsWith("Task :")) {
	now = new Date();
	current = event.getDescriptor().toString().replace("Task :", "");
		if(current.equals(task)) {
			diff = now.getTime() - diff;
			System.out.println("Name: " + current +   "	  Duration: " + calcDiff(diff) + "		exakt: " + diff);
		}else {
			task = current;
			diff = now.getTime();
		}	
	}
}


public String calcDiff(long diff) {
	if(diff < 1000) {
		return (diff + " ms");
	} else if((1000 <= diff) && (diff < 60000)){
		double i = (double) diff / 1000;
		return(i  + " s");
	} else {
		double i = ((double)diff / 1000) / 60;
		i = round(i, 4);
		return(i  + " min");
	}
}

Viewing all articles
Browse latest Browse all 20645

Trending Articles