让一周的代价为一天

时间:2015-10-12 12:12:19

标签: android

我想让一周对应一个日期。例如,如果我放置2012年12月24日我希望从太阳获得一周:坐下来,本周应该包含dec24。

我尝试了几个代码。目前我正在使用此代码

<feature name="InAppBrowser">
  <param name="android-package" value="org.apache.cordova.InAppBrowser"/>
</feature>
<feature name="InAppBrowser">
  <param name="ios-package" value="CDVInAppBrowser" />
</feature>

2 个答案:

答案 0 :(得分:0)

修改上面的代码如下

public static String[] getpreviousweek(Calendar calendar) {
    // TODO Auto-generated method stub
     Calendar c = calendar;
        // Set the calendar to monday of the current week
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        c.add(Calendar.DATE, num * 7);
        // Print dates of the current week starting on Monday
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        String[] days = new String[7];

          for (int i = 0; i < 7; i++) 
             {
                  days[i] = df.format(c.getTime());
                   c.add(Calendar.DAY_OF_MONTH, 1);
             } 
     return days;
 }

并将此方法称为

Calendar inputDate = GregorianCalendar.getInstance();
 inputDate.set(2012, Calendar.DECEMBER, 24);
 String[] dates = getpreviousweek(inputDate);

答案 1 :(得分:0)

首先查找当天的当前日期并获取该周的startDate以及该特定周的EndDate。

try {
        String yourDateString="10-12-2015";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy");
        Date yourDate=df.parse(yourDateString);
        c.setTime(yourDate);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        Log.e("day", "" + dayOfWeek);

        c.add(Calendar.DAY_OF_YEAR, dayOfWeek * -1);
        Log.e("startDate", c.getTime().toString());
        Date startDate=c.getTime();
        c.add(Calendar.DAY_OF_YEAR, 7);
        Log.e("endDate", c.getTime().toString());
        Date endDate=c.getTime();

}
catch(Exception  e)
{
    e.printStackTrace();
}

然后您可以获得该周的日期列表。

List<Date> dates = new ArrayList<Date>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate);
while (calendar.getTime().before(endDate))
{
    Date result = calendar.getTime();
    dates.add(result);
    calendar.add(Calendar.DATE, 1);
}
dates.add(calendar.getTime());
for(int i=0;i<dates.size();i++)
{
     System.out.print("Day " + (i+1) + " = " + dates.get(i).toString());
}