Tìm tuần trong năm trong Java



Miêu tả vấn đề

Cách tìm một tuần của năm trong Java?

Giải pháp

Ví dụ sau minh họa cách hiển thị số tuần của năm hoặc tháng trong 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");
   }
}

Kết quả

Code trên sẽ cho kết quả sau:

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

date_time_trong_java.jsp