我正在开发一个Android应用程序,我从服务器获取开始日期和结束日期。
例如:2016年6月20日和20-06-2017
我想找到这两个日期之间的日期列表,间隔为10天。 不包括星期六和星期日。
例如: 20-06-2016 是星期一,所以下一个日期应 04-07-2016 (不包括星期六和星期日)。等等。
在每个日期之后(从日期列表中)我想在该特定日期的日历中添加事件,以便我可以通过某些消息通知用户。
我编写了在日历中添加事件的代码,并在特定时间通知用户。所以现在我只想要日期列表。
请帮帮我。
谢谢。
答案 0 :(得分:4)
我设法完成了这项任务。
这是代码
public List<String> findDates(String startDate,String endDate) throws ParseException {
List<Date> dates = new ArrayList<Date>(); //this list will store all the dates from startDate to endDate
List<String> intervalDates = new ArrayList<>(); //this list will store dates excluding saturday and sunday.
DateFormat formatter;
formatter = new SimpleDateFormat("MM-dd-yyyy");
Date sDate = (Date) formatter.parse(startDate);
Date eDate = (Date) formatter.parse(endDate);
long interval = 24 * 1000 * 60 * 60; // 1 day in millis
long endTime = eDate.getTime(); // create your endtime here, possibly using Calendar or Date
long curTime = sDate.getTime();
while (curTime <= endTime) {
dates.add(new Date(curTime));
curTime += interval; //adding the interval of 1day
}
for (int i = 0; i < dates.size(); i++) {
Date lDate = (Date) dates.get(i); // getting each date from the list of all dates to find the day name
String ds = formatter.format(lDate);
Date day = formatter.parse(ds);
DateFormat dayFormat = new SimpleDateFormat("EEEE");
String dy = dayFormat.format(day); // to get the day name
if (dy.equalsIgnoreCase("Saturday") || dy.equalsIgnoreCase("Sunday")) {
continue;
}
intervalDates.add(ds); //adding date excluding saturday and sunday.
//Log.d(TAG, "findDates: Date is:" + ds + " " + dy);
}
return intervalDates;
}
答案 1 :(得分:1)
以下代码的输出是:
20-06-2016
2016年4月7日
我希望它有所帮助
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HelloWorld{
public static void main(String []args) throws ParseException{
String str = "20-06-2016";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = formatter.parse(str);
String dater = formatter.format(date);
System.out.println(dater);
Date dateNew = new Date(date.getYear(), date.getMonth(), date.getDate() +14);
System.out.println(formatter.format(dateNew));
}
}
来源:
答案 2 :(得分:0)
要使用日期和时间间隔,我建议您使用JodaTime库。它包含您需要的所有功能。
http://www.joda.org/joda-time/
http://joda-time.sourceforge.net/userguide.html
它是一个罐子,因此您可以轻松地将其包含在您的项目中。
希望它有所帮助。
答案 3 :(得分:0)
emrah答案是可以的,但如果你必须排除星期日和星期六,我建议使用Calendar类的实例。例如:
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Calendar calendar = Calendar.getInstance();
calendar.setTime(format.parse("26-06-2016"));
int dow = calendar.get(Calendar.DAY_OF_WEEK);
if (dow == Calendar.SATURDAY)
calendar.add(Calendar.DATE, 2);
else if (dow == Calendar.SUNDAY)
calendar.add(Calendar.DATE, 1);
int intervalCount = 10;
// List of dates
List<Date> dates = new ArrayList<>(intervalCount);
for(int i = 0; i < intervalCount; i++) {
// Two weeks, I guess
calendar.add(Calendar.DATE, 14);
dates.add(calendar.getTime());
}
for (Date date : dates) {
Log.e(TAG, format.format(date));
}
答案 4 :(得分:0)
尝试以下代码,它必须有所帮助:
public class MainActivity extends AppCompatActivity {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
int no_of_days = 0;
List<Date> dates = null;
RecyclerView recycler_view;
Calendar calendar = null;
RecyAdapter recyAdapter = null;
LinearLayoutManager linearLayoutManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
dates = new ArrayList<>();
linearLayoutManager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
recycler_view.setLayoutManager(linearLayoutManager);
no_of_days = getNoOfDays("20-06-2016", "20-08-2016");
calendar = Calendar.getInstance();
try {
calendar.setTime(new SimpleDateFormat("dd-MM-yyyy").parse("20-06-2016"));
} catch (ParseException e) {
e.printStackTrace();
}
for (int i = 0; i < no_of_days; i++) {
calendar.add(Calendar.DATE, 10);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
} else {
dates.add(calendar.getTime());
}
}
recyAdapter = new RecyAdapter(MainActivity.this, dates);
recycler_view.setAdapter(recyAdapter);
}
public int getNoOfDays(String Created_date_String, String Expire_date_String) {
Date Created_convertedDate = null, Expire_CovertedDate = null, todayWithZeroTime = null;
try {
Created_convertedDate = DATE_FORMAT.parse(Created_date_String);
Expire_CovertedDate = DATE_FORMAT.parse(Expire_date_String);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar createdDay = Calendar.getInstance();
createdDay.setTime(Created_convertedDate);
Calendar expireDay = Calendar.getInstance();
expireDay.setTime(Expire_CovertedDate);
long diff = createdDay.getTimeInMillis() - expireDay.getTimeInMillis();
long days = (diff / (24 * 60 * 60 * 1000));
return (Math.abs((int) days))/10;
}
}
其Adapter类如下:
public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.ViewHolder> {
Context context;
List<Date> dates = null;
public RecyAdapter(Context context, List<Date> dates) {
this.context = context;
this.dates = dates;
}
@Override
public RecyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyAdapter.ViewHolder holder, int position) {
holder.txt.setText(MainActivity.DATE_FORMAT.format(dates.get(position)));
}
@Override
public int getItemCount() {
return dates.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
public ViewHolder(View itemView) {
super(itemView);
txt = (TextView) itemView.findViewById(R.id.txt);
}
}
}
它的布局是:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fitness.vario.stacklistofdates.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/txt"/>
</LinearLayout>