Solved

Determine date 1 month ago

Posted on 2001-07-13
7
1,194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

710 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