Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.7 kB
1
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":
    	    System.out.print("請輸入欲查詢日期(年/月/日):\n");
    	    Scanner scannerA = new Scanner(System.in);
    	    String dateA = scanner.next();
    		showMonth(dateA);
    		scannerA.close();
    		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(String string) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
    	    Date date = sdf.parse(string);
    	    Calendar calendar = Calendar.getInstance();
    	    calendar.setTime(date);
    	    int month = calendar.get(Calendar.MONTH);
    	    int year = calendar.get(Calendar.YEAR);

            
                	    
    	    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); //get day of week for 1st of month
            int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            
            System.out.println(month);
            System.out.println(year);
            
            System.out.println(dayOfWeek);
            System.out.println(daysInMonth);
            
            System.out.println("Sun Mon Tus Wen Thu Fri Sat");

            //print initial spaces
            String initialSpace = "";
            for (int i = 0; i < dayOfWeek - 1; i++) {
                initialSpace += "    ";
            }
            
            System.out.print(initialSpace);
    	    for (int i = 0, dayOfMonth = 1; dayOfMonth <= daysInMonth; i++) {
                for (int j = ((i == 0) ? dayOfWeek - 1 : 0); j < 7 && (dayOfMonth <= daysInMonth); j++) {
                    System.out.printf("%3d ", dayOfMonth);
                    dayOfMonth++;
                }
                System.out.println();
            }
    	    
    	    return ;
        } catch (ParseException e) {
            e.printStackTrace();
            
            return;
        }
    }

    
    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");
    }
}