Javaexamples-date-weekday
提供:Dev Guides
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
次は、年、月の週の別の例です。
import java.util.Calendar;
public class GetWeekOfMonthAndYear {
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));
}
}
上記のコードサンプルは、次の結果を生成します。
Current week of month is : 2
Current week of year is : 46
date after one year : 11-18-2016