Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Determine date 1 month ago

Posted on 2001-07-13
7
Medium Priority
?
1,212 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Accepted Solution

by:
dorothy2 earned 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

972 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