You may need to extract or filter the date or time from a String or text. You can use the following program:
The code is as follows: |
Copy code |
Public String run (String text ){ String dateStr = text. replaceAll ("r? N ",""); DateStr = dateStr. replaceAll ("\ s + ",""); Try { List matches = null; Pattern p = Pattern. compile ("(\ d {} [-| \/] \ d {} [-| \/] \ d }: \ d {1, 2 }:\ d {1, 2}) ", Pattern. CASE_INSENSITIVE | Pattern. MULTILINE ); Matcher = p. matcher (dateStr ); If (matcher. find () & matcher. groupCount ()> = 1 ){ Matches = new ArrayList (); For (int I = 1; I <= matcher. groupCount (); I ++ ){ String temp = matcher. group (I ); Matches. add (temp ); } } Else { Matches = Collections. EMPTY_LIST; } If (matches. size ()> 0 ){ Return (String) matches. get (0). trim (); } Else { Return ""; } } Catch (Exception e ){ Return ""; } } |
The current function of this program is to extract from the string in the form of "yyyy-MM-dd HH: mm: ss" or "yyyy/MM/dd HH: mm: ss. If the time format is short, it cannot be processed, but it can be easily expanded.