如何获取特定周内特定日期的日期

时间:2016-10-02 12:00:39

标签: java android jodatime

我想知道特定一周中特定日期的日期,以周数表示。

以下是一个例子:

今天是星期一(2016年10月3日),这是58号的一周。 现在我想得到例如本周五。

我在我的Android应用程序中使用Joda-Time。

目前我正在创建一个LocalDate。

    // This is the date of today
private LocalDate reportDate = new LocalDate();

// The number of the week is calculated here
int week = Weeks.weeksBetween(startDate.dayOfWeek().withMinimumValue().minusDays(1),
        reportDate.dayOfWeek().withMaximumValue().plusDays(1)).getWeeks();

switch(day_of_week) {
    case "Monday":
        // Get date of this day in current week
        break;
    case "Tuesday":
        // Get date of this day in current week
        break;
    // ...

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码来实现所需的日期,

    LocalDateTime yourDate = LocalDateTime.now();
    System.out.println(yourDate.getWeekOfWeekyear());
    int weekOfyear = yourDate.getWeekOfWeekyear();
    //Fetch Week Start Date for Given Week Number
    DateTime weekStartDate = new DateTime().withWeekOfWeekyear(weekOfyear);
    System.out.println(weekStartDate.toString());
    //Fetch Specific Days for given week
    DateTime wedDateTime = weekStartDate.withDayOfWeek(DateTimeConstants.WEDNESDAY);

我希望这能回答你的问题。

答案 1 :(得分:1)

根据我对您的要求的理解,我已经实现了以下代码,只是检查它是否对您有帮助,

首先导入日历包

import java.util.Calendar;

现在创建以下功能

public String getSpecificDate(int weekOfYear, int dayOfWeek)
{    

   Calendar cal = Calendar.getInstance();

    cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
    cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);      // 0 to 11
    int day = cal.get(Calendar.DAY_OF_MONTH);

    String selectedDate = " " + day + "-" +  (month+1) + "-" + year;
    return selectedDate;
}

// I have passed getSpecificDate(41, 6) and get 7-10-2016 as output