-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateParser.java
32 lines (28 loc) · 1.01 KB
/
DateParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DateParser
{
public static void main(String[] args)
{
printDateParts("1397/12/11");
Scanner scanner = new Scanner(System.in);
String inputLine;
while (scanner.hasNextLine() &&
!(inputLine = scanner.nextLine()).equalsIgnoreCase("end")) //Getting input till 'end' is not input
printDateParts(inputLine);
}
private static Pattern datePattern = Pattern.compile("^(\\d{2}|\\d{4})/(\\d{1,2})/(\\d{1,2})$");
private static void printDateParts(String strDate)
{
Matcher matcher = datePattern.matcher(strDate);
if(!matcher.find())
{
System.out.println("Invalid date format");
return;
}
System.out.println("year = " + matcher.group(1));
System.out.println("month = " + matcher.group(2));
System.out.println("day = " + matcher.group(3));
}
}