我想在calendarDaysBetween()
中使用TextView
方法,或者希望我的TextView
显示两个日期的差异。任何人都可以帮我这个吗?
public class MainActivity extends FragmentActivity {
static EditText metTodate, metFromdate, metInTime, metOutTime;
static long no_of_days1;
static long no_of_days2;
Button mbtnApplyLeave;
ImageView mivBack;
RadioButton halfday1, halfday2, first_half, second_half, first_half1, second_half1, full_day1, full_day2;
static TextView no_of_days;
static TextView no_of_days3;
public static String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
metTodate = (EditText)findViewById(R.id.etTodate);
metFromdate = (EditText)findViewById(R.id.etFromdate);
metInTime = (EditText)findViewById(R.id.etInTime);
metOutTime = (EditText)findViewById(R.id.etOutTime);
mivBack = (ImageView)findViewById(R.id.ivBack);
halfday1 = (RadioButton)findViewById(R.id.halfday1);
halfday2 = (RadioButton)findViewById(R.id.halfday2);
first_half = (RadioButton)findViewById(R.id.firsthalf1);
second_half = (RadioButton)findViewById(R.id.secondhalf1);
first_half1 = (RadioButton)findViewById(R.id.firsthalf2);
second_half1 = (RadioButton)findViewById(R.id.secondhalf2);
full_day1 = (RadioButton)findViewById(R.id.fullday1);
full_day2 = (RadioButton)findViewById(R.id.fullday2);
no_of_days = (TextView)findViewById(R.id.etnoofdays);
// Here is my method where I want my text view to display dates.
no_of_days.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendarDaysBetween(Calendar metFromdate, Calendar metTodate);
}
});
}
// Both date picker dialogs
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metTodate.setText(day + "/" + (month + 1) + "/" + year);
}
}
public void showFromDatePickerDialog(View v) {
DialogFragment newFragment = new FromDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class FromDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c1 = Calendar.getInstance();
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day1);
// calendarDaysBetween(Calendar metFromdate, Calendar metTodate);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metFromdate.setText(day + "/" + (month + 1) + "/" + year);
}
}
public static long calendarDaysBetween(Calendar metFromdate, Calendar metTodate) {
// Create copies so we don't update the original calendars.
Calendar start = Calendar.getInstance();
start.setTimeZone(metFromdate.getTimeZone());
start.setTimeInMillis(metFromdate.getTimeInMillis());
Calendar end = Calendar.getInstance();
end.setTimeZone(metTodate.getTimeZone());
end.setTimeInMillis(metTodate.getTimeInMillis());
// Set the copies to be at midnight, but keep the day information.
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
end.set(Calendar.HOUR_OF_DAY, 0);
end.set(Calendar.MINUTE, 0);
end.set(Calendar.SECOND, 0);
end.set(Calendar.MILLISECOND, 0);
// At this point, each calendar is set to midnight on
// their respective days. Now use TimeUnit.MILLISECONDS to
// compute the number of full days between the two of them.
no_of_days1 = TimeUnit.MILLISECONDS.toDays(Math.abs(end.getTimeInMillis() - start.getTimeInMillis()));
String finalresult = new Double(no_of_days1).toString();
no_of_days.setText(finalresult);
return no_of_days1;
}
}
答案 0 :(得分:1)
试试这个
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date past = format.parse("05/06/2015");
Date now = new Date();
System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");
它会给你与当前日期的分钟和小时不同,你可以使用自己的日期格式和自己的日期对象
答案 1 :(得分:1)
您可以使用以下代码获取天数的日期差异。
public void calendarDaysBetween(Calendar metFromdate, Calendar etTodate)
{
long diff = (metFromdate.getTimeInMillis() - metTodate.getTimeInMillis());
long no_of_days1 = Math.abs(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
no_of_days.setText(no_of_days1+"");
}
首先去除日历对象
public Calendar metFromdate= Calendar.getInstance();
public Calendar metTodate== Calendar.getInstance();
更改第一个DatePicker值
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metFromdate.set(Calendar.YEAR, year);
metFromdate.set(Calendar.MONTH, month);
metFromdate.set(Calendar.DAY_OF_MONTH, day);
}
更改第二个DatePicker值
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metTodate.set(Calendar.YEAR, year);
metTodate.set(Calendar.MONTH, month);
metTodate.set(Calendar.DAY_OF_MONTH, day);
}
更改按钮单击
no_of_days.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendarDaysBetween(metFromdate, metTodate);
}
});
答案 2 :(得分:0)
你用过它
Calendar date1 = Calendar.getInstance(); Calendar date2 =
Calendar.getInstance();
date1.clear();
date1.set(datePicker1.getYear(),datePicker1.getMonth(),datePicker1.getDayOfMonth());
date2.clear();
date2.set(datePicker2.getYear(),datePicker2.getMonth(),datePicker2.getDayOfMonth());
long diff = date2.getTimeInMillis() - date1.getTimeInMillis();
float dayCount = (float) diff / (24 * 60 * 60 * 1000);
textView.setText(Long.toString(diff) + " " + (int) dayCount);
答案 3 :(得分:0)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
........
........
compile 'joda-time:joda-time:2.2'
}
和
public String getTimeAgo(long ago){
DateTime timeAgo = new DateTime(ago*1000L);
DateTime now = new DateTime();
Period period = new Period(timeAgo, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" Years, ")
.appendMonths().appendSuffix(" Months, ")
.appendWeeks().appendSuffix(" Weeks, ")
.appendDays().appendSuffix(" Days, ")
.appendHours().appendSuffix(" Hours, ")
.appendMinutes().appendSuffix(" Minutes, ")
.appendSeconds().appendSuffix(" Seconds ago ")
.printZeroNever()
.toFormatter();
String elapsed = formatter.print(period);
return elapsed;
}
和
yourTextView.setText(getTimeAgo(long-your_past_date_millis));