嗨,我问了同样类型的问题,但在这里我想问更多信息,以避免混淆。
我可以看到问题出在哪里,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%,,;
答案 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处insert
和String
,也可以使用{{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