1

I have some data's like 2014-06-11T22:22:17

which i am matching using regex ([\d-])+T([\d:])+ in two different part by separating date and time like

2014-06-11 and 22:22:17 so i have created two groups

but when i am extracting the data like as below

Date =m.group(1); 

it extract only digit "1" in Date String, why is it not extracting the entire group which is 2014-06-11?

2
  • 1
    You have to repeat the character class instead of the capturing group. [\d-]+ Repeating the group will give you the value of the last iteration, and findall will return the group which will then contain a single value. Commented Nov 6, 2019 at 13:30
  • 1
    Why not write the whole pattern? (\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}) Commented Nov 6, 2019 at 13:32

1 Answer 1

3

I would like to use LocalDateTime in your case to get the date and time separately like this :

LocalDateTime dateTime = LocalDateTime.parse("2014-06-11T22:22:17"); LocalDate date = dateTime.toLocalDate(); // 2014-06-11 LocalTime time = dateTime.toLocalTime(); // 22:22:17 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.