Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Schedule top / terminate a Java program on a specific time

Is there any way to schedule to stop or terminate a java program on a specific time. I am using java.util.timer. For example at 10pm. I know how to schedule start a java program on a specific time. Problem is I cannot figure out a way to terminate a program.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can create a background thread (in order to not block UI) and inside you can check current hour/minutes/day

GregorianCalendar calendar = new GregorianCalendar (); // creates a new calendar instance 
calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
calendar.get(Calendar.HOUR);        // gets hour in 12h format
calendar.get(Calendar.MONTH);       // gets month number, NOTE this is zero based!
Thread.sleep(60000)                 //sleep 60seconds

if(<rightTime>)
    System.exit(0);

See also https://stackoverflow.com/a/907207/6726261

over 4 years ago · Santiago Trujillo Denunciar

0

You can use TimerTask to do that, here is an example:

  import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class StopApp {
Timer timer = new Timer();
TimerTask StopApp = new TimerTask() {
    @Override
    public void run() {
        System.exit(0);
    }
};
public StopApp() {

//timer.schedule(exitApp, getDateDiff(new Date("get the actual time"), new Date("get the time you want to stop your app"), TimeUnit.SECONDS));
    //Example
    timer.schedule(StopApp, new Date(System.currentTimeMillis()+2*1000));//Exits after 2 sec of starting the app

while(true)

    System.out.println("The App still turn");
}

public static Date getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillies = date2.getTime() - date1.getTime();
    Date date=new Date(diffInMillies);

    return date;//timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}

public static void main(String[] args) {

    new StopApp();

}
}
over 4 years ago · Santiago Trujillo Denunciar

0

Check this doc: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int). System.exit(0); can do what you want.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda