I have tried it with EditText which will receive value of datepicker and timepicker and will display the birthdate and birth time but now I don't know how to calculate age from the date and time.
First get today date
Date today = new Date();
Get difference in time
long timeDiff = today.getTime() - DOB.getTime();
Now you can convert time to DATE, MONTH, or YEAR
int numOfDays = (int) (timeDiff / (1000 * 60 * 60 * 24));
double years = 0.00273973 * numOfDays;
Related
In my Android app. I am running some Task every 2 hours.How can i check that the time has exceeded 2 hours.
I tried to use this but it says depreceated
Date date = new Date();
date.setHours(date.getHours() + 2);
I would appreciate the insight on how to implement this?
Check this link
http://www.dotnetexpertsforum.com/comparing-date-time-values-in-android-t1567.html
Calendar current_time = Calendar.getInstance ();
current_time.add(Calendar.YEAR, 0);
current_time.add(Calendar.DAY_OF_YEAR, 0);
current_time.set(Calendar.HOUR_OF_DAY,
//Subtract 2 hours
current_time.get(Calendar.HOUR_OF_DAY)-2);
current_time.set(Calendar.MINUTE, 0);
current_time.set(Calendar.SECOND, 0);
Calendar given_time = Calendar.getInstance ();
given_time.set(Calendar.YEAR, syear);
//Give the day sDay and hour shour
given_time.set(Calendar.DAY_OF_YEAR, sday);
given_time.set(Calendar.HOUR_OF_DAY, shour);
given_time.set(Calendar.MINUTE, 0 );
given_time.set(Calendar.SECOND, 0);
Date current_calendar = current_time.getTime();
Date given_calendar = given_time.getTime();
System.out.println("Current Calendar "+ current_calendar);
System.out.println("Given Calendar "+ given_calendar);
boolean v = current_calendar.after(given_calendar);
if(v){
return true;
}
You should use the Calendar class.
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)+2);
calendar.getTime();//your date +2 hours
use Calendar class :
Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.HOUR)>(cal.get(Calendar.HOUR)+2));
Date is deprecated. Use Calendar instead.
Source: Java: Why is the Date constructor deprecated, and what do I use instead?
Two hours is 2 * 60 minutes, is 2 * 60 * 60 seconds and is 2 * 60 * 60 * 1000 milliseconds. So you can just add this number of milliseconds to your date:
Date date = new Date ();
Date after2Hours = new Date (date.getTime () + 2L * 60L * 60L * 1000L);
If you have to repeat that task every two hours, you should use Alarm manager service for that. See the link below for more details.
http://www.javacodegeeks.com/2012/09/android-alarmmanager-tutorial.html
I have an application which use the current date and a date that is chose by user using date picker as follow:
If the date that the user chose is more than the current date +280 day,
some code will be executed.
If the date that the user chose is less than the current date , some
code will be executed.
I used this code to do so ..
Calendar start2 = Calendar.getInstance();
int birthYear = birthDayDatePicker.getYear();
int birthMonth = birthDayDatePicker.getMonth();
int birthDay = birthDayDatePicker.getDayOfMonth();
start2.set(birthYear, birthMonth, birthDay);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(birthDate);
cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
birthDayChosenCalender.set(birthYear,birthMonth,birthDay);
MaxBirthDayCalender.set(currentYear, currentMonth, currentDay);
long diff = birthDayChosenCalender.getTimeInMillis() - MaxBirthDayCalender.getTimeInMillis(); //result in millis
long daysBetween = diff / (24 * 60 * 60 * 1000);
System.out.println("Days between ,,,,,,,,,,,,,,,,,,"+daysBetween);
if(MaxBirthDayCalender.before(birthDayChosenCalender) && daysBetween <= 280){
do sth }
Is there any other clean way to do that ! because this way is not working well !
The other clean way to do it is to use the Joda Time library.
Other than that, everything can be done using millis and a single calendar instance:
Calendar pickedDate = new GregorianCalendar(
picker.getYear(),
picker.getMonth(),
picker.getDayOfMonth());
long pickedTime = pickedDate.getTimeInMillis();
long now = new Date().getTime();
if (pickedTime - now <= (280 * 24 * 60 * 60 * 1000)) { // 280 days in milliseconds
// ...
}
Should cover the requirement:
I have an application which use the current date and a date that is chose by user using date picker as follow:
If the date that the user chose is more than the current date +280 day, some code will be executed.
If the date that the user chose is less than the current date , some code will be executed.
I am Using GWT.
I need to retrieve the current date and and last week date. and pass it to GWT server using RPC.
How to retrieve the system date and last week date.??
You will Get Date/Time in/with GWT.
get a unix time stamp since the epoch
get year, month, today, date, hours, minutes seconds
//Get the browsers date (!!!note: I can't get GMT time zone in eclipse debugger)
Date date = new Date();
int Month = date.getMonth();
int Day = date.getDate();
int Year = date.getYear();
int Hour = date.getHours();
int min = date.getMinutes();
int sec = date.getSeconds();
int tz = date.getTimezoneOffset();
int UnixTimeStamp = (int) (date.getTime() * .001);//get unix time stamp example (seconds)
Long lTimeStamp = date.getTime(); //time in milleseconds since the epoch
int iTimeStamp = (int) (lTimeStamp * .001); //(Cast) to Int from Long, Seconds since epoch
String sTimeStamp = Integer.toString(iTimeStamp); //seconds to string
//get the gmt date - will show tz offset in string in browser, not eclipse debug window
String TheDate = date.toString();
//render date to root panel in gwt
Label label = new Label(TheDate);
RootPanel.get().add(label);
****** other wise Visit following link to get more information
1)a GWTInfo
2)one more a Stack
I hope it will help.
I make it using calculation by stander date util
you can find the date as below
Date fromday = new Date(System.currentTimeMillis() - 6000L * 60L * 60L * 24L);
Date today = new Date(System.currentTimeMillis());
Today will get current day, fromDay will get past 6 day
This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 9 years ago.
I want to calculate the difference between two dates.
Currently, I am doing:
Calendar firstDate = Calendar.getInstance();
firstDate.set(Calendar.DATE, 15);
firstDate.set(Calendar.MONTH, 4);
firstDate.get(Calendar.YEAR);
int diff = (new Date().getTime - firstDate.getTime)/(1000 * 60 * 60 * 24)
This gives me output 0. But I want that I should get the output 0 when the new Date() is 15. Currently the new date is 14. It makes my further calculation wrong and I am confused how to resolve this. Please suggest.
Finding the difference between two dates isn't as straightforward as
subtracting the two dates and dividing the result by (24 * 60 * 60 *
1000). Infact, its erroneous!
/* Using Calendar - THE CORRECT (& Faster) WAY**/
//assert: startDate must be before endDate
public static long daysBetween(final Calendar startDate, final Calendar endDate) {
int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
long endInstant = endDate.getTimeInMillis();
int presumedDays = (int) ((endInstant - startDate.getTimeInMillis()) / MILLIS_IN_DAY);
Calendar cursor = (Calendar) startDate.clone();
cursor.add(Calendar.DAY_OF_YEAR, presumedDays);
long instant = cursor.getTimeInMillis();
if (instant == endInstant)
return presumedDays;
final int step = instant < endInstant ? 1 : -1;
do {
cursor.add(Calendar.DAY_OF_MONTH, step);
presumedDays += step;
} while (cursor.getTimeInMillis() != endInstant);
return presumedDays;
}
You can read more on this here.
I don't think that by creating a new Date() will give you the current time and date instead do this:
Calendar cal = Calendar.getInstance();
Date currentDate = cal.getTime();
Date firstDate = new Date();
firstDate.setHour(...);
firstDate.setMinute(...);
firstDate.setSeconds(...);
long dif = currentDate.getTime() - firstDate.getTime();
So as you can see you can be as straightforward as subtracting one from another...
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I calculate someone's age in Java?
I have two dates eg 19/03/1950 and 18/04/2011. how can i calculate the difference between them to get the person's age? do I have to keep multiplying to get the hours or seconds etc?
String date1 = "26/02/2011";
String date2 = "27/02/2011";
String format = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = sdf.parse(date1);
Date dateObj2 = sdf.parse(date2);
long diff = dateObj2.getTime() - dateObj1.getTime();
int diffDays = (int) (diff / (24* 1000 * 60 * 60));
You use the classes Date and Duration:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Date.html
http://download.oracle.com/javase/1.5.0/docs/api/javax/xml/datatype/Duration.html
You create Date-objects, then use Duration's methods addTo() and subtract()
The following code will give you difference between two dates:
import java.util.Date;
import java.util.GregorianCalendar;
public class DateDiff {
public static void main(String[] av) {
/** The date at the end of the last century */
Date d1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();
/** Today's date */
Date today = new Date();
// Get msec from each, and subtract.
long diff = today.getTime() - d1.getTime();
System.out.println("The 21st century (up to " + today + ") is "
+ (diff / (1000 * 60 * 60 * 24)) + " days old.");
}
}
Why not use jodatime? It's much easier to calculate date and time in java.
You can get the year and use the method yearsBetween()