forked from Shreerang4/learning-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDMMEH.java
More file actions
72 lines (61 loc) · 2.24 KB
/
DDMMEH.java
File metadata and controls
72 lines (61 loc) · 2.24 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.Scanner;
class MonthError extends Exception{
public MonthError(String msg){
super(msg);
}
}
class DayError extends Exception{
public DayError(String msg){
super(msg);
}
}
public class DDMMEH {
public static String changeformat(String numericDate) throws MonthError, DayError{
String[] months = {
"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
try{
String[] parts = numericDate.split("/");
int month = Integer.parseInt(parts[0]);
int day = Integer.parseInt(parts[1]);
if(month < 1 || month > 12){
throw new MonthError("Invalid Month: " + month);
}
if(day < 1 || day > 31){
throw new DayError("Invalid Day: " + day);
}
if(month == 2 && day > 29){
throw new DayError("Invalid Day: " + day);
}
if((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){
throw new DayError("Invalid Day: " + day);
}
return months[month] + " " + day;
}
catch (NumberFormatException e){
throw new NumberFormatException("Invalid Date Format: " + numericDate);
}
}
public static void main(String[] args){
String date;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of dates you want to enter: ");
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i<n; i++){
date = sc.nextLine();
try{
String orderedDate = changeformat(date);
System.out.println(date + " corresponds to " + orderedDate);
}
catch (MonthError e) {
System.out.println("Error!!! --->" + date + ": " + e.getMessage());
}catch (DayError e) {
System.out.println("Error!!! --->" + date + ": " + e.getMessage());
}catch (NumberFormatException e) {
System.out.println("Error!!! --->" + date + ": " + e.getMessage());
}
}
}
}