Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.0 kB
3
Indexable
Never
import java.util.Arrays;
import java.util.Scanner;
import java.util.Date; 
import java.text.SimpleDateFormat; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.util.Calendar; 

public class Main {
    public static void main(String[] args) {
    	System.out.print("輸入指令號碼或 E(結束使用)\n");
    	System.out.print("輸入指令:\n");
    	System.out.print("1) A 顯示該月月曆\n");
    	System.out.print("2) B 西元轉換干支、生肖\n");
    	System.out.print("3) C 計算天數\n");
    	System.out.print("4) D 計算日期\n");
    	System.out.print("5) E 離開\n\n");
    	
    	Scanner scanner = new Scanner(System.in);
    	String select = scanner.next();
    	switch(select) {
    	case "A":
    		showMonth();
    		break;
    	case "B":
    		transform();
    		break;
    	case "C":
    	    Scanner scannerC = new Scanner(System.in);
    	    String date = scanner.next();
    		calDays(date);
    		scannerC.close();
    		break;
    	case "D":
    	    System.out.print("請輸入往後推算得天數: \n");
    	    Scanner scannerD = new Scanner(System.in);
    	    int count = scanner.nextInt();
    		calDate(count);
    		scannerD.close();
    		break;
    	case "E":
    		System.out.print("---EXIT---\n");
    		System.exit(0);
    		break;
    	default:
    		System.out.print("請重新輸入\n");
    		break;
    	}
    	
    	scanner.close();
    	
    }

    private static void showMonth() {
    	
    }
    
    private static void transform() {
    	
    }
    
        
    private static int calDays_helper(String string) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
    	    Date date = sdf.parse(string);
    	    Calendar calendar = Calendar.getInstance();
    	    calendar.setTime(date);
    	    Calendar calendar2 = Calendar.getInstance();
    	    Date today = calendar2.getTime();
    	    
    	    int days = (int) ((date.getTime()-today.getTime()) / (1000*3600*24));
    	    return days;
        } catch (ParseException e) {
            e.printStackTrace();
            
            return 0;
        }
    }
    
    private static void calDays(String string) {
    	System.out.print("請輸入欲查詢日期(年/月/日):\n");
    	int ans = calDays_helper(string);

    	System.out.println(string + "距離今天還有" + ans + "天\n");
    }

    
    private static void calDate(int count) {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    	String ans = "";
	    Calendar calendar = Calendar.getInstance();
	    Date startDay = calendar.getTime();
        Calendar cl = Calendar.getInstance();
        cl.setTime(startDay);
        cl.add(Calendar.DATE, count);
        ans = sdf.format(cl.getTime());
        
    	System.out.println("往後" + count + "天是" + ans + "\n");
    }
}