在java中获取四分之一周的周数

时间:2017-04-04 05:26:41

标签: java date

我试图从一个季度内的日期获得周数。

从下面我可以得到一年中的周数,但不是在季度内。

String input = "20130805";
String format = "yyyyMMdd";

SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);

结果应该是第3季的周数

2 个答案:

答案 0 :(得分:0)

做一个模数会给你答案:

int x = 52 / 4; //52 week as ave in a year
System.out.println(week);
System.out.println("you are in the quartal number: " + (week / x + 1));
System.out.println("this is the week number: " + week % x + " in the quartal");

答案 1 :(得分:0)

试试这个,

String input = "20130805";
String format = "yyyyMMdd";

SimpleDateFormat df = new SimpleDateFormat(format);
Date date = df.parse(input);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int weekForYear = cal.get(Calendar.WEEK_OF_YEAR);
int previousQuarter = (date.getMonth() / 3);

System.out.println("No. of weeks for current Quarter: " + weekForYear - (previousQuarter*13));