位置:首頁 > Java技術 > java實例教學 > Java顯示一年中的周數

Java顯示一年中的周數

如何找到一個星期是在哪一年中的月份?

解決方法

下麵的示例顯示第幾個星期在年份及月份中。

import java.util.*;

public class Main {
   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");
   }
}

結果

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

today is 30 week of the year
today is a 5month of the year
today is a 4week of the month