Java tutorial
//package com.java2s; //License from project: Open Source License import android.util.Log; import java.util.Calendar; import java.util.Date; public class Main { public static Date getDateTimeFrom(int hour, int minute) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 0); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.YEAR, 0); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date ret = calendar.getTime(); return ret; } public static Date getDateTimeFrom(int year, int monthOfYear, int dayOfMonth) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); calendar.set(Calendar.MONTH, monthOfYear - 1); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date ret = calendar.getTime(); return ret; } public static Date getDateTimeFrom(Date time, Date date) { Date iTime, iDate; Calendar calendar = Calendar.getInstance(); if (date == null) { iTime = time; iDate = new Date(); } else { iTime = time; iDate = date; } Calendar calendarDate = Calendar.getInstance(); calendarDate.setTime(iDate); Calendar calendarTime = Calendar.getInstance(); if (iTime != null) calendarTime.setTime(iTime); if (iTime != null) { calendar.set(Calendar.MINUTE, calendarTime.get(Calendar.MINUTE)); calendar.set(Calendar.HOUR_OF_DAY, calendarTime.get(Calendar.HOUR_OF_DAY)); } else { calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); } //calendar.set(Calendar.AM_PM, calendarTime.get(Calendar.AM_PM) ); calendar.set(Calendar.DAY_OF_MONTH, calendarDate.get(Calendar.DAY_OF_MONTH)); calendar.set(Calendar.MONTH, calendarDate.get(Calendar.MONTH)); calendar.set(Calendar.YEAR, calendarDate.get(Calendar.YEAR)); Date ret = calendar.getTime(); Log.v("!!!!!!!!!! calendar=", "" + ret); return ret; } public static Date getDateTimeFrom(Date iDate, int mode, int adddate) { Calendar calendar = Calendar.getInstance(); Calendar calendarDate = Calendar.getInstance(); calendarDate.setTime(iDate); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); switch (mode) { case Calendar.DAY_OF_MONTH: { calendar.set(Calendar.DAY_OF_MONTH, calendarDate.get(Calendar.DAY_OF_MONTH) + adddate); } break; } Date ret = calendar.getTime(); Log.v("!!!!!!!!!! calendar=", "" + ret); return ret; } }