how to add time in java.sql.Timestamp [duplicate] - java

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());

Related

Unable subtract days from date in java [duplicate]

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.

Given a birth date, how can I calculate the current age? [duplicate]

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;

Display date and time comparing present date with past date in java

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;

setMaxDate value cannot be set to 18 year back

I am trying to set the maxdate value to 18year before, but I couldn't do it.
setMaxDate(new Date().getTime()-18*365*24*60*60*1000);
I am using the above method and I would like to use only this menthod. Kindly help
18*365*24*60*60*1000 is a number too large to fit in a 32-bit signed integer. Change it to a 64-bit signed long integer: 18*365*24*60*60*1000L (notice the L).
But there are better methods to go 18 years back, e.g.
Calendar c = Calendar.getInstance(); // current date
c.add(Calendar.YEAR, -18);
Date t = c.getTime(); // to convert to Date
If you are passing Date parameter to setMaxDate function then it should be
setMaxDate( new Date( new Date().getTime()- 1L * 18 * 365 * 24 * 60 * 60 * 1000 ) );

Subtract four weeks from current time stamp [duplicate]

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);

Categories