[英]Getting dates from week number of year in android
I am trying to get the dates from a given weeknumber and the year. 我正在尝试从给定的周数和年份中获取日期。 The below piece of code is always showing the currentDates even though I enter week and year.
即使我输入星期和年份,下面的代码也始终显示currentDates。
Calendar c = new GregorianCalendar(Locale.getDefault());
c.set(Calendar.WEEK_OF_YEAR, 15);
c.set(Calendar.YEAR, 2015);
String result = "";
int firstDayOfWeek = c.getFirstDayOfWeek();
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
result += new SimpleDateFormat("yyyy.MM.dd").format(c.getTime()) + "\n";
}
instead of getting dates of Week number 15. I am getting current dates values always. 而不是获取第15周的日期。我总是获取当前日期值。 I am not what is wrong here?
我不是这里有什么问题吗?
How to solve this issue? 如何解决这个问题?
Thanks! 谢谢!
Try this 尝试这个
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2015);
now.set(Calendar.WEEK_OF_YEAR,15);
System.out.println(new SimpleDateFormat("yyyy.MM.dd").format(now.getTime()));
OUTPUT
OUTPUT
please update the my code: it's works for me. 请更新我的代码:它对我有用。
Calendar now = Calendar.getInstance();
now.set(Calendar.YEAR,2015);
now.set(Calendar.WEEK_OF_YEAR,15);
String result = "";
int firstDayOfWeek = now.getFirstDayOfWeek();
Log.e("day","day:"+firstDayOfWeek);
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
now.set(Calendar.DAY_OF_WEEK, i);
result += new SimpleDateFormat("yyyy.MM.dd").format(now.getTime()) + "\n";
}
Log.e("result","result:"+result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.