string="2022-05-17T18:14:41.000 00:00"
i want to split the string in a way where I can get
s1="2022-05-17"
s2="18:14"
how can I do this in java?
CodePudding user response:
For this case, You can implement something like this:
s1 = string.substring(0, 10); s2 = string.substring(11, 16);
CodePudding user response:
This sounds like an X/Y problem: You have defined a clear need (I have this string; I wish to obtain these 2 strings from it), but then also decided on how this should be done (regexes).
Using regular expressions to do this task is not a good idea.
What you have is a timestamp. What you wish to obtain is the localdate component of it, and a local hours minutes component of it.
At least, that's what it sounds like.
Assuming you want that, here's the general plan:
Define a
DateTimeFormatter
object that matches the format of the input you have. Should be easy - that's simply DateTimeFormatter.ISO_DATE_TIME.Use that to turn your input string into an object that actually represents that data:
OffsetDateTime.parse(string, thatFormatter)
. You now have a OffsetDateTime.Do stuff to it if needed. Does the offset need to be adjusted? What if the input is
2022-05-17T17:14:41.000 01:00
- do you want the output to be 17:14, or 16:14? This is the phase where you 'fix' such things, and instead of string manipulation which is error prone, you now manipulate an OffsetDateTime object which has methods for all this that are much easier to use.Extract what you need. You can either put it together with e.g.
offsetDateTimeObj.getYear()
, or use formatters again: Make a formatter that formats stuff into2022-05-17
and runoffsetDateTimeObj.format(yourFormatObj)
.DateTimeFormatter.ISO_LOCAL_DATE
already does2022-05-17
for you.DateTimeFormatter.ofPattern("HH:mm")
should take care of the 18:14 part.Regular expressions shouldn't be involved in the answer at all. Too low-level a tool for such a task. You're just waiting to get messed up due to exotic zones and weird situations (such as daylight savings or whatnot).
NB: This answer was based on ZonedDateTime
, but as commenter @deHaar pointed out, the input is an offset date time, not a zoned date time. OffsetDateTime is the appropriate data type here.
CodePudding user response:
most elegant solution should be to let a library do that for you
val parsedDateTime = OffsetDateTime.parse("2022-05-17T18:14:41.000 00:00", DateTimeFormatter.ISO_DATE_TIME);
val date = parsedDateTime.toLocalDate();
val time = parsedDateTime.toLocalTime();
System.out.println(date);
System.out.println(time);
please consider the timezone
CodePudding user response:
If you want to have objects you can perform operations with date and time, you should use a suitable library in order to parse the source and properly retrieve the values you desire.
I would use a java.time.OffsetDateTime
to parse the String
, then extract the date part and the time part separately and format them as required:
public static void main(String[] args) {
// source String containing date, time and offset
String datetime = "2022-05-17T18:14:41.000 00:00";
// directly parse it to an OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(datetime);
// prepare the date String (use a prebuilt format)
String s1 = odt.toLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
// prepare the time-of-day String (define a format that outputs hour and minute only)
String s2 = odt.toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm"));
// print both
System.out.println(s1);
System.out.println(s2);
}
This code produces the following output:
2022-05-17
18:14
CodePudding user response:
But of course, you can also use a regexp:
private static final Pattern RE
= Pattern.compile("(\\d{4}-\\d{2}-\\d{2})T(\\d{2}:\\d{2}):.*");
public static void main(String[] args) {
String string="2022-05-17T18:14:41.000 00:00";
Matcher matcher = RE.matcher(string);
if (!matcher.matches()) {
throw new IllegalArgumentException("Syntax error in " string);
}
String s1 = matcher.group(1);
String s2 = matcher.group(2);
System.out.println(s1);
System.out.println(s2);
}