So I tried using Date.compareTo * 1000 to get the milliseconds. This, my current implementation, also works but continues to fail the test.
package com.codegym.task.task16.task1605;
import java.util.Date;
/*
Let's talk music
*/
public class Solution {
public static int delay = 1000;
public static void main(String[] args) {
Thread violin = new Thread(new Violin("Player"));
violin.start();
}
public static void sleepNSeconds(int n) {
try {
Thread.sleep(n * delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public interface MusicalInstrument extends Runnable {
Date startPlaying();
Date stopPlaying();
}
public static class Violin implements MusicalInstrument {
private String owner;
public Violin(String owner) {
this.owner = owner;
}
public Date startPlaying() {
System.out.println(this.owner + " is starting to play");
return new Date();
}
public Date stopPlaying() {
System.out.println(this.owner + " is stopping playing");
return new Date();
}
@Override
public void run() {
Date start = startPlaying();
long START = System.currentTimeMillis();
sleepNSeconds(1);
Date stop = stopPlaying();
long STOP = System.currentTimeMillis();
long time = STOP - START;
System.out.println("Played for " + time + " ms");
}
}
}