年と週

1607 ワード

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;



public class DateUtil {

	

	/* , */

	public static String getWeekGap(int year, int weekNum) {

		Calendar c = Calendar.getInstance();

		c.set(year, 0, 01); //  

		int weekDay = c.get(Calendar.DAY_OF_WEEK) == 1 ? 8 : c

				.get(Calendar.DAY_OF_WEEK);

		c.add(Calendar.DATE, 7 * weekNum);



		c.add(Calendar.DATE, Calendar.MONDAY - weekDay);

		Date start = c.getTime();

		c.add(Calendar.DATE, 6);

		Date end = c.getTime();



		String datestr = String.format("%1$tY-%1$tm-%1$td", start) + " "

				+ String.format("%1$tY-%1$tm-%1$td", end);

		return datestr;

	}

	/* */

	public static String getWeekGap_Default() {

		Calendar c = Calendar.getInstance();

		c.getTime();

		int weekDay = c.get(Calendar.DAY_OF_WEEK) == 1 ? 8 : c

				.get(Calendar.DAY_OF_WEEK);

		c.add(Calendar.DATE, Calendar.MONDAY - weekDay);

		Date start = c.getTime();

		c.add(Calendar.DATE, 6);

		Date end = c.getTime();



		String datestr = String.format("%1$tY-%1$tm-%1$td", start) + " "

				+ String.format("%1$tY-%1$tm-%1$td", end);

		return datestr;

	}

	/* */

	public static int getWeekNumOfYear(){

		TimeZone zone=TimeZone.getTimeZone("Asia/Shanghai");

		Calendar cal = Calendar.getInstance(zone);

		int c = cal.get(Calendar.WEEK_OF_YEAR);

		System.out.println(c);

		return c-1;

	}

	

	public static int getYear(){

		Calendar ca = Calendar.getInstance();

	    ca.setTime(new java.util.Date());

		return ca.get(Calendar.YEAR);

	}

	

}