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);
Related
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);
}
This question already has answers here:
Get yesterday's date using Date [duplicate]
(8 answers)
Closed 4 years ago.
I want to select the previous day in a calendar. For example, if I have 02-28-2018 I need to set 27 automatically.
I have tried the below code but it fails when the date is 1st.
String currentDate = new SimpleDateFormat("dd").format(new Date());
int previousDay = Integer.parseInt(currentDate) - 1;
There is an easy way to do this in Java 8
int previousDay = LocalDate.now().minusDays(1).getDayOfMonth();
For LocalDate Reference see here
Hope this Helps!
If using Java7 you can use Calendar
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -1);
System.out.println(new SimpleDateFormat("dd").format(cal.getTime()));
But for Java8 see http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
Also a back-port for Java 6 & Java 7: ThreeTen-Backport.
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());
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);
This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to subtract X day from a Date object in Java?
(10 answers)
Closed 9 years ago.
I have an input field, where i can select the date from date picker.Based on this selected value i need to get the 30 days back date.
Thanks in advance.
Calendar cal = Calendar.getInstance();
cale.add(Calendar.DATE, -30);
System.out.println("Date = " + cal.getTime());