Skip to content

Instantly share code, notes, and snippets.

@anirudhjoshi
Created October 31, 2012 09:21
Show Gist options
  • Save anirudhjoshi/3986050 to your computer and use it in GitHub Desktop.
Save anirudhjoshi/3986050 to your computer and use it in GitHub Desktop.
DateParser
import java.lang.Exception;
import java.util.Scanner;
import java.util.StringTokenizer;
public class DateParser {
public static void main(String[] args) throws Exception {
while (true) {
try {
String s;
System.out.println("Enter m/d/y format: ");
Scanner k = new Scanner(System.in);
s = k.next();
StringTokenizer tk = new StringTokenizer(s, "/");
if (tk.countTokens() < 3) {
throw new EntriesException("Not enough entries.");
}
String month = tk.nextToken();
String day = tk.nextToken();
String year = tk.nextToken();
s = formatDate(month, day, year);
System.out.println(s);
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
private enum Months {
January, February, March, April, May, June, July, August, September, October, November, December
};
public static String formatDate(String month, String day, String year)
throws Exception {
int m, d, y;
try {
m = Integer.parseInt(month);
if (m < 1 || m > 12) {
throw new MonthException(month);
}
month = Months.values()[m].toString();
} catch (Exception e) {
throw new MonthException(month);
}
try {
d = Integer.parseInt(day);
if (d < 1 || d > 31) {
throw new DayException(day);
}
} catch (Exception e) {
throw new DayException(day);
}
try {
y = Integer.parseInt(year);
if (y < 1000 || y > 3000)
throw new YearException(day);
} catch (Exception e) {
throw new YearException(day);
}
return String.format("%s %d, %d", month, d, y);
}
}
public class DayException extends Exception {
/**
*
*/
private static final long serialVersionUID = 3300289856005540808L;
private String s;
public DayException(String s) throws Exception {
super(s);
this.s = s;
}
@Override
public String getMessage() {
return "Please enter a day between 1 and 31";
}
public String getDay() {
return s;
}
}
public class EntriesException extends Exception {
/**
*
*/
private static final long serialVersionUID = 3300289856005540808L;
private String s;
public EntriesException(String s) throws Exception {
super(s);
this.s = s;
}
@Override
public String getMessage() {
return "Not enough entries.";
}
public String getNumber() {
return s;
}
}
package Date;
import java.util.Scanner;
import java.util.StringTokenizer;
public class DateParser {
public static void main(String[] args) throws DayException, MonthException, YearException{
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
Integer month,day,year;
Scanner input = new Scanner(System.in);
while(true){
try{
System.out.println("Please enter your date in the format month/day/year");
String date = input.nextLine();
StringTokenizer st = new StringTokenizer(date, "/");
month = Integer.parseInt(st.nextToken());
day = Integer.parseInt(st.nextToken());
year = Integer.parseInt(st.nextToken());
if (month < 1 || month > 12){
throw new MonthException();
}
if(day < 1 || day > 31){
throw new DayException();
}
if(year < 1000 || year > 3000){
throw new YearException();
}
System.out.println(months[month-1] + " " + day + ", " + year);
}
catch (MonthException e){
System.out.println(e.getMessage());
}
catch(DayException e){
System.out.println(e.getMessage());
}
catch(YearException e){
System.out.println(e.getMessage());
}
}
}
}
package Date;
public class DayException extends Exception{
public DayException(){
super();
}
@Override
public String getMessage(){
String s = "Please enter a day between 1 and 31";
return s;
}
}
package Date;
public class MonthException extends Exception{
public MonthException(){
super();
}
@Override
public String getMessage(){
String s = "Please enter a month between 1 and 12";
return s;
}
}
package Date;
public class YearException extends Exception{
public YearException(){
super();
}
@Override
public String getMessage(){
String s = "Please enter a year between 1000 and 3000";
return s;
}
}
public class MonthException extends Exception {
/**
*
*/
private static final long serialVersionUID = 3300289856005540808L;
private String s;
public MonthException(String s) throws Exception {
super(s);
this.s = s;
}
@Override
public String getMessage() {
return "Month must be between 1 and 12";
}
public String getMonth() {
return s;
}
}
public class YearException extends Exception {
/**
*
*/
private static final long serialVersionUID = 3300289856005540808L;
private String s;
public YearException(String s) throws Exception {
super(s);
this.s = s;
}
@Override
public String getMessage() {
return "Years must be between 1000 and 3000";
}
public String getYear() {
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment