Java While随时间循环

Java While随时间循环,java,string,time,while-loop,Java,String,Time,While Loop,如何使用while循环在特定时间之间执行特定任务(例如使用伪代码: 时间>05:00和时间=05)和&(小时您可以比较日期,直到满足条件 例子: publicstaticvoidmain(字符串[]args){ 扫描仪s=新的扫描仪(System.in); 字符串格式=时间\ U格式; 布尔值isDateOk=false; 日期1=新日期(); 日期2=新日期(); 试一试{ Date1=新的SimpleDateFormat(时间\格式).parse(“05:00”); Date2=新的Simp

如何使用while循环在特定时间之间执行特定任务(例如使用伪代码:

时间>05:00和时间<16:59

我知道我需要在输入之后将这些更改为int,我只是不确定如何执行实际的while循环

我已通过以下方式将它们更改为ints:

String hoursString = time.substring(0,1);
String minutesString = time.substring(3,4);

int hours = Integer.parseInt(hoursString);

int minutes = Integer.parseInt(minutesString);
编辑:

非常感谢大家的帮助,我选择了if语句的另一个方向,用于检查当时的小于和大于条件。:)


如果((小时>=05)和&(小时您可以比较日期,直到满足条件

例子:
publicstaticvoidmain(字符串[]args){
扫描仪s=新的扫描仪(System.in);
字符串格式=时间\ U格式;
布尔值isDateOk=false;
日期1=新日期();
日期2=新日期();
试一试{
Date1=新的SimpleDateFormat(时间\格式).parse(“05:00”);
Date2=新的SimpleDateFormat(TIME_格式).parse(“16:59”);
}捕获(解析异常e1){
}
字符串inp=“”;
SimpleDataFormat sdf=新的SimpleDataFormat(格式);
而(!isDateOk){
System.out.println(“请在此fomrat中给出所需的时间HH:mm…”);
inp=s.nextLine();
试一试{
日期=sdf.parse(inp);
如果(日期比较到(日期1)>0和日期比较到(日期2)<0){
isDateOk=true;
}
//date.compareTo(日期1)//如果为负数,将返回一个整数
//表示日期时间大于日期1
}捕获(解析异常){
System.err.println(“无效日期…”);
}
}
//暂时
System.out.println(“给定日期正常”);
}
定时器 您不想使用while循环。while循环将锁定您的UI。您应该改为使用while循环

基本上,你应该像这篇文章中所说的那样:


然后,您只需在结束时间结束计时器。当然,在您的特定情况下,您只需使用所需的特定日期初始化date对象,而不是在将来使用特定的毫秒数。

在Java中,同时使用表达式。因此,您不能使用大于。我认为在Java中,在while用作条件的表达式。你能编辑要添加的问题吗?你是如何在计算小时和分钟时使用它的?谢谢。
计时器是一种很好的学习方法,因为它提到你应该逐步使用,特别是.Search堆栈溢出来获取更多信息。
if ((hours >= 05) && (hours <= 16) { do stuff}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String format = TIME_FORMAT;
boolean isDateOk = false;
Date theDate1 = new Date();
Date theDate2 = new Date();
try {
    theDate1 = new SimpleDateFormat(TIME_FORMAT).parse("05:00");
    theDate2 = new SimpleDateFormat(TIME_FORMAT).parse("16:59");
} catch (ParseException e1) {
}
String inp = "";
SimpleDateFormat sdf = new SimpleDateFormat(format);
while (!isDateOk) {
    System.out.println("Please give the desired time in this fomrat HH:mm ...");
    inp = s.nextLine();
    try {
    Date date = sdf.parse(inp);
    if (date.compareTo(theDate1) > 0 && date.compareTo(theDate2) < 0) {
        isDateOk = true;
    }
    // date.compareTo(theDate1) // will return an int, if negative
    // means date time is bigger than theDate1
    } catch (ParseException e) {
    System.err.println("invalid date...");
    }
}
// out of the while
System.out.println("the given date was ok");
}
   import java.sql.Date; import java.util.Timer; import 
  java.util.TimerTask;

        public class Main {   public static void main(String[] argv) throws
         Exception {

             Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);


              Timer timer = new Timer();

                  timer.schedule(new TimerTask() {
                    public void run() {
                      System.out.println("doing");
                   }
                  }, timeToRun);   } }