Solved

Determine date 1 month ago

Posted on 2001-07-13
7
1,193 Views
Last Modified: 2007-12-19
I am using the following code to get the current date and format the way I need it.

          SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-mm-dd");
          java.util.Date currentTime_1 = new java.util.Date();
          String dateToday = formatter.format(currentTime_1);


The problem I am having now is trying to figure out how to determine what the date was 1 month ago, using the same format.
0
Comment
Question by:Chad Smith
7 Comments
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6280885
You should use GregorianCalendar instead, and use its add method to change the month field.

Date is ridden with inconsistencies (see the abstract class Calendar for details).
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 6280954
I agree with Stimpy. Just beware of two features with GregorianCalendar. Months are zero-based (like array counting); everything else is 1-based. Also it's my recollection that you have to code for dates where the year would change. Adding -1 to the month when you are in the month of January does not automatically go back to month 11 (December) of the previous year.


Dorothy
0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6281044
According to the API, overflow in a smaller field (months) carries over to a larger field (years).  If the actual operation is different than this, it's a bug.

The roll method, by comparison, *does not* propagate overflow.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 6

Accepted Solution

by:
dorothy2 earned 100 total points
ID: 6281107
Actually, only month is incorrect, not year. Here are the test cases I created for July 2, 2001, January 1, 2002, and June 29, 2001, and the code I had to write to correct.

Test cases

   // Testing for beginning of month, beginning of year, regular date
    // Confusing because you have to pass a GregorianCalendar object month - 1
    //GregorianCalendar d = new GregorianCalendar(2001, 6, 2);
    //GregorianCalendar d = new GregorianCalendar(2002, 0, 1);
    //GregorianCalendar d = new GregorianCalendar(2001, 5, 29);

// Actual code
      int month = (fileDate.get(GregorianCalendar.MONTH)) + 1;

        int checkdate = currentDate.get(GregorianCalendar.DAY_OF_MONTH);
        int filedate = fileDate.get(GregorianCalendar.DAY_OF_MONTH);

       /**
         * Check for first of year and first of month
         */
        if(checkdate < filedate) {
          if( (month - 1) == 0)
            month = 12;
          else
            month--;
        }
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6286221
I agree with stimpyjcat. If your implementation of Calendar does not rollback to the previous year when subtracting months then it is defective. I don't see how the code you posted supports your claim that adding -1 to Calendar.MONTH when the current month is Calendar.JANUARY does not result in a MONTH value of DECEMBER and a YEAR value of the previous year. My testing indicates that this feature of Calendar works correctly -- at least in the Sun and IBM JDKs.

Best regards,
Jim Cakalic
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 6286237
Thanks for the input. I will double-check it.
0
 
LVL 10

Author Comment

by:Chad Smith
ID: 6286328
FYI, this is the code that I used to meet my needs.  Thank you all, especially dorothy2 for putting me on the right track.


     Calendar currentDate = new GregorianCalendar();
      currentDate.add(GregorianCalendar.MONTH,-1);
      int month = (currentDate.get(GregorianCalendar.MONTH)) + 1;
     int day = (currentDate.get(GregorianCalendar.DAY_OF_MONTH));
     int year = (currentDate.get(GregorianCalendar.YEAR));

     System.out.println(year + "-" + month + "-" + day);
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java jdbc batch example 8 41
Custom Java Application connects to database: Which one of the 20? 4 35
iterator/ListIterator approach 17 39
ejb entity bean example 2 17
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question