0

I have the current date and I want to get the date which is 1 year back by using only java.util.Date

I'm working in gwt so cannot use SimpleDateFormat or Calendar

Date currentDate = new Date();
Date oneYearBefore = new Date( - (365 * 24 * 60 * 60 * 1000));

The above mentioned code is not working (got it from some forum)

Rakesh
  • 45
  • 2
  • 6
  • That expression will overflow the int datatype. Try using long constants. (e.g. 365L * 24L *60L...). Also you have not taken daylight saving or leap seconds into account. Do they matter for your use cases? – kiwiron Nov 17 '14 at 07:37
  • This will cause you a lot of pain because you also have to check for leap years and the like. – SpaceTrucker Nov 17 '14 at 07:46
  • possible duplicate of [How to do calendar operations in Java GWT? How to add days to a Date?](http://stackoverflow.com/questions/2527845/how-to-do-calendar-operations-in-java-gwt-how-to-add-days-to-a-date) – SpaceTrucker Nov 17 '14 at 07:48

4 Answers4

4

Use Calendar class

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
System.out.println(calendar.getTime());
Michael Shrestha
  • 2,547
  • 19
  • 30
0

Why You cannot use Calendar? I think You might have misunderstood something?

anyways:

Date oneYearBefore = new Date( System.currentTimeMillis() - (365 * 24 * 60 * 60 * 1000));

Or by using Your code pasted from the forum:

Date currentDate = new Date();
Date oneYearBefore = new Date( currentDate.getTime() - (365 * 24 * 60 * 60 * 1000));
maslan
  • 2,078
  • 16
  • 34
  • 1
    A year is not always exactly 365 times 24 hours. – Jesper Nov 17 '14 at 07:41
  • http://img3.wikia.nocookie.net/__cb20090707173543/wykopedia/pl/images/9/9b/200px-CaptainobviousChooseOption.jpg Do You prefer to give us a nice implementation of Gregorian Calendar? In most applications this is sufficient. – maslan Nov 17 '14 at 07:48
  • I guess in gwt I'm not allowed to use Calendar. It says no source code found for java.util.Calendar in runtime. – Rakesh Nov 18 '14 at 09:39
  • How about this Util, maybe it will be usefull (then its just -365 days)? http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/datepicker/client/CalendarUtil.html – maslan Nov 18 '14 at 09:46
0

Using only java.util.Date, you can use:

Date oneYearBefore = new Date(currentDate.getTime() - (365L * 24L * 60L * 60L * 1000L));

But keep in mind, this doesn't account for potential leap years.

badjr
  • 2,166
  • 3
  • 20
  • 31
0

You are using all int's, when you multiply them you get an int. You're converting that int to a long, but only after the int multiplication has already resulted in the wrong answer. Actually it is overflowing the int type

 public static void main(String[] args) {
        Date currentDate = new Date();
        System.out.println(currentDate);
        long milliseconds = (long) 365 * 24 * 60 * 60 * 1000;
        Date oneYearBefore = new Date(currentDate.getTime() - milliseconds);
        System.out.println(oneYearBefore);
    }

Mon Nov 17 13:11:10 IST 2014
Sun Nov 17 13:11:10 IST 2013
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • There is a bug here, it should be : long milliseconds = (long) 365 * 24 * 60 * 60 * 1000; Date oneYearBefore = new Date(currentDate.getTime() - milliseconds); – lazywiz Apr 22 '15 at 19:01