Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- int lastday=1;
- int lastmonth=1;
- int lastyear=1800;
- int day;
- int month;
- int year;
- boolean c1;
- boolean c2;
- boolean c3;
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter today's date in form MM DD YYYY");
- month = sc.nextInt();
- day = sc.nextInt();
- year = sc.nextInt();
- c1 = (day >= 1) && (day <= 31);
- c2 = (month >= 1) && (month <= 12);
- c3 = (year >= 1800) && (year <= 2050);
- if (!c1)
- System.out.println("Value of day not in the range 1...31");
- if (!c2)
- System.out.println("Value of month not in the range 1...12");
- if (!c3)
- System.out.println("Value of year not in the range 1800...2050");
- do {
- switch (month) {
- case 5:
- case 7:
- case 10:
- case 12:
- if (day > 1) {
- lastday = day - 1;
- lastmonth = month;
- lastyear = year;
- } else {
- lastday = 30;
- lastmonth = month - 1;
- lastyear = year;
- }
- break;
- case 4:
- case 6:
- case 8:
- case 9:
- case 11:
- if (day > 1) {
- lastday = day - 1;
- lastmonth = month;
- lastyear = year;
- } else {
- lastday = 31;
- lastmonth = month - 1;
- lastyear = year;
- }
- break;
- case 2:
- if (day > 1) {
- if((year % 4 != 0 &&day==29)||day>29){
- System.out.println("Wrong day");
- }else{
- lastday = day - 1;
- lastmonth = month;
- lastyear = year;
- }
- } else {
- lastday = 31;
- lastmonth = month - 1;
- lastyear = year;
- }
- break;
- case 3:
- if (day > 1) {
- lastday = day - 1;
- lastmonth = month;
- lastyear = year;
- } else {
- if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
- lastday = 29;
- lastmonth = 2;
- lastyear = year;
- } else {
- lastday = 28;
- lastmonth = 2;
- lastyear = year;
- }
- }
- break;
- case 1:
- if (day > 1) {
- lastday = day - 1;
- lastmonth = month;
- lastyear = year;
- } else {
- lastday = 31;
- lastmonth = 12;
- if (year > 1800)
- lastyear = year - 1;
- else {
- System.out.println("lastyear is not in range");
- }
- }
- break;
- default:
- System.out.println("error!");
- }
- if (c1 && c2 && c3) {
- System.out.println();
- System.out.println("the lastdate is " + lastmonth + " "
- + lastday + " " + lastyear);
- }
- return;
- } while (c1 && c2 && c3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement