Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  14.  
  15.         System.out.println(
  16.                 String.format("Enter a string in the format %s. ex. Now: %s",
  17.                         dateFormat.toPattern(), dateFormat.format(new Date())));
  18.  
  19.         Date userTime = null;
  20.         while (userTime == null) {
  21.             try {
  22.                 userTime = dateFormat.parse(scanner.nextLine());
  23.             } catch (ParseException e) {
  24.                 System.out.println(String.format("Invalid date-time. Enter a date-time of the format %s", dateFormat.toPattern()));
  25.             }
  26.         }
  27.  
  28.         dateFormat = new SimpleDateFormat("EEEEE, MMMMM d");
  29.  
  30.         Calendar cal = Calendar.getInstance();
  31.         cal.setTime(userTime);
  32.  
  33.         System.out.println(cal.get(Calendar.DAY_OF_YEAR));
  34.  
  35.         int quarter = cal.get(Calendar.MONTH) / 3;
  36.  
  37.         SimpleDateFormat monthFormatter = new SimpleDateFormat("MMMMM");
  38.  
  39.         for (int i = 0; i < 3; i++) {
  40.             cal.set(Calendar.DAY_OF_MONTH, 1);
  41.             cal.set(Calendar.MONTH, quarter * 3 + i);
  42.             int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  43.  
  44.             int payDay1 = 15; int payDay2 = lastDay;
  45.             cal.set(Calendar.DAY_OF_MONTH, payDay1);
  46.             if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) payDay1 -= 2;
  47.             if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) payDay1 -= 1;
  48.             cal.set(Calendar.DAY_OF_MONTH, payDay2);
  49.             if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) payDay2 -= 2;
  50.             if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) payDay2 -= 1;
  51.  
  52.  
  53.  
  54.             for (int day = 1; day <= lastDay; day++){
  55.                 cal.set(Calendar.DAY_OF_MONTH, day);
  56.                 if      (day == payDay1) System.out.println(dateFormat.format(cal.getTime()) + " Payday");
  57.                 else if (day == payDay2) System.out.println(dateFormat.format(cal.getTime()) + " Payday");
  58.                 else if (day == 1) System.out.println(dateFormat.format(cal.getTime()));
  59.                 else if (day == lastDay) System.out.println(dateFormat.format(cal.getTime()));
  60.                 else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
  61.                     if (day <= 7) System.out.println(dateFormat.format(cal.getTime()));
  62.                     else if (lastDay - day <= 7) System.out.println(dateFormat.format(cal.getTime()));
  63.                 }
  64.                 else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) System.out.println(dateFormat.format(cal.getTime()));
  65.             }
  66.         }
  67.  
  68.         System.out.println("Enter a starting week number");
  69.  
  70.         int week1 = 0;
  71.         while (week1 == 0) {
  72.             try {
  73.                 week1 = Integer.parseInt(scanner.nextLine());
  74.             } catch (NumberFormatException e) {
  75.                 System.out.println(String.format("Invalid number"));
  76.             }
  77.             if (!(week1 >=1 && week1 <= 52)) {
  78.                 week1 = 0;
  79.                 System.out.println("Invalid number");
  80.             }
  81.         }
  82.  
  83.         System.out.println("Enter a final week number");
  84.  
  85.         int week2 = 0;
  86.         while (week2 == 0) {
  87.             try {
  88.                 week2 = Integer.parseInt(scanner.nextLine());
  89.             } catch (NumberFormatException e) {
  90.                 week2 = 0;
  91.                 System.out.println(String.format("Invalid number"));
  92.             }
  93.             if (!(week2 >= week1 && week2 <= 52)) {
  94.                 week2 = 0;
  95.                 System.out.println("Invalid number");
  96.             }
  97.         }
  98.  
  99.         System.out.println("Enter a province/territory code");
  100.         System.out.println("Options are " + Arrays.toString(provinces.toArray()));
  101.  
  102.         String province = "";
  103.  
  104.         do {
  105.             System.out.print("Province: ");
  106.             province = scanner.nextLine().toUpperCase();
  107.         }
  108.         while (!provinces.contains(province));
  109.  
  110.         printSpecialDays(week1, week2, province);
  111.  
  112.  
  113.     }
  114.  
  115.     private static void printSpecialDays(int week1, int week2, String locale) {
  116.  
  117.         SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd/yy");
  118.  
  119.         Calendar cal = Calendar.getInstance();
  120.         cal.set(Calendar.YEAR, 2019);
  121.  
  122.         System.out.println("Week #\tStart Date\tEnd Date\tHolidays");
  123.  
  124.         for (int w = week1; w <= week2; w++) {
  125.  
  126.             cal.set(Calendar.WEEK_OF_YEAR, w);
  127.             System.out.print("\t" + w);
  128.             cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(cal.DAY_OF_WEEK));
  129.             System.out.print("\t" + dateFormat.format(cal.getTime()));
  130.             cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(cal.DAY_OF_WEEK));
  131.             System.out.print("\t" + dateFormat.format(cal.getTime()));
  132.             System.out.println("\t" + getHoliday(cal, locale));
  133.  
  134.  
  135.         }
  136.     }
  137.  
  138.     private static List<String> provinces = new ArrayList<String>(Arrays.asList(new String[] {"NL", "PE", "NS", "NB", "QC", "ON", "MB", "SK", "AB", "BC", "YT", "NT", "NU"}));
  139.  
  140.     // https://www.canada.ca/en/revenue-agency/services/tax/public-holidays.html
  141.     // https://www.epochconverter.com/days/2019
  142.  
  143.     private static Map<Integer, String> holidays2019 = new HashMap<>();
  144.     static {
  145.         holidays2019.put(1, "New Year");
  146.         holidays2019.put(16, "Good Friday");
  147.         holidays2019.put(21, "Victoria Day");
  148.         holidays2019.put(27, "Canada Day");
  149.         holidays2019.put(32, "Civic Holiday?-QC,YT");
  150.         holidays2019.put(36, "Labour Day");
  151.         holidays2019.put(42, "Thanksgiving Day");
  152.         holidays2019.put(46, "Remembrance Day");
  153.         holidays2019.put(52, "Christmas Day;Boxing Day?+ON");
  154.     }
  155.  
  156.     private static String getHoliday(Calendar cal, String locale) {
  157.         int day = cal.get(Calendar.WEEK_OF_YEAR);
  158.         if (holidays2019.containsKey(day)){
  159.             String days[] = holidays2019.get(day).split(";");
  160.  
  161.             dayLoop:
  162.             for (int d = 0; d < days.length; d++) {
  163.                 String[] pack = days[d].split("\\?");
  164.                 if (pack.length > 1){
  165.  
  166.                     switch (pack[1].substring(0,1)){
  167.  
  168.                         case "+":
  169.                             if (!pack[1].contains(locale)) {
  170.                                 days[d] = "";
  171.                                 continue dayLoop;
  172.                             }
  173.                             break;
  174.  
  175.                         case "-":
  176.                             if (pack[1].contains(locale)) {
  177.                                 days[d] = "";
  178.                                 continue dayLoop;
  179.                             }
  180.                             break;
  181.  
  182.  
  183.                     }
  184.                     days[d] = pack[0];
  185.                 }
  186.             }
  187.  
  188.             String out = "";
  189.             for (int d = 0; d < days.length; d++){
  190.                 if(days[d] != ""){
  191.                     if (out != "")
  192.                         out += ", ";
  193.                     out += days[d];
  194.                 }
  195.             }
  196.             return out;
  197.  
  198.         } else
  199.             return "";
  200.     }
  201.  
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement