Solved

Determine date 1 month ago

Posted on 2001-07-13
7
1,183 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 6

Accepted Solution

by:
dorothy2 earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the input. I will double-check it.
0
 
LVL 10

Author Comment

by:Chad Smith
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
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 …
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now