-1

this is my string val fulldate = "10/30/2019 6:10:52 AM" how I can I get separate this string in to date and time. val date = "10/30/2019" val time = "6:10:52 AM"

BacII
  • 39
  • 1
  • 6
  • can you try to work with string manipulation and/or regex for this? If the fulldate is in a constant format, you should be able to do it without much hassle, just try to work with edge cases, I guess. Try do some code by yourself! And add the code here if you get stuck. – mfakhrusy Nov 01 '19 at 04:12
  • a bit hint: first split the string by the spaces, then you got 3 variable, then combine the last two as one variable, you'll get there, or you can just split it by the first space only. – mfakhrusy Nov 01 '19 at 04:17
  • I took this occasion for writing [a new answer to one of the original questions here](https://stackoverflow.com/a/58654203/5772882). I think it matches very well with yur requirements. – Ole V.V. Nov 01 '19 at 04:46

1 Answers1

0

Try using String#split, with the second limit parameter to split only on the first space separating the date and time components:

String fulldate = "10/30/2019 6:10:52 AM";
System.out.println("date: " + fulldate.split(" ", 2)[0]);
System.out.println("time: " + fulldate.split(" ", 2)[1]);

This prints:

date: 10/30/2019
time: 6:10:52 AM
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360