简体   繁体   中英

How to convert yyyy-mm-dd hh:mm:ss.ms to mm/dd/yyyy hh24:mi using Java

I have a SQL query which returns one column value-2020-07-01 03:43:30.0(yyyy-mm-dd hh:mm:ss.ms).I want to convert this into 'mm/dd/yyyy hh24:mi' format using Java code.How to do this with Java.

You can use two different DateTimeFormatter s:

DateTimeFormatter parseFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
DateTimeFormatter resultFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm");

Where parseFormat is used for parsing and resultFormat is used to produce the String from the parsed date:

LocalDateTime ldt = LocalDateTime.parse("2020-07-01 03:43:30.0", parseFormat);
String result = ldt.format(resultFormat);
System.out.println(result);

Prints:

07/01/2020 03:43

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