gpt4 book ai didi

java - 下周在 android 中实现

转载 作者:搜寻专家 更新时间:2023-11-01 08:59:22 25 4
gpt4 key购买 nike

在我的示例项目中,我必须在 TextView 中实现下周星期一到星期日(例如 6 May >> 12 My)。单击下一步按钮,它必须显示下周的开始日期和结束日期(如 5 月 13 日 >> 5 月 19 日)。我已经使用以下代码实现了初始周 View

   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);

点击下周按钮我已经完成了这个但是它是静态的它只是一次尝试:)

onclick 会调用这个函数 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());


}

请告诉我如何显示下周的开始和结束日期?

最佳答案

这是您的解决方案。

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);

按钮实现更新

写一个方法,它将weekNumber作为参数..

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

现在声明一个静态文件为

private static int weekNumber = -1; 

然后在点击按钮时写下代码

weekNumber = weekNumber + 1;
gestureEvent.setText(getNextWeek(weekNumber));

这会起作用。

编码愉快:)

关于java - 下周在 android 中实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com