需要帮助我的当前时间阵列

时间:2014-01-30 15:51:42

标签: java arrays datetime

import java.util.*;
import java.text.*;


public class dateTimeArr {
    static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    static String[] now=new String[10];
    static String[] later=new String[10];
    static int a,b;
    static String[] dateTimeArr = new String[20];
    static Date theDate = new Date();
    static String currentDateTime = (theDate.getDate() + "/"
            + (theDate.getMonth() + 1) + "/" + (theDate.getYear() + 1900) + " "
            + theDate.getHours() + ":" + theDate.getMinutes() + ":" + theDate
            .getSeconds());

    public static void main(String[] args) 
    {   
        dateTimeArr[a]=currentDateTime;
        System.out.println("What are you doing now?");
        now[a]=input.next();
        System.out.println("You are doing " +now[a]+ " at " + dateTimeArr[a]);
        a++;
            dateTimeArr[b]=currentDateTime;
        System.out.println("What are you doing later?");
        later[b]=input.next();
        System.out.println("You are doing " +now[b]+ " at " + dateTimeArr[b]);
        b++;

    }

}

帮助我。时间不想改变。提前致谢。需要帮助。虽然真的很有帮助。抱歉这个丑陋的帖子,因为我是新来的。 例如我的问题如下:

  

“我早上7:30:15醒来”

  

“我早上7:30:15从床上起床”

我希望它像:

  

“我早上7:30:15醒来”

  

“我早上7:31:35从床上起床”

1 个答案:

答案 0 :(得分:0)

您正在为dateTimeArr [a]和dateTimeArr [b]分配currentdatetime:

 >> dateTimeArr[a]=currentDateTime;
 System.out.println("What are you doing now?");
 now[a]=input.next();
 System.out.println("You are doing " +now[a]+ " at " + dateTimeArr[a]);
 a++;
 >> dateTimeArr[b]=currentDateTime;

由于currentDateTime仅在初始化时设置,因此在执行代码时不会发生变化。

更新 这可能是一种更好的方法:

import java.util.*;
import java.text.*;

public class dateTimeArr {
    static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    static String now=new String();
    static String later=new String();
    static String dateTimeArr = new String();

    public static void main(String[] args) 
    {   
        String dateTime=getCurrentDateTime();

        System.out.println("What are you doing now?");
        now=input.next();
        System.out.println("You are doing " +now+ " at " + dateTime);

        dateTime=getCurrentDateTime();
        System.out.println("What are you doing later?");
        later=input.next();
        System.out.println("You are doing " + later + " at " + dateTime);
    }

    public static String getCurrentDateTime(){
        Date theDate = new Date();
        String currentDateTime = (theDate.getDate() + "/"
            + (theDate.getMonth() + 1) + "/" + (theDate.getYear() + 1900) + " "
            + theDate.getHours() + ":" + theDate.getMinutes() + ":" + theDate
            .getSeconds());

        return currentDateTime;
    }
}
相关问题