regex - How to extract a date from a string and put it into a date variable in Java -
i have been looking around stack-overflow , various other websites solution problem haven't found suitable specific purposes , have been unable change these solutions suit code. these include regex codes not understand or know how manipulate.
so here question, have string has structure:
"name+" at:"+date+" notes:"+meetingnotes"
(name, date , meetingnotes being variables). want extract date part of string , stick in date variable. basic dateformat dates "yyyy-mm-dd". how do this?
for this, regular expression friend:
string input = "john doe at:2016-01-16 notes:this test"; string regex = " at:(\\d{4}-\\d{2}-\\d{2}) notes:"; matcher m = pattern.compile(regex).matcher(input); if (m.find()) { date date = new simpledateformat("yyyy-mm-dd").parse(m.group(1)); // use date here } else { // bad input }
or in java 8+:
localdate date = localdate.parse(m.group(1));
Comments
Post a Comment