Date Picker AlertDialog Box: (XML file)
In JAVA:
public void alertDatePicker() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.date_picker, null, false);
int dialogTheme = R.style.DialogTheme;
final DatePicker myDatePicker = (DatePicker) view.findViewById(R.id.myDatePicker);
myDatePicker.setMaxDate(System.currentTimeMillis()); //For disabling Future Dates
myDatePicker.setCalendarViewShown(true);
new AlertDialog.Builder(context, dialogTheme).setView(view)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@TargetApi(11)
public void onClick(DialogInterface dialog, int id) {
int month = myDatePicker.getMonth() + 1;
int day = myDatePicker.getDayOfMonth();
int year = myDatePicker.getYear();
String fromDate = year + "-" + month + "-" + day;
dialog.cancel();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
date_picker.xml
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myDatePicker"
style="@style/LinearLayout_mm"
android:maxDate=""
android:scaleX="1"
android:scaleY="1"
android:theme="@style/DialogTheme" />
style.xml:
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/blue</item>
<item name="android:alertDialogTheme">@color/blue</item>
</style>
====================
To get DEVICE TIME:
Calendar ci = Calendar.getInstance();
DEVICE_TIME = "" + ci.get(Calendar.YEAR) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.DAY_OF_MONTH) + " " +
ci.get(Calendar.HOUR_OF_DAY) + ":" +
ci.get(Calendar.MINUTE) + ":" +
ci.get(Calendar.SECOND);
===================
Getting CurrentDate:
private SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
public String getCurrentDate() {
int yr, month, day, hour
Calendar today = Calendar.getInstance();
yr = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);
hour = today.get(Calendar.HOUR_OF_DAY);
return (yr + "-" + getLength(month + 1) + "-" + getLength(day));
}
public static String getLength(int i) {
if (i > 9)
return "" + i;
return "0" + i;
}
=
===================
Comparing CurrentDate & FromDate:
String fromDate = year + "-" + month + "-" + day;
public boolean fromDateCheck(String fromDate) {
try {
String toDate = pref.getString("TO_DATE", getCurrentDate());
Date toDATE = dfDate.parse(toDate);
Date fromDATE = dfDate.parse(fromDate);
if (fromDATE.before(toDATE) || fromDATE.compareTo(toDATE) == 0) {
SharedPreferences.Editor editor = pref.edit();
editor.putString("FROM_DATE", fromDate);
editor.putString("TO_DATE", toDate);
editor.putBoolean("DATE_CHANGED", true);
editor.commit();
return true;
} else if (fromDATE.after(toDATE)) {
Toast.makeText(this.context, "Invalid Date",Toast.LENGTH_SHORT).show();
return false;
} else {
Toast.makeText(this.context, "Choose Correct Data", Toast.LENGTH_SHORT).show();
return false;
}
} catch (Exception e) {
}
return false;
}
==========================
Convert Time to another Format:
Date SD;
Date DD;
String DdOutput;
String sdOutput;
getconvertDEVICE_date(DEVICE_TIME);
private String getconvertDEVICE_date(String date) {
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);
try {
DD = inputFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
DdOutput = outputFormat.format(DD);
return DdOutput;
}
==============================
Comments
Post a Comment