Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.Array;
- import java.util.*;
- import java.lang.String;
- public class Main {
- public static void main(String args[]) {
- Stack dates = getPalindromicDate(2021);
- System.out.println(dates);
- }
- public static Stack getPalindromicDate(int year){
- Stack<String> stack = new Stack<String>();
- int month = getMonth(year);
- int day = getDay(year);
- if (!checkDate(day, month)) return stack;
- String monthString = getMonthString(year);
- String dayString = getDayString(year);
- stack.push(monthString);
- stack.push(dayString);
- stack.push(Integer.toString(year));
- return stack;
- }
- public static int reverseDate ( int n){
- String number = Integer.toString(n);
- String output;
- output = "";
- for (int i = number.length() - 1; i >= 0; i--)
- output += number.charAt(i);
- return Integer.parseInt(output);
- }
- public static boolean checkDate ( int day, int month){
- int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- if (month > 12) return false;
- if (days[month - 1] < day) return false;
- return true;
- }
- public static int getMonth ( int reverseYear){
- String number = Integer.toString(reverseYear);
- String output = "";
- output += number.charAt(3);
- output += number.charAt(2);
- return Integer.parseInt(output);
- }
- public static String getMonthString ( int reverseYear){
- String number = Integer.toString(reverseYear);
- String output = "";
- output += number.charAt(3);
- output += number.charAt(2);
- return output;
- }
- public static int getDay ( int reverseYear){
- String number = Integer.toString(reverseYear);
- String output = "";
- output += number.charAt(1);
- output += number.charAt(0);
- return Integer.parseInt(output);
- }
- public static String getDayString ( int reverseYear){
- String number = Integer.toString(reverseYear);
- String output = "";
- output += number.charAt(1);
- output += number.charAt(0);
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement