Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
4.1 kB
0
Indexable
Never
package demo;

import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

public class main {
    public static void printCalender(String date) {
		String[] splitted = date.split("/");
		if(splitted.length != 3) {
			System.out.println("輸入格式錯誤!");
			return;
		}
		int year = Integer.valueOf(splitted[0]);
		int month = Integer.valueOf(splitted[1]);
		int day = Integer.valueOf(splitted[2]);
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		int startDay = calendar.get(Calendar.DAY_OF_WEEK); // 求本週第一天是星期幾
		int count = startDay - 1; // 第一週的初始計數
		int maxday = 31;
		if(month == 4 | month == 6 | month == 9 | month == 11)
			maxday = 30;
		else if (month == 2)
			maxday = 28;
		else if (month == 2 & (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
			maxday = 29;
		System.out.println("Sun Mon Tue Wed Thu Fri Sat");
		for (int i = 1; i < startDay; i++) 
			System.out.printf("%4s", " ");
		for (int i = 1; i <= maxday; i++) {
			System.out.printf("%3d%s", i, " "); // 使用printf函式進行格式對齊
			count++;
			if (count >= 7) { // 每輸出7天換一次行
				count = 0;
				System.out.println();
			}
		}
		System.out.println();
	}
	
	public static void printZodiac(int year) {	
		int heavenly_stems = (year - 3 % 60) % 10;
		int earthly_branches = (year - 3 % 60) % 12;
		char[] x = new char[] {' ','甲','乙','丙','丁','戊','己','庚','辛','壬','癸'};          //天干
		char[] y = new char[] {' ','子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'}; //地支
		char[] z = new char[] {' ','鼠','牛','虎','兔','龍','蛇','馬','羊','猴','雞','狗','豬'}; //生肖
		System.out.printf("%d是%c%c年,屬%c\n", year, x[heavenly_stems], y[earthly_branches], z[earthly_branches]);
	}

	public static void printDays(String day) {	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date targerDay = sdf.parse(day);	
			long targetTime = targerDay.getTime();
			long todaytime = new Date().getTime();
			long t = todaytime - todaytime % (1000 * 60 * 60 * 24) - 28800000;
			long time = Math.abs(targetTime - t);
			String today = sdf.format(new Date());
			//System.out.println("今天是" + today);
			System.out.println("距離" + day + "有" + (time) / 1000 / 60 / 60 / 24 + "天");
		} catch (ParseException e) {}
	}

	public static void printAfterDays(int days) {		
		
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DATE, days);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("往後" + days + "天是" + sdf.format(today.getTime()));
	}

	public static void main(String[] args) {
		boolean running = true;
		String instruction;
		Scanner scanner = new Scanner(System.in);
		while (running) {
			System.out.print(
					"輸入指令號碼或 E(結束使用)?\r\n\r\n" + 
					"輸入指令: \r\n" +
					"1) A 顯示該月月曆\r\n" +
					"2) B 西元轉換干支、生肖\r\n" +
					"3) C 計算天數\r\n" +
					"4) D 計算日期\r\n" +
					"5) E 結束使用\r\n");
			instruction = scanner.next();
			switch (instruction) {
			case "A":
				System.out.println("請輸入欲查詢日期(年/月/日): ");
				String date = scanner.next();
				printCalender(date);
				break;
			case "B":
				System.out.println("請輸入欲查詢年: ");
				int year = scanner.nextInt();
				printZodiac(year);
				break;
			case "C":
				System.out.println("請輸入您要算的日期 (格式:yyyy-MM-dd)");
				String day = scanner.next();
				printDays(day);
				break;
			case "D":
				System.out.println("請輸入往後推算的天數: ");
				int days = scanner.nextInt();
				printAfterDays(days);
				break;
			case "E":
				running = false;
				System.out.println("離開系統");
				break;
			default:
				System.out.println("指令錯了!");
				break;
			}
		}
		scanner.close();
	}
}