Java如何顯示一年的周數?


在Java中,如何查找一年中或一個月中的第幾個星期?

以下示例顯示年份和月份的第幾周。

package com.yiibai; import java.util.*; public class DisplayWeekNumber { public static void main(String[] args) throws Exception { Date d1 = new Date(); Calendar cl = Calendar.getInstance(); cl.setTime(d1); System.out.println("today is " + cl.WEEK_OF_YEAR + " week of the year"); System.out.println("today is a " + cl.DAY_OF_MONTH + "month of the year"); System.out.println("today is a " + cl.WEEK_OF_MONTH + "week of the month"); } } 
Java

上述代碼示例將產生以下結果。

today is 3 week of the year
today is a 5month of the year
today is a 4week of the month
Shell

示例-2
以下是一年中的一個月的一個例子。

package com.yiibai; import java.util.Calendar; public class DisplayWeekNumber2 { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("Current week of month is : " + cal.get(Calendar.WEEK_OF_MONTH)); System.out.println("Current week of year is : " + cal.get(Calendar.WEEK_OF_YEAR)); cal.add(Calendar.WEEK_OF_MONTH, 1); System.out.println("date after one year : " + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DATE) + "-" + cal.get(Calendar.YEAR)); } } 
Java

上述代碼示例將產生以下結果。

Current week of month is : 4
Current week of year is : 38
date after one year : 9-24-2017


免責聲明!

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



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