Get the difference between two Calendar

Modify the exercise "android.app.TimePickerDialog", calculate the difference between time and next set time in TimePickerDialog.

Get the difference between two Calendar


package com.exercise.AndroidTime;

import java.util.Calendar;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class AndroidTimeActivity extends Activity {

TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textPrompt;

TimePickerDialog timePickerDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textPrompt = (TextView)findViewById(R.id.prompt);

buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
openTimePickerDialog(false);

}});

}


private void openTimePickerDialog(boolean is24r){
Calendar calendar = Calendar.getInstance();

timePickerDialog = new TimePickerDialog(
AndroidTimeActivity.this,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("TimePickerDialog Title");
timePickerDialog.setMessage("TimePickerDialog Message");

timePickerDialog.show();

}

OnTimeSetListener onTimeSetListener
= new OnTimeSetListener(){

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();

calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);

String stringPrompt = "Current Time is: " + calNow.getTime() + " / " + calNow.getTimeInMillis() + "\n"
+ "Set Time is: " + calSet.getTime() + " / " + calSet.getTimeInMillis() + "\n"
+ "calSet.compareTo(calNow) = " + calSet.compareTo(calNow) + "\n\n";

if(calSet.compareTo(calNow) > 0){
//Today Set time not yet passed
long diff = calSet.getTimeInMillis() - calNow.getTimeInMillis();
stringPrompt += "Millis to set time = " + diff;
}else{
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
long diff = calSet.getTimeInMillis() - calNow.getTimeInMillis();
stringPrompt += "Millis to tomorrow set time = " + diff;
}

textPrompt.setText(stringPrompt);
}};

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/startSetDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Target Time"/>
<TextView
android:id="@+id/prompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>


Related:
- java.util.Calendar


thumbnail
Title: Get the difference between two Calendar
Rating: 100% based on 99998 ratings. 10 user reviews.
Post by

Related articles :

0 comments:

Post a Comment

My Blog List

 
Copyright © 2015. About - Web Version - Mobile Version Sitemap - Contact - Privacy