过去两天我一直坚持这个问题。我的问题是:如何显示从一周到另一周(周四到周四)的日期?例如:
1/30/2014 to 2/6/2014
或
30 jan 2014 to 6 feb 2014
当一周完成后,就会发生变化,如:
2014年2月6日至2014年2月13日
或
2014年2月6日至2014年3月13日
任何帮助或示例代码都将受到高度赞赏。
答案 0 :(得分:1)
试试这个,
String start_date = "01-30-2014"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
try {
c.setTime(sdf.parse(start_date));
} catch (ParseException e) {
e.printStackTrace();
}
cal.add(Calendar.DATE, 7); // number of days to add,in your case its 7
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
String to_date = sdf1.format(cal.getTime());
其实我不喜欢android日历(我更喜欢Joda-Time)但是上面的解决方案应该适合你
答案 1 :(得分:1)
最后我得到了一个解决方案并解决了我的问题:
在oncreate中:
TextView tv_chart_menuvotes = (TextView) findViewById(R.id.tv_chart_menuvotes);
String csPrevThur = getPreviousThursday();
String csNextThur = getNextThursday();
tv_chart_menuvotes.setText("Vote from " + csPrevThur + " To "+ csNextThur);
在oncreate之外:
public String getPreviousThursday() {
String csDate = "";
int perSut = 0;
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
switch (day) {
case Calendar.SUNDAY:
perSut = -3;
break;
case Calendar.MONDAY:
perSut = -4;
break;
case Calendar.TUESDAY:
perSut = -5;
break;
case Calendar.WEDNESDAY:
perSut = -6;
break;
case Calendar.THURSDAY:
perSut = 0;
break;
case Calendar.FRIDAY:
perSut = -1;
break;
case Calendar.SATURDAY:
perSut = -2;
break;
}
SimpleDateFormat mDF = new SimpleDateFormat("dd-MM-yyyy");
calendar.add(Calendar.DAY_OF_MONTH, perSut);
csDate = mDF.format(calendar.getTime());
System.out.println("Prev Thursday >> " + csDate);
return csDate;
}
public String getNextThursday() {
String csDate = "";
int perSut = 0;
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
switch (day) {
case Calendar.SUNDAY:
perSut = 4;
break;
case Calendar.MONDAY:
perSut = 3;
break;
case Calendar.TUESDAY:
perSut = 2;
break;
case Calendar.WEDNESDAY:
perSut = 1;
break;
case Calendar.THURSDAY:
perSut = 7;
break;
case Calendar.FRIDAY:
perSut = 6;
break;
case Calendar.SATURDAY:
perSut = 5;
break;
}
SimpleDateFormat mDF = new SimpleDateFormat("dd-MM-yyyy");
calendar.add(Calendar.DAY_OF_MONTH, perSut);
csDate = mDF.format(calendar.getTime());
System.out.println("NextThursday >> " + csDate);
return csDate;
}
答案 2 :(得分:0)
在您的情况下,setMaxDate()
是您的朋友。文档说:
设置此DatePicker支持的最大日期(以毫秒为单位) 自1970年1月1日00:00:00起getDefault()时区。
因此,将下周的时间作为long
并使用该方法。