简体   繁体   中英

String Joiner help needed in Java

I need a help in String Joiners and i want to append To_date to before string and the format after string..

public static StringJoiner inputDatesToQuery(
        List<FileSubmtRetrlStatus> fileSubmtRetrlStatus) {
    StringJoiner joinNames = new StringJoiner(",", "TO_DATE('", "','YYYY-MM-DD HH24:MI:SS')");   // passing comma(,) and square-brackets as delimiter   
    for (FileSubmtRetrlStatus status : fileSubmtRetrlStatus) {
        joinNames.add(status.getFileSubRetDateTime());
        }
    return joinNames;

But the out put is coming like as below..

TO_DATE('2017-04-13 11:18:16,2017-04-13 11:17:44,2017-04-13 10:16:07',
'YYYY-MM-DD HH24:MI:SS')

But it should be like below...

TO_DATE('2017-04-13 11:18:16,'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2017-04-13 11:17:44,'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2017-04-13 10:16:07,'YYYY-MM-DD HH24:MI:SS')

StringJoiner only produces one output string. The example code in its documentation is:

The String "[George:Sally:Fred]" may be constructed as follows:

StringJoiner sj = new StringJoiner(":", "[", "]");
sj.add("George").add("Sally").add("Fred");
String desiredString = sj.toString();

Ie it takes all the items you add() , separates them by the delimiter (in your case, a comma), and when you call toString() puts the prefix at the beginning of the full string, and the suffix at the end.

If you want to use StringJoiner, you need a separate one for each entry you want to output. You might want to look at Formatter or StringBuilder instead.

If you want that output, you just need to setup the StringJoiner correctly:

String[] input = { "2017-04-13 11:18:16", "2017-04-13 11:17:44", "2017-04-13 10:16:07" };

StringJoiner joiner = new StringJoiner(",'YYYY-MM-DD HH24:MI:SS'),\nTO_DATE('",
                                       "TO_DATE('",
                                       ",'YYYY-MM-DD HH24:MI:SS')");
for (String s : input)
    joiner.add(s);
System.out.println(joiner.toString());

Output

TO_DATE('2017-04-13 11:18:16,'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2017-04-13 11:17:44,'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2017-04-13 10:16:07,'YYYY-MM-DD HH24:MI:SS')

Or to use Streams, like @LouisWasserman thinks you should :

String[] input = { "2017-04-13 11:18:16", "2017-04-13 11:17:44", "2017-04-13 10:16:07" };

String joined = Arrays.stream(input)
                      .collect(Collectors.joining(",'YYYY-MM-DD HH24:MI:SS'),\nTO_DATE('",
                                                  "TO_DATE('",
                                                  ",'YYYY-MM-DD HH24:MI:SS')"));

System.out.println(joined);

Remember, the prefix only goes before the first item , the suffix only goes after the last item , and the delimiter should be the entire text you want between items .

prefix
TO_DATE(' ​2017-04-13 11:18:16​ ,'YYYY-MM-DD HH24:MI:SS'),delimiter
TO_DATE(' ​2017-04-13 11:17:44,'YYYY-MM-DD HH24:MI:SS'),
TO_DATE('2017-04-13 10:16:07​ ,'YYYY-MM-DD HH24:MI:SS')suffix


UPDATE

For the people who don't like repetition or string with newlines, do this:

String prefix = "TO_DATE('";
String suffix = ",'YYYY-MM-DD HH24:MI:SS')";
StringJoiner joiner = new StringJoiner(suffix + ", " + prefix, prefix, suffix);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM