Java 給定一個日期,求對應的周數、星期幾等


 

主要用到了SimpleDateFormat這個類,該類有一個方法:void applyPattern(String pattern) 用來將給定模式字符串應用於日期格式。

 

public static void main(String[] args) throws ClassNotFoundException,
            IOException, ParseException {
        // TODO Auto-generated method stub
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請鍵入日期(如:2008-8-8):");
        String str = br.readLine();
        Date date = dateFormatter.parse(str);
        dateFormatter.applyPattern("D");//
        System.out.println("一年中的第幾天:" + dateFormatter.format(date));

        dateFormatter.applyPattern("d");
        System.out.println("一個月中的第幾天:" + dateFormatter.format(date));

        dateFormatter.applyPattern("w");
        System.out.println("一年中的第幾周:" + dateFormatter.format(date));

        dateFormatter.applyPattern("W");
        System.out.println("一個月中的第幾周:" + dateFormatter.format(date));

        dateFormatter.applyPattern("E");
        System.out.println("一個星期中的天數:" + dateFormatter.format(date));

        br.close();
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM