How to split date and time from a datetime string?

后端 未结 8 1680
面向向阳花
面向向阳花 2020-12-11 15:53

I have a string like 8/29/2011 11:16:12 AM. I want to split that string into separate variables like

date = 8/29/2011
time = 11:16:12 AM
         


        
8条回答
  •  感情败类
    2020-12-11 16:47

    You can do it by splitting your string into two substrings, as follows

    String main = "8/29/2011 11:16:12 AM";
    String s[] = main.split(" ",2);
    String date = s[0];
    String time = s[1];
    

    NOTE: The split method will split the string into two parts as mentioned in the second argument.

提交回复
热议问题