This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 6 years ago.
birthDate's type is java.util.Date .
What is the best way to calculate the current age?
I want a java.util.Date variable storing TODAY - birthDate.
That depends on what do you mean by current age.
If you mean the precise astronomical meaning, use
DateTime startDate = new DateTime(2000, 1, 19, 5, 14, 0, 0);
DateTime endDate = new DateTime();
Days d = Days.daysBetween(startDate, endDate);
If you need the common sense difference, based on both dates given in whole days:
long thisDay = (new Date()).getTime() / (1000 * 60 * 60 * 24);
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date birthDate = formatter.parse("11-June-07");
long birthdayInDays = birthDate.getTime() / (1000 * 60 * 60 * 24);
long dayDiff = thisDay - birthdayInDays;
Related
This question already has answers here:
How to subtract X days from a date using Java calendar?
(11 answers)
Closed 6 years ago.
I want to subtract days from date in java. But I dont want to use external libraries. I have referred some of questions from stackoverflow but they are suggesting to use external libraries. So I have applied following logic
noOfDays = 24;
Date compareDate = new Date(currentDate - noOfDays * 24 * 60 * 60 * 1000);
System.out.println("compare date " + compareDate);
It is working fine till 24 days.But after 24 days it is giving unexpected result. Is there any solution to this ?
Use java.util.Calendar.
Something like that:
Calendar c = new Calendar()
c.setTime(currentDate);
c.add(Calendar.DAY_OF_MONTH, noOfDays)
compareDate = c.getTime()
You can use a LocalDate (which is part of the JDK since Java 8):
LocalDate today = LocalDate.now();
LocalDate compareDate = today.minusDays(24);
You computation is about integers, which won't fit higher values than the max integer value.
Declare your variable as a long :
long noOfDays = 24;
Date compareDate = new Date(currentDate - noOfDays * 24 * 60 * 60 * 1000);
System.out.println("compare date " + compareDate);
However, as the comments said, this is not the best approach for substracting days, so have a look at better solutions in other answers.
This question already has answers here:
How do I increment a java.sql.Timestamp by 14 days?
(5 answers)
Closed 8 years ago.
How to add 3 hours to the time that store in java.sql.Timestamp by not using the deprecated API?
I use the below code, but it doesn't work.
Timestamp later = new Timestamp(old + (1000 * 60 * 60 * 3));
Assuming old is a Timestamp; your code is close. You just need to convert the old timestamp to a millisecond value first. Do:
Timestamp later = new Timestamp(old.getTime() + (1000 * 60 * 60 * 3));
Neither getTime() nor the Timestamp(long) constructor are deprecated.
Note that all of this information is readily available in the Timestamp documentation.
You can use Calendar for date manipulation:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.HOUR, 3);
Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
I am using JodaTime to compare the dates
DateTime past = new DateTime(quitYear, quitMonth, quitDay, quitTimeHour, quitTimeMin);
DateTime today = new DateTime();
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays();
This gives me the number of days difference. How do i display the time difference aswell. For example it shows that the difference is 5:23(5 hours and 23 minutes).
I don't think you can get that from one query, you must do more operations.
Probably you can do this
Duration duration = new Interval(past, today).toDuration();
int days = duration.toStandardDays().getDays();
int hours = duration.toStandardHours().getHours() - days * 24;
int minutes = duration.toStandardMinutes().getMinutes() - days * 24 * 60 - hours * 60;
This question already has answers here:
How to subtract X days from a date using Java calendar?
(11 answers)
Closed 9 years ago.
long epoch = System.currentTimeMillis() / 1000;
String dateStr = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.0Z'").format(new java.util.Date(epoch * 1000));
System.out.println(dateStr);
Can anyone please tell me how to get the time stamp of 4 weeks from the current one? I'm working on payroll testing.
Subtract the number of milliseconds in 4 weeks from the current time.
long now = System.currentTimeMillis();
long fourWeeksAgo = now - 1000 * 60 * 60 * 24 * 28;
java.util.Calendar provides a means of manipulating dates so that code is readable:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.WEEK_OF_YEAR, -4);
long fourWeeksAgo = calendar.getTime().getTime();
java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.WEEK_OF_MONTH, -4);
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How can I calculate the age of a person in year, month, days?
How can I calculate the difference between two dates
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
if(petDetails.getDateOfDeath() != null){
String formatedDateOfDeath = formatter.format(petDetails.getDateOfDeath());
String formateDateOfBirth = formatter.format(petDetails.getDateOfBirth());
}
How can i calculate the age of death from the above. I dont want to use any externallibraries
EDIT: please look at what I've got so far.none of the other threads are like mine. most of them are about date from DOB to today and not in the format im using.
Try this:
public class Age {
public static void main(String[] args) {
Calendar birthDate = new GregorianCalendar(1979, 1, 1);
Calendar deathDate = new GregorianCalendar(2011, 1, 1);
int age = deathDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
if ((birthDate.get(Calendar.MONTH) > deathDate.get(Calendar.MONTH))
|| (birthDate.get(Calendar.MONTH) == deathDate.get(Calendar.MONTH) && birthDate.get(Calendar.DAY_OF_MONTH) > deathDate
.get(Calendar.DAY_OF_MONTH))) {
age--;
}
System.out.println(age);
}
}
You can solve this without converting them to strings.
since the getDateOfBirth and getDateOfDeath return date objects, you can use the .getTime() method on them which Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
A fairly simple way of doing this could be
long millisecondsDiff = petDetails.getDateOfDeath().getTime - petDetails.getDateOfBirth().getTime;
You can then either create a new date object directly from this long, or you can do the proper calculations to change milliseconds into days. ie
long age = millisecondsDiff / (1000 * 60* 60 * 24);