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

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.

Related

How to calculate remaining days of the month? [duplicate]

This question already has answers here:
How to get number of days between today's date and last date of the current month? [closed]
(3 answers)
How to calculate the number of days in a period?
(3 answers)
Closed 2 years ago.
How do I calculate how many days are left in the month we are in, in Java?
For example, today is the 5th of November, the result would be:
25 days left for the end of the month.
How to do this?
In Java there's been for a while Java Time API, which allows to get the number of days in the given month and calculate the difference using java.time.temporal.ChronoUnit.DAYS:
LocalDate today = LocalDate.now();
LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());
long daysBetween = DAYS.between(today, endOfMonth);

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 force java.time.LocalDate to assume 19th century as YY year pattern? [duplicate]

This question already has answers here:
Parsing string to local date doesn't use desired century
(1 answer)
How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?
(1 answer)
Closed 6 years ago.
The following test fails: a '86 birthday is formated as 2068. How can I format is as 1986?
#Test
public void testBirthday() {
assertEquals("1986-08-07", java.time.LocalDate.parse("070886",
java.time.format.DateTimeFormatter.ofPattern("ddMMyy")));
}
Fails with:
java.lang.AssertionError: expected:<1986-08-07> but was:<2086-08-07>
This is much different to org.joda.time library which would correctly assume 19' here.
/Sidenote: regarding the marked answers in the "duplicate" questions, I don't think this is a duplicate!
The base year can be controlled using DateTimeFormatterBuilder.appendValueReduced().
This code would parse with a base date of 1900 rather than 2000:
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendPattern("ddMM")
.appendValueReduced(ChronoField.YEAR, 2, 2, 1900)
.toFormatter();
LocalDate date = LocalDate.parse("070886", f);
Java 8's LocalDate class uses 2000+ for the year by default. In order to parse a yy format for 1900 and up, you'll need to reduce the time yourself.
Of course, even if you check the date against the current year and subtract 100 years if its after today, this fix won't work for people over 100 years old.
You can also use a yyyy format. Or just make use of a Date object instead, and convert it to a LocalDate if you wish.

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

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)

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

Categories