Skip to content

Instantly share code, notes, and snippets.

@cxubrix
Created August 29, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxubrix/b1a8aaaf76f369e1acf2eb3f9be498ef to your computer and use it in GitHub Desktop.
Save cxubrix/b1a8aaaf76f369e1acf2eb3f9be498ef to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
int year = 1099; // -9999.....9999
// range check
int date = 12;
String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int month = 12; // -100 - +100 --> valid 1..12
String monthStr = months[month - 1];
String dateStr = "31";
boolean validDate = true; // assume date is valid
if (date < 1) {
validDate = false;
}
// 31
// 30
// 28 || 29
switch (month) {
case 2:
if (date > 29) {
validDate = false;
}
break;
case 1:
case 3:
case 5:
case 7:
case 12:
if (date > 31) {
validDate = false;
}
break;
case 4:
case 6:
case 8:
case 9:
case 10:
case 11:
if (date > 30) {
validDate = false;
}
break;
default: {
System.out.println("ERROR in month");
System.exit(1);
break;
}
}
if (validDate) {
String yearStr = "" + year; // "-10"
if (year < 0) {
yearStr = "" + (-year) + " BC";
}
dateStr = "" + date;
String message = dateStr + ". " + monthStr + " " + yearStr;
System.out.println(message);
} else {
System.out.println("Error!!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment