0

I am trying to see how I can change the DateToStr (Unix time) when an Array item is showing. Currently it is always showing the same time.

public class Testarray {
    public static void main(String args[]) {
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        String  DateToStr = format.format(new Date());

        String[] anArray = {
            "001,"+ DateToStr +",,F,", "001,"+ DateToStr +",,F,", "001,"+ DateToStr +",,F,"
        };

        for (int i =0 ;i <anArray.length;i++) {
             try{
                 //  show array element every 10sc with new date and time. 
                 Thread.sleep(10000);
             }catch(InterruptedException ex){

             }
             System.out.println(anArray[i]);
        }
    }

Current output:

001,181102074606,,F,
001,181102074606,,F,
001,181102074606,,F,

Expected output: each output should have new date as it is 10 seconds delayed.

2
  • 3
    You are never changing the value of the date. You are only setting it once. Commented Nov 2, 2018 at 8:26
  • 1
    You transform the current date to a string once and only once, before the loop. Then you store that uniqe string value three times in an array. For the output to vary, you would need to get the current date and transform it to a string inside the loop. Commented Nov 2, 2018 at 8:27

2 Answers 2

3

DateToStr variable is common for all the three entries in the array and that is why it is giving you same date every time. If you want your date to be new every time then you will have to get that value in the loop before sleep method.

Try this,

  SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");

        String[] anArray = new String[3];

        char c ='E';
        for (int i =0 ;i <anArray.length;i++) {

            try{
                anArray[i]= "001,"+ format.format(new Date()) +",,"+c+",";
                c++;
            //  show array element every 10sc with new date and time. 
                Thread.sleep(10000);
            }catch(InterruptedException ex){

            }

            System.out.println(anArray[i]);

        }
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, but there is a one problem, inside for loop, how will pass three 3 iteams ? because you doing bellow, so how will pass sthe 2nd and 3rd iteam ? anArray[i]= "001,"+ format.format(new Date()) +",,F,";
Since you are using it inside the loop, it will automatically run 3 times and assign value to your 2nd and third element of array.
Thanks, but from where its getting the values of 2nd and 3rd iteam ? because we are hand coded the first iteam anArray[i]= "001,"+ format.format(new Date()) +",,F,"; from where its getting other 2 iteam which is ** "001,"+ DateToStr +",,F," "001,"+ DateToStr +",,F," **
So when you dry run the code, it will be as follows, i=0, anArray[i]="001,"+the formated new date +",,F,". Now for second iteration, i=1, anArray[i]= "001,"+the formated new date +",,F," and here new date will be 10 seconds ahead as we are making our thread to sleep for 10 seconds. and for last, i=2, anArray[i]="001,"+the formated new date +",,F,".
Ahh I see where is the confusion, those 3 elements are different alement, let me change the code in main section to make it clear that those 3 elements are different it will be like this just to provide example, i changed the character E, F , G to show its different element , not same one String[] anArray = { "001,"+ DateToStr +",,E,", "001,"+ DateToStr +",,F,", "001,"+ DateToStr +",,G," };
|
0

You need to update your DateToStr value every time you insert. And you insert everything at once. You only read with 10s delay.

Correct is to wait 10s between inserts, and read date each time at insert time.

  SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");

    String[] anArray = new String[3];
    for(int i = 0; i < anArray.length; ++i) {
        try {
            // Sleep before inserting
            Thread.sleep(1000);
            // Insert with new value every time
            anArray[i] = "001,"+ format.format(new Date()) + ",,F,";
            System.out.println(anArray[i]);
        } catch (InterruptedException ex) {
        }
    }

1 Comment

Thanks, but there is a one problem, inside for loop, how will pass three 3 iteams ? because you doing bellow, so how will pass sthe 2nd and 3rd item ? anArray[i]= "001,"+ format.format(new Date()) +",,F,";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.