如何动态更改数组中每个元素的日期

时间:2018-11-02 15:20:03

标签: java

嗨,我问了同样类型的问题,但在这里我想问更多信息,以避免混淆。

我可以看到问题出在哪里,DateOldStr变量在使用前已设置为空。但我想知道如何动态更改 DateOldStr在for循环内。

public class Testarray {
public static void main(String args[]) {

    String[] LoopArray = new String[3];


    SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
    String DateOldStr = "";
    String[] anArray = {
            "001,"+ DateOldStr +",F,162948.00,A,5153.68366,N,00026.29111,W,3.863,136.93,,1,0,0.00%,,;",
            "001,"+ DateOldStr +",,F,163018.00,A,5153.67529,N,00026.27327,W,8.855,121.16,,1,0,0.00%,,;",
            "001,"+ DateOldStr +",,F,163018.00,A,5153.67529,N,00026.27327,W,9.855,121.16,,2,0,0.00%,,;"
    };

    for (int i =0 ;i <anArray.length;i++) {

        String  DateToStr = format.format(new Date());

                System.out.println(anArray[i]); // Here I need to pass new Date  but not happening.
        try{
        //  shwo array element every 10sc so that it gets current date and time.
            Thread.sleep(10000);
        }catch(InterruptedException ex){

        }
    }
}

因此,当我执行此命令时,我将其放出,但正如您所看到的,由于我将它们设置为String DateOldStr = "";

,因此日期戳是空的
001,,F,162948.00,A,5153.68366,N,00026.29111,W,3.863,136.93,,1,0,0.00%,,;
001,,,F,163018.00,A,5153.67529,N,00026.27327,W,8.855,121.16,,1,0,0.00%,,;
001,,,F,163018.00,A,5153.67529,N,00026.27327,W,9.855,121.16,,2,0,0.00%,,;

3 个答案:

答案 0 :(得分:2)

您可以执行字符串replaceAll来交换日期。

public class Testarray {
public static void main(String args[]) {
String[] LoopArray = new String[3];


SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String DateOldStr = "";
String[] anArray = {
        "001,insertdateoldstr,F,162948.00,A,5153.68366,N,00026.29111,W,3.863,136.93,,1,0,0.00%,,;",
        "001,insertdateoldstr,,F,163018.00,A,5153.67529,N,00026.27327,W,8.855,121.16,,1,0,0.00%,,;",
        "001,insertdateoldstr,,F,163018.00,A,5153.67529,N,00026.27327,W,9.855,121.16,,2,0,0.00%,,;"
};

for (int i =0 ;i <anArray.length;i++) {

    String  DateToStr = format.format(new Date());
    anArray[i]=anArray[i].replaceAll("insertdateoldstr",DateToStr);   
    System.out.println(anArray[i]); // Here I need to pass new Date  but not happening.

    try{
    //  shwo array element every 10sc so that it gets current date and time.
        Thread.sleep(10000);
    }catch(InterruptedException ex){

    }
}
}

答案 1 :(得分:2)

您当然可以使用String.format。请注意,您必须在字符串末尾附近将百分号替换为“ %%”,因为百分号表示格式化程序参数的开头。

String[] arr = {
    "001,%s,F,162948.00,A,5153.68366,N,00026.29111,W,3.863,136.93,,1,0,0.00%%,,;",
    "001,%s,,F,163018.00,A,5153.67529,N,00026.27327,W,8.855,121.16,,1,0,0.00%%,,;",
    "001,%s,,F,163018.00,A,5153.67529,N,00026.27327,W,9.855,121.16,,2,0,0.00%%,,;"
};

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMddHHmmss");
Arrays.stream(anArray)
    .forEach(t -> {
        System.out.println(String.format(t, LocalDateTime.now().format(formatter)));
        try {
            Thread.sleep(2000);
        }
        catch (InterruptedException ex) {
            ...
        }
    });

答案 2 :(得分:1)

您可能会理解,每一行都接连执行,因此,如果您放置DateOldStr,则不能只更改它的值并等待数组中的更改,就没有绑定

您可以做的是使用now's date在循环中插入StringBuilder,该索引允许您在索引4处insertString,也可以使用{{1} },因为它是最新的LocalDateTime,而不是过时的time api

date

  • 遵循Java命名约定,对变量,参数等使用DateTimeFormatter format = DateTimeFormatter.ofPattern("yyMMddHHmmss"); String[] anArray = { "001,,F,162948.00,A,5153.68366,N,00026.29111,W,3.863,136.93,,1,0,0.00%,,;", "001,,,F,163018.00,A,5153.67529,N,00026.27327,W,8.855,121.16,,1,0,0.00%,,;", "001,,,F,163018.00,A,5153.67529,N,00026.27327,W,9.855,121.16,,2,0,0.00%,,;" }; LocalDateTime dateNow; StringBuilder sb; for (int i = 0; i < anArray.length; i++) { dateNow = LocalDateTime.now(); sb = new StringBuilder(anArray[i]); sb.insert(4, dateNow.format(format)); anArray[i] = sb.toString(); System.out.println(anArray[i]); try { Thread.sleep(10000); } catch (InterruptedException ex) { ex.printStackTrace(); } } /* 001,181102113025,F,162948.00,A,5153.68366,N,00026.29111,W,3.863,136.93,,1,0,0.00%,,; 001,181102113035,,F,163018.00,A,5153.67529,N,00026.27327,W,8.855,121.16,,1,0,0.00%,,; 001,181102113045,,F,163018.00,A,5153.67529,N,00026.27327,W,9.855,121.16,,2,0,0.00%,,; ...

  • 切勿让lowerCamelCase块为空,至少使用catch