Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- System.out.println(
- String.format("Enter a string in the format %s. ex. Now: %s",
- dateFormat.toPattern(), dateFormat.format(new Date())));
- Date userTime = null;
- while (userTime == null) {
- try {
- userTime = dateFormat.parse(scanner.nextLine());
- } catch (ParseException e) {
- System.out.println(String.format("Invalid date-time. Enter a date-time of the format %s", dateFormat.toPattern()));
- }
- }
- dateFormat = new SimpleDateFormat("EEEEE, MMMMM d");
- Calendar cal = Calendar.getInstance();
- cal.setTime(userTime);
- System.out.println(cal.get(Calendar.DAY_OF_YEAR));
- int quarter = cal.get(Calendar.MONTH) / 3;
- SimpleDateFormat monthFormatter = new SimpleDateFormat("MMMMM");
- for (int i = 0; i < 3; i++) {
- cal.set(Calendar.DAY_OF_MONTH, 1);
- cal.set(Calendar.MONTH, quarter * 3 + i);
- int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
- int payDay1 = 15; int payDay2 = lastDay;
- cal.set(Calendar.DAY_OF_MONTH, payDay1);
- if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) payDay1 -= 2;
- if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) payDay1 -= 1;
- cal.set(Calendar.DAY_OF_MONTH, payDay2);
- if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) payDay2 -= 2;
- if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) payDay2 -= 1;
- for (int day = 1; day <= lastDay; day++){
- cal.set(Calendar.DAY_OF_MONTH, day);
- if (day == payDay1) System.out.println(dateFormat.format(cal.getTime()) + " Payday");
- else if (day == payDay2) System.out.println(dateFormat.format(cal.getTime()) + " Payday");
- else if (day == 1) System.out.println(dateFormat.format(cal.getTime()));
- else if (day == lastDay) System.out.println(dateFormat.format(cal.getTime()));
- else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
- if (day <= 7) System.out.println(dateFormat.format(cal.getTime()));
- else if (lastDay - day <= 7) System.out.println(dateFormat.format(cal.getTime()));
- }
- else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) System.out.println(dateFormat.format(cal.getTime()));
- }
- }
- System.out.println("Enter a starting week number");
- int week1 = 0;
- while (week1 == 0) {
- try {
- week1 = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException e) {
- System.out.println(String.format("Invalid number"));
- }
- if (!(week1 >=1 && week1 <= 52)) {
- week1 = 0;
- System.out.println("Invalid number");
- }
- }
- System.out.println("Enter a final week number");
- int week2 = 0;
- while (week2 == 0) {
- try {
- week2 = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException e) {
- week2 = 0;
- System.out.println(String.format("Invalid number"));
- }
- if (!(week2 >= week1 && week2 <= 52)) {
- week2 = 0;
- System.out.println("Invalid number");
- }
- }
- System.out.println("Enter a province/territory code");
- System.out.println("Options are " + Arrays.toString(provinces.toArray()));
- String province = "";
- do {
- System.out.print("Province: ");
- province = scanner.nextLine().toUpperCase();
- }
- while (!provinces.contains(province));
- printSpecialDays(week1, week2, province);
- }
- private static void printSpecialDays(int week1, int week2, String locale) {
- SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd/yy");
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, 2019);
- System.out.println("Week #\tStart Date\tEnd Date\tHolidays");
- for (int w = week1; w <= week2; w++) {
- cal.set(Calendar.WEEK_OF_YEAR, w);
- System.out.print("\t" + w);
- cal.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(cal.DAY_OF_WEEK));
- System.out.print("\t" + dateFormat.format(cal.getTime()));
- cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(cal.DAY_OF_WEEK));
- System.out.print("\t" + dateFormat.format(cal.getTime()));
- System.out.println("\t" + getHoliday(cal, locale));
- }
- }
- 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"}));
- // https://www.canada.ca/en/revenue-agency/services/tax/public-holidays.html
- // https://www.epochconverter.com/days/2019
- private static Map<Integer, String> holidays2019 = new HashMap<>();
- static {
- holidays2019.put(1, "New Year");
- holidays2019.put(16, "Good Friday");
- holidays2019.put(21, "Victoria Day");
- holidays2019.put(27, "Canada Day");
- holidays2019.put(32, "Civic Holiday?-QC,YT");
- holidays2019.put(36, "Labour Day");
- holidays2019.put(42, "Thanksgiving Day");
- holidays2019.put(46, "Remembrance Day");
- holidays2019.put(52, "Christmas Day;Boxing Day?+ON");
- }
- private static String getHoliday(Calendar cal, String locale) {
- int day = cal.get(Calendar.WEEK_OF_YEAR);
- if (holidays2019.containsKey(day)){
- String days[] = holidays2019.get(day).split(";");
- dayLoop:
- for (int d = 0; d < days.length; d++) {
- String[] pack = days[d].split("\\?");
- if (pack.length > 1){
- switch (pack[1].substring(0,1)){
- case "+":
- if (!pack[1].contains(locale)) {
- days[d] = "";
- continue dayLoop;
- }
- break;
- case "-":
- if (pack[1].contains(locale)) {
- days[d] = "";
- continue dayLoop;
- }
- break;
- }
- days[d] = pack[0];
- }
- }
- String out = "";
- for (int d = 0; d < days.length; d++){
- if(days[d] != ""){
- if (out != "")
- out += ", ";
- out += days[d];
- }
- }
- return out;
- } else
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement