Exception:
PARSE EXCEPTION
Sample Input and Output 1:
Enter the stage event start date and end date
27-01-2017-12
Input dates should be in the format 'dd-MM-yyyy-HH:mm:ss'
Sample Input and Output 2:
Enter the stage event start date and end date
27-01-2017-12:0:0
28-01-2017-12:0:0
Start date:27-01-2017-12:00:00
End date:28-01-2017-12:00:00
Enter the stage event start date and end date
27-01-2017-12
Input dates should be in the format 'dd-MM-yyyy-HH:mm:ss'
Sample Input and Output 2:
Enter the stage event start date and end date
27-01-2017-12:0:0
28-01-2017-12:0:0
Start date:27-01-2017-12:00:00
End date:28-01-2017-12:00:00
CODE:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String startDate, endDate;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the stage event start date and end date");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH:mm:ss");
try {
startDate=scanner.nextLine();
Date date1 = (Date) sdf.parse(startDate);
endDate=scanner.nextLine();
Date date2 = (Date) sdf.parse(endDate);
System.out.println("Start date:"+sdf.format(date1));
System.out.println("End date:"+sdf.format(date2));
}catch (ParseException prsExp) {
System.out.println("Input dates should be in the format 'dd-MM-yyyy-HH:mm:ss'");
}
}
}
Comments
Post a Comment