問(wèn)題描述
在我的示例項(xiàng)目中,我必須在下周周一至周日在文本視圖中實(shí)施(如 5 月 6 日 >> 12 My).單擊下一個(gè)按鈕時(shí),它必須顯示下周的開(kāi)始日期和結(jié)束日期(如 5 月 13 日 >> 5 月 19 日).我已經(jīng)使用以下代碼實(shí)現(xiàn)了初始周視圖
In my sample project i have to implement next week Monday to sunday in a text view (like 6 May >> 12 My). on click of next button it must show next week start date and end date (like 13 May >> 19 May). I have implemented the intial week view with the following code
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat df = new SimpleDateFormat("dd MMMM");
String printDate = df.format(c.getTime());
//gestureEvent.setText(reportDate);
c.add(Calendar.DAY_OF_WEEK, 6);
String printDate2 = df2.format(c.getTime());
gestureEvent.setText(reportDate +" >> "+reportDate2);
點(diǎn)擊下周按鈕我已經(jīng)這樣做了,但它是靜態(tài)的,這只是一次嘗試:)
on click of next week button i have done this but it static it was just an attempt attempt :)
onclick 會(huì)調(diào)用這個(gè)函數(shù) goNextWeek()
onclick will call this function goNextWeek()
public void goNextWeek()
{
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.add(Calendar.DAY_OF_WEEK, 6);
System.out.println("End Date : " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd MMMM");
String reportDate = df.format(c.getTime());
gestureEvent.setText(reportDate);
c.add(Calendar.DAY_OF_WEEK, dates);
c.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("End Date asdfadf: " + c.getTime());
}
請(qǐng)告訴我如何顯示下周的開(kāi)始和結(jié)束日期?
please tell me how to show next week start and end date?
推薦答案
這是您的解決方案.
Calendar mCalendar = new GregorianCalendar();
mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
String printDate = mDF.format(mCalendar.getTime());
mCalendar.add(Calendar.DAY_OF_MONTH, 6);
String printDate2 = mDF.format(mCalendar.getTime());
System.out.println(printDate + " >> " + printDate2);
gestureEvent.setText(printDate + " >> " + printDate2);
<小時(shí)>
按鈕實(shí)施更新
寫(xiě)一個(gè)方法,將weekNumber作為參數(shù)..
Write a method, which will take weekNumber as params..
private static String getNextWeek(int weekFromToday) {
Calendar mCalendar = new GregorianCalendar();
mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
mCalendar.set(Calendar.WEEK_OF_YEAR,
mCalendar.get(Calendar.WEEK_OF_YEAR) + weekFromToday);
SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
String printDate = mDF.format(mCalendar.getTime());
System.out.println(printDate);
//gestureEvent.setText(reportDate);
mCalendar.add(Calendar.DAY_OF_MONTH, 6);
String printDate2 = mDF.format(mCalendar.getTime());
System.out.println(printDate + " >> " + printDate2);
return printDate + " >> " + printDate2;
}
現(xiàn)在聲明一個(gè)靜態(tài)文件為
Now declaire a static filed as
private static int weekNumber = -1;
并在按鈕單擊時(shí)編寫(xiě)以下代碼
and write below code on button click
weekNumber = weekNumber + 1;
gestureEvent.setText(getNextWeek(weekNumber));
這會(huì)起作用.
編碼愉快 :)
這篇關(guān)于下周在android中實(shí)現(xiàn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!