Solved

Determine date 1 month ago

Posted on 2001-07-13
7
1,191 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 59
eclipse java  build path 6 56
how do i compare an object based on two fields 6 53
MySqlDump not dumping triggers 1 31
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

770 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