字符串格式的日期名称,以及将来的日期

时间:2013-11-22 07:28:50

标签: android calendar dayofweek days

我需要一周中的名字,但是依赖于实际的日期和日期。例如,我需要今天的名字,然后明天的名字,日期的名称+2天等我制作这个代码但不能正常工作。那么有什么更好的方法可以让我在android中获得一天的名字?

 public String getDay3Name() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String[] days = new String[] { "SUNDAY","SUNDAY","SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
        String day = days[calendar.get(Calendar.DAY_OF_WEEK)+1];
        return day;
    }
    public String getDay4Name() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String[] days = new String[] { "SUNDAY","SUNDAY","SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
        String day = days[calendar.get(Calendar.DAY_OF_WEEK)+2];
        return day;
    }

3 个答案:

答案 0 :(得分:2)

您可以直接使用

calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);

使用

设置日历
calendar.setTime();

您可以使用 -

在日历中设置日期
calendar.set(int year, int month, int day);

希望有所帮助

答案 1 :(得分:2)

import java.util.Calendar;
import java.util.Locale;


public class CalSample {

    private static final int TODAY = 0;
    private static final int TOMORROW = 1;
    private static final int DAY_AFTER_TOMORROW = 2;
    private static final int THREE_DAYS_FROM_TODAY = 3;
    private static final int FOUR_DAYS_FROM_TODAY = 4;
    private static final int FIVE_DAYS_FROM_TODAY = 5;
    private static final int SIX_DAYS_FROM_TODAY = 6;

    public static void main(String args[]){
        CalSample calSample = new CalSample();
        calSample.getNameOfDayOfWeek(TODAY);
        calSample.getNameOfDayOfWeek(TOMORROW);
        calSample.getNameOfDayOfWeek(DAY_AFTER_TOMORROW);
        calSample.getNameOfDayOfWeek(THREE_DAYS_FROM_TODAY);
        calSample.getNameOfDayOfWeek(FOUR_DAYS_FROM_TODAY);
        calSample.getNameOfDayOfWeek(SIX_DAYS_FROM_TODAY);

    }
    public String getNameOfDayOfWeek(int noOfDaysFromToday){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, noOfDaysFromToday);
        String name = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH);
        System.out.println("Day Of Week:- " + name);
        return name;
    }
}

答案 2 :(得分:1)

首先,您在从String数组中获取日期名称方面做得很好,但如果从资源(strings.xml)获取它们以便将来支持多语言,那么它会更好。

其次,你应该知道从calendar.get(Calendar.DAY_OF_WEEK)得到什么样的结果;方法:

Calendar.SUNDAY    = (1)
Calendar.MONDAY    = (2)
Calendar.TUESDAY   = (3)
Calendar.WEDNESDAY = (4)
Calendar.THURSDAY  = (5)
Calendar.FRIDAY    = (6)
Calendar.SATURDAY  = (7)

最后,你的日期名称数组是错误的;

new String[] { "SUNDAY","SUNDAY","SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };

应该是;

new String[] {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };

你可以尝试下面的方法,它们只是对你的方法进行了一些修改。

/**
 * 
 * @param next
 * @return
 * 
 * send next 0 if you want to get current day name 
 */
public String getDayName(int next) {
    int dayIndex = getCurrentDayIndex() - 1;
    dayIndex += next;
    dayIndex = dayIndex % 7;

    String[] days = new String[] {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
    String day = days[dayIndex];
    return day;
}

/**
 * 
 * @return  1,2,3,4,5,6 or 7
 */
public int getCurrentDayIndex() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());   
    int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
    return dayIndex;
}