2013-11-22 41 views
2

我需要每週的姓名,但依賴於實際的日期和日期。例如,我需要今天的名字,然後明天的名字,日期的名稱+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; 
    } 

回答

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; 
    } 
} 
+0

thx很棒。) –

2

您可以直接使用

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

其中日曆已使用

calendar.setTime(); 

設置您可以設置一天的帶有日曆 -

calendar.set(int year, int month, int day); 

希望幫助

+0

在android/java中沒有設置setDay(n)或者我沒有找到它。 –

+1

我對此感到抱歉。你可以使用calendar.set(int year,int month,int day)。 如果你不介意我告訴,我是否可以知道爲什麼你將你的String數組中的前3個元素存儲爲「Sunday」。如果您查看日曆課程,DAY_OF_WEEK將接受星期日,星期一...至星期六,分別表示爲1,2 ... 7。 –

1

首先,如果從字符串數組中獲取日期名稱的做法很好,但如果從資源(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; 
} 
相關問題