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