How to compare Date objects ignoring seconds and milliseconds? [duplicate] - java

This question already has answers here:
Set time to 00:00:00
(14 answers)
Closed 6 years ago.
I am trying to compare Date objects in Java but when I test it, it fails because (I think) of seconds and milliseconds.
Date date = (Date) jSpinner.getValue();
Date now = Calendar.getInstance().getTime();
if(date.before(now))
System.out.println("Error: Date too early");
else
System.out.println("Date is good");
So when I pick from the JSpinner today's date and time (up to minutes) the comparison with now should print the second statement but prints the first one. So how do you tell the date object to set seconds and milliseconds to 0?

You should be able to use Calendar API to zero out the seconds and milliseconds to zero.
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,%20int)

Related

How to calculate Date days difference, and not by seconds? [duplicate]

This question already has answers here:
getting the difference between date in days in java [duplicate]
(3 answers)
Java, Calculate the number of days between two dates [duplicate]
(10 answers)
Java 8 Day Difference without the Time Component
(1 answer)
Remaining days to a date is not showing correctly
(4 answers)
Closed 4 years ago.
For example i have two dates:
2018-11-30 18:00:00
2018-12-01 00:00:00
How you can see, that less then 24 hours difference, but I need to recognize that a one day of differnce, and i can't just subtract, cause of month change.
In output i need to return int count of days.
In c# it will be just:
(EndDate - StartDate).TotalDays
What Java code will be similar?
I searched somewhere before I asked, but that didn’t solve the problem. Here’s my attempt after searching:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime d1 = LocalDateTime.parse("2018-11-30 18:00:00", dtf);
LocalDateTime d2 = LocalDateTime.parse("2018-12-01 00:00:00", dtf);
long days = ChronoUnit.DAYS.between(d1, d2);
System.out.println("Days: " + days); // Days: 0
I wish to get 1 day of difference.

Java turning calculated milliseconds of time left into human readable time (years, months, days, etc...) [duplicate]

This question already has answers here:
Java format hour and min
(4 answers)
Convert number of seconds into HH:MM (without seconds) in Java
(3 answers)
Calculating the difference between two Java date instances
(45 answers)
How to format a duration in java? (e.g format H:MM:SS)
(22 answers)
Closed 4 years ago.
I'm trying to countdown to a time in the future (2024) and I have the time left from the users system to the exact date in 2024.
String input = "Mon Apr 08 2024 18:18:29 UTC";
Date date = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH).parse(input);
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (new Date()).getTime();
That is my code, im trying to turn millisecondsFromNowinto a date a human can read, more specifically, years, months, days, hours, minutes, seconds and milliseconds left until the date.
You can create a Duration object using the ofMillis method. Then have a look into this question for formatting options.

how to add number of days into a given date in the format mm/dd/yyyy? [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
How to add number of days into a given date in the format mm/dd/yyyy .
If my date is 9/12/2007, I want to add 30 days into the date and the result should be 10/12/2007.
I have many frequencies like Weekly, monthly, Every 2 weeks, Twice a month, Every 4 weeks, Once in 2 months, Every 3 months, Every 6 months, Every 3 months,
Annually, etc.
If we select the different frequencies from the list, the result should vary based on the frequency. Can anyone help me on this ?
Convert your date to a LocalDate, add the required values to it and then convert it back to the format you need it.
For example adding 30 days would look like this:
LocalDate d = LocalDate.of(2007,9,12).plus(30, ChronoUnit.DAYS)
And if you look at ChronoUnit you can see there are some units defined like weeks, days, months and so on...
String dt = "9/12/2007"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);

How to check if the timestamp is current date's timestamp [duplicate]

This question already has answers here:
How to compare two Dates without the time portion?
(33 answers)
Closed 9 years ago.
I have a scheduler that needs to check if the incoming timestamp is current day's timestamp.
The incoming timestamp will be of the format Eg:1384956395.
How to check this in java? Please help. I am not using Joda
The epoch you posted is in seconds. Java uses milliseconds so you have to convert it and then compare the two.
long epochInMillis = epoch * 1000;
Calendar now = Calendar.getInstance();
Calendar timeToCheck = Calendar.getInstance();
timeToCheck.setTimeInMillis(epochInMillis);
if(now.get(Calendar.YEAR) == timeToCheck.get(Calendar.YEAR)) {
if(now.get(Calendar.DAY_OF_YEAR) == timeToCheck.get(Calendar.DAY_OF_YEAR)) {
}
}
You can also change the time zone if you do not want to use the default, in case the input epoch is in a different time zone.
Assuming that your timestamp was created via System.currentTimeMillis() (or any other compatible mechanism), you can do the following:
Create a Calendar instances and set the hour, minute, second and millisecond fields to zero. This is today at 0:00:00,0.
Clone the instance and add 1 day. You'll get tomorrow at 0:00:00,0.
Now check if your timestamp is in the range between today.getTime() (inclusive) and tomorrow.getTime() (exclusive).

get date difference in java, use of getDate() [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
getting the difference between date in days in java [duplicate]
(3 answers)
Closed 10 years ago.
I am co-working with a group, I want to ask how can i get the date difference from a formate of "Sat Feb 23 00:00:00 GMT 2013". The pickerfrom and to is a calendar, and getDate returns that formate. How can I get the date difference in days? any idea?
/* Current format Sat Feb 23 00:00:00 GMT 2013 */
Date date_from = pickerFrom.getDate();
Date date_to = pickerTo.getDate();
int date_diff = (int)((date_to)-(date_from));
Checkout getting the difference between date in days in java
My preference would be to use Joda time - it has many useful date functions that'll make your life much easier when it comes to dates and date manipulation
You can get the difference in milliseconds of each date and subtract these values.
long diff = date_to.getTime() - date_from.getTime();
will return you the number of milliseconds between the two dates.
Then, you can use something like this to get the number of hours, days,... out of these milliseconds.

Categories