Presently i want to display last one month records based on current date. So i need last month,last date & last year. In between months i need 30 days. For Ex: current date is 12/12/2017 (dd/mm/yyyy) then expected date is 13/11/2017 EX: current date is 12/03/2017 then expected date is 11/02/2017
Asked
Active
Viewed 4,717 times
-3
-
mainly i want date – Bindhu Yanamadala Dec 12 '17 at 14:11
-
Tip: Assume all the basic date-time questions have been asked and answered. Search *Stack Overflow* thoroughly before posting. – Basil Bourque Dec 16 '17 at 00:57
2 Answers
0
You can simply do it using calendar.
Calendar c=Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_YEAR, -30);
System.out.println(c.getTime());

Uta Alexandru
- 324
- 4
- 11
-
1
-
-
he said he need 30 days... between months if i see it clear.. so basically he want exactly 30 days difference but his way to say it was unclear. – Uta Alexandru Dec 12 '17 at 14:23
-
For Ex: SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String dateInString = "12-03-2017 10:20:56"; Date date = sdf.parse(dateInString); Calendar c=Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_YEAR, -30); System.out.println(c.getTime()); ACTUAL OUT: Fri Feb 10 10:20:56 IST 2017 here in between input date and output date i am getting 31 days . But i need 30 days only – Bindhu Yanamadala Dec 12 '17 at 14:29
-
-
You re welcome. Aniway you need to clarify what happens when date is 1 March.. There u could have a glitch..You can do some clarification on your question.. My code will always give 30 days back (but in that case u will go 2 months until 31 January ) .I don't know if that is what u wanna happen. – Uta Alexandru Dec 12 '17 at 14:42
-
FYI, the troublesome old date-time classes such as [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html) are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). `LocalDate.now().minusMonths(1)` – Basil Bourque Dec 16 '17 at 00:58
0
This should solve your problem.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
cal.add(Calendar.DATE, -1);

Rohit
- 146
- 1
- 5
-
FYI, the troublesome old date-time classes such as [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html) are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). `LocalDate.now().minusMonths(1)` – Basil Bourque Dec 16 '17 at 00:58
-
I agree with Basil. But when you want your code to be executed on client environment where java version can differ. Not all people jave upgraded their machines to latest java version. That's where this classes will be still in use. – Rohit Dec 16 '17 at 06:13
-
For Java 6 & Java 7, use the *ThreeTen-Backport* library in your app. The legacy date-time classes really are a wretched mess, and should be avoided. – Basil Bourque Dec 16 '17 at 06:59