Suppose you have 2 dates (for example: from 24/04/2016 to 29/10/2016) and you want to calculating number of Sunday or Monday,... between them. In Java, with 2 objects called
Date
and Calendar
, you can solve this problem easily by making a loop from first date to last date and comparing all dates between them with values of Calendar.DAY_OF_WEEK
. This this "day of week" values table:
Specific
day
|
Values
|
MONDAY
|
2
|
TUESDAY
|
3
|
WEDNESDAY
|
4
|
THURSDAY
|
5
|
FRIDAY
|
6
|
SATURDAY
|
7
|
SUNDAY
|
1
|
And this is function which calculating number of a specific day:
public static int calculateSpecificday(int specificDay, Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
int numberOfSpecificDay = 0;
while (c2.after(c1)) {
if (c1.get(Calendar.DAY_OF_WEEK) == specificDay)
numberOfSpecificDay ++;
c1.add(Calendar.DATE, 1);
}
return numberOfSpecificDay;
}
Building an Android example
First, making a simple layout like this:
activity_main.xml
When running, this is output:
<?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="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="19"
android:text="From:" />
<EditText
android:id="@+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:background="@drawable/border"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:gravity="center"
android:text="--" />
<EditText
android:id="@+id/month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:background="@drawable/border"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:gravity="center"
android:text="--" />
<EditText
android:id="@+id/year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:background="@drawable/border"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="19"
android:text="To:" />
<EditText
android:id="@+id/to_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:background="@drawable/border"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:gravity="center"
android:text="--" />
<EditText
android:id="@+id/to_month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:background="@drawable/border"
android:inputType="number" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:gravity="center"
android:text="--" />
<EditText
android:id="@+id/to_year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="16"
android:background="@drawable/border"
android:inputType="number" />
</LinearLayout>
<Button
android:id="@+id/button"
android:text="Calculate number of Sundays"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And the programming code, the most important work is converting the
Strings
input (in a format like "dd/MM/yyyy" to Date
by using SimpleDateFormat
):
MainActivity.java
Running app, we have this result:package info.devexchanges.calculatedate;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private EditText date;
private EditText month;
private EditText year;
private EditText toDate;
private EditText toMonth;
private EditText toYear;
private Button button;
private void findViews() {
date = (EditText) findViewById(R.id.date);
month = (EditText) findViewById(R.id.month);
year = (EditText) findViewById(R.id.year);
toDate = (EditText) findViewById(R.id.to_date);
toMonth = (EditText) findViewById(R.id.to_month);
toYear = (EditText) findViewById(R.id.to_year);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@SuppressLint("SimpleDateFormat")
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateFromString = getText(date) + "/" + getText(month) + "/" + getText(year);
String dateToString = getText(toDate) + "/" + getText(toMonth) + "/" + getText(toYear);
try {
Date date1 = formatter.parse(dateFromString);
Date date2 = formatter.parse(dateToString);
Log.i("MainActivity", "" + calculateSunday(Calendar.SUNDAY, date1, date2));
Toast.makeText(MainActivity.this, "number of sundays: " + calculateSunday(Calendar.SUNDAY, date1, date2),
Toast.LENGTH_SHORT).show();
} catch (ParseException e) {
e.printStackTrace();
Log.e("Main", "error");
}
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
public static int calculateSunday(int sunday, Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
int sundays = 0;
while (c2.after(c1)) {
if (c1.get(Calendar.DAY_OF_WEEK) == sunday)
sundays++;
c1.add(Calendar.DATE, 1);
}
return sundays;
}
private String getText(TextView textView) {
return textView.getText().toString();
}
}