I have the following situation:
Calendar calendar = Calendar.getInstance();
Date currenthour= calendar.getTime();
if (currenthour.after(timePicker.getTime()) && currenthour.before(timePicker2.getTime())){
//DO SOMETHING
}
It seems that timePicker.getTime() does not exist. Can a getTime() function can
be created for timePicker? Or is there a simpler method to get this to work?
Try it this way... Be carefull to check also seconds and miliseconds...
Avoid converting time to String!!
Calendar calendar = Calendar.getInstance();
Date currenthour= calendar.getTime();
Calendar calendarTmp = Calendar.getInstance();
calendarTmp.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendarTmp.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendarTmp.set(Calendar.SECOND, 0);
if (currenthour.after(calendarTmp.getTime()) && currenthour.before(calendarTmp.getTime())){
//DO SOMETHING
}
In TimePicker you can get this hour and minute to Integer variable.
Like
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
Related
I'm trying to count the days between two dates but I can't get a right result.
I did the same that someone described to me.
My result should be an int or long. For this example I would expext 11 but 10 is also fine.
That's the code:
String startDate = "2018-03-25";
String endDate = "2018-04-05";
Date startDate1 = stringToDate(startDate);
Date endDate1 = stringToDate(endDate);
long ab = daysBetween(startDate1, endDate1);
String ab1 = String.valueOf(ab);
And that's the methods:
public static long daysBetween(Date startDate, Date endDate) {
Calendar sDate = getDatePart(startDate);
Calendar eDate = getDatePart(endDate);
long daysBetween = 0;
while (sDate.before(eDate)) {
sDate.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
return daysBetween;
}
public Date stringToDate(String stringDatum) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(stringDatum);
return date;
}
catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static Calendar getDatePart(Date date){
Calendar cal = Calendar.getInstance(); // get calendar instance
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0); // set hour to midnight
cal.set(Calendar.MINUTE, 0); // set minute in hour
cal.set(Calendar.SECOND, 0); // set second in minute
cal.set(Calendar.MILLISECOND, 0); // set millisecond in second
return cal; // return the date part
}
java.util.Date, Calendar and SimpleDateFormat are part of a terrible API. They make the job of date/time handling harder than it already is.
Make yourself a favor and use a decent date/time library: https://github.com/JakeWharton/ThreeTenABP - here's a nice tutorial on how to use it - How to use ThreeTenABP in Android Project
With this API, it's so easy to do what you want:
LocalDate startDate = LocalDate.parse("2018-03-25");
LocalDate endDate = LocalDate.parse("2018-04-05");
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate); // 11
I've chosen to use LocalDate based on your code: the inputs have only day, month and year, and you're setting the hour/minute/seconds to zero, so I understand that you don't care about the time of the day to calculate the difference - which makes LocalDate the best choice.
Date and Calendar represent a specific point in time, and Calendar also uses a timezone, so Daylight Saving changes might affect the results, depending on the device's default timezone. Using a LocalDate avoids this problem, because this class doesn't have a timezone.
But anyway, I've tested your code and also got 11 as result, so it's not clear what problems you're facing.
private static long daysBetween(Date date1, Date date2){
return (date2.getTime() - date1.getTime()) / (60*60*24*1000);
}
I've a TimePicker to get time. And I've following Java code for an Android Application :
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Log.e("Alarm", "" + calendar.getTime());
calendar.set(Calendar.HOUR, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());
The second process gives a day ahead than first process.
I've tried following code and subtracted the one day.
calendar.add(Calendar.DATE, -1);
Is this the only way or best way? How to get the today's date / time using the TimePicker?
Check the following code.....may be you get help from this
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(R.id.tvTime);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
tvDisplayTime.setText(
new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
// set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}
OK, I got the solution. Thank you everyone! :)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());
You should add: calendar.getInstance(); instead calendar.add(Calendar.DATE, -1);
My code:
Date today = new Date();
final Calendar c = Calendar.getInstance();
preventa.setText(VarGlobales.formatofecha(today));
Time Picker gives the GMT time, you should also take TimeZone offset into account.
I am trying to convert my time in minutes to HH:MM. For example 418minutes = 6:58. I am using following code:
long milli = PriemCas*60000;
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(milli);
int hours3 = calendar1.get(Calendar.HOUR_OF_DAY);
int minutes3 = calendar1.get(Calendar.MINUTE);
System.out.println(hours3+":"+minutes3);
I get 7:58 when my variable PriemCas = 418 instead of 6:58. What could be wrong here? Thank you very much.
Create your Calendar with correct time zone:
Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
I have some date and I want to get last x day before this date so this is my code:
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE, -x);
date = new Date(today.getTimeInMillis()))
this code works only if some day is actual day. How I can change it. Is there some method to get calendar from date ?
Use the setTime method to set the date of your calendar :
Calendar aDay = Calendar.getInstance();
aDay.setTime(aDate);
I'm facing some problems in comparing the current date and the date which is retrieved from Database.I just retrieved date from DataBase and Stored in a Date variable like this
String due_date_task = cursor.getString(cursor.getColumnIndex(dueDateOfTask));
SimpleDateFormat currentFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = currentFormater.parse(due_date_task);
Now,what i want is should check whether date which is retrieved from DataBase is Equivalent to CurrentDate or not.
Calendar currentDate = Calendar.getInstance();
Date date2 = currentDate.getTime();
if(date1.equals(date2))
System.out.println("Today Task");
i just want to check like this.Thanks in advance
For exact match including milliseconds, use getTime:
if(date.getTime() == date1.getTime()){
//do something
}
You can use this function:
private boolean compareDates(Calendar objCal1, Calendar objCal2) {
return ((objCal1.get(Calendar.YEAR) == objCal2.get(Calendar.YEAR))
&& (objCal1.get(Calendar.DAY_OF_YEAR) == objCal2.get(Calendar.DAY_OF_YEAR)));
}
creating the calendar objects:
Calendar objCal1 = new GregorianCalendar().setTime(date);
Calendar objCal2 = new GregorianCalendar().setTime(date1);
Try this way to get the current date,
Calendar calCurr = Calendar.getInstance();
Log.i("Time in mili of Current - Normal", ""+calCurr.getTimeInMillis()); // see what it gives? dont know why?
Date date = new Date();
calCurr.set(date.getYear()+1900, date.getMonth()+1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());// so added one month to it
Log.i("Time in mili of Current - after update", ""+calCurr.getTimeInMillis()); // now get correct
now create Calendar object for database value,
String due_date_task = cursor.getString(cursor.getColumnIndex(dueDateOfTask));
SimpleDateFormat currentFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date = currentFormater.parse(due_date_task);
Calendar start = Calendar.getInstance();
start.set(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
and now Compare both the Calendar objects
if(calCurr.equals(start))