Calendar get date and check difference two date [duplicate] - java

This question already has answers here:
How to get list of dates from some date? [duplicate]
(3 answers)
Closed 4 years ago.
I used the Calendar.getInstance().get(Calendar.DATE) to get current date. So the output is today (2018/05/14).
I change the device time to 2018/06/17. So I use the Calendar get the date. I got the 17.
My question is I want to get 05/15 05/16 .... 06/01 ... 0616 these two days. I need to know all the difference days.
So I don't know which function can do this.

private static long daysBetween(Date one, Date two) {
long difference = (one.getTime()-two.getTime())/86400000;
return Math.abs(difference);
}

Related

i want to get difference of days by subtracting dates in java using Gregorian calendar: [duplicate]

This question already has answers here:
Calculating difference in dates in Java
(7 answers)
Closed 6 years ago.
GregorianCalendar last = new GregorianCalendar(py,pm-1,pd);
System.out.println(last.getTime());
GregorianCalendar present = new GregorianCalendar(py,pm-1,pd);
System.out.println(present.getTime());
long diff = Math.abs(prsent.getTime() - last.getTime());
Problem facing when subtracting these two dates
ChronoUnit is what you can use for the difference between dates:
long minutesDiff = ChronoUnit.MINUTES.between(present.toInstant(), last.toInstant());

Java - get Monday date of a week give a date in that week [duplicate]

This question already has answers here:
How in Java find dates for previous 2 mondays?
(3 answers)
Closed 6 years ago.
I got a date YYYY-MM-DD how can I get the Monday date of that date whatever the given date is?
Edited: got the answer from How in Java find dates for previous 2 mondays?.
LocalDate monday =
localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY);

Subtracting Dates using Java Calendar [duplicate]

This question already has answers here:
How do I calculate someone's age in Java?
(28 answers)
Closed 6 years ago.
I have 2 Calendar objects for the current date and birthdate.
I need to subtract birthdate from the current date and return the difference in years (a truncated Int is all I need).
What's the simplest way of doing this?
You can get EPOCH time (miliseconds since 01.01.1900), subtract both values and divide it by 1000(miliseconds)/60(seconds)/60(minutes)/24(hours)/365(days).
This would give you approximate result (in years) because of ignoring leap years.
To get epoch time, use Date#getTime();

How to get current time+n months in java? [duplicate]

This question already has answers here:
How do I add one month to current date in Java?
(18 answers)
Closed 7 years ago.
I have searched a lot on internet about how to get current time in Java but what I am looking for is how to get current time + n months?
You can try like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, numberofMonths);
If you can use Java 8, use LocalDate.plusMonths()
LocalDate localDate = LocalDate.now();
localDate.plusMonths(numberOfMonths);

Difference between two dates in java using joda API [duplicate]

This question already has answers here:
Calculate month difference in Joda Time
(3 answers)
Closed 8 years ago.
I have two dates date1 and date2, i need to get number of months between those two dates,
can any one help me to do.
int result = Months.monthsBetween(date1, date2).getValue();

Categories

Resources