Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:59:40+00:00 2026-06-13T07:59:40+00:00

I have searched for the answer to this, but cannot seem to find one.

  • 0

I have searched for the answer to this, but cannot seem to find one. Can someone explain why the following code does not give me the 1 of each month, but jumps to the 31 for some months? It is for a report event that needs to determine the next month’s date.

DateFormat formatter = new SimpleDateFormat("dd-MMM-yy"); 
Calendar cal = Calendar.getInstance( TimeZone
    .getTimeZone( "America/Los_Angeles" ) );
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy"); 
Date date;
try
{
  date = (Date)formatter.parse("01-JUN-12");
  cal.setTime(date);
  //cal.set(2012, 05, 01);

  long now = new Date().getTime();
  int frequency = 1;

  System.out.println("Current calendar time=" + cal.getTime().toString()) ;

  while (cal.getTime().getTime() < now)
  {
      cal.add( Calendar.MONTH, frequency );
      System.out.println("In loop - current calendar time=" + cal.getTime().toString()) ;
  }
}
catch (ParseException e)
{
  // TODO Auto-generated catch block
  e.printStackTrace();
} 

The output is:

Current calendar time=Fri Jun 01 00:00:00 EDT 2012

In loop - current calendar time=Sun Jul 01 00:00:00 EDT 2012

In loop - current calendar time=Tue Jul 31 00:00:00 EDT 2012

In loop - current calendar time=Fri Aug 31 00:00:00 EDT 2012

In loop - current calendar time=Mon Oct 01 00:00:00 EDT 2012

In loop - current calendar time=Wed Oct 31 00:00:00 EDT 2012

Notice how it jumps to 31, then back to 1. If I use Calendar.set() instead, the output is correct:

Current calendar time=Fri Jun 01 15:14:26 EDT 2012

In loop - current calendar time=Sun Jul 01 15:14:26 EDT 2012

In loop - current calendar time=Wed Aug 01 15:14:26 EDT 2012

In loop - current calendar time=Sat Sep 01 15:14:26 EDT 2012

In loop - current calendar time=Mon Oct 01 15:14:26 EDT 2012

In loop - current calendar time=Thu Nov 01 15:14:26 EDT 2012

This seems like it is either 1) A bug with the Calendar API, or 2) a lack of understanding of how the Calendar API works. In either case, I just want the next month (same day), unless of course there is a problem with certain months and days. But the above scenario is puzzling me. This does not happen with any other day of the month, only with the 1st.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:59:41+00:00Added an answer on June 13, 2026 at 7:59 am

    It’s a timezone issue. Replace your existing setup lines with the following, so that the Calendar and the Formatter are both using the same timezone:

        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
        formatter.setTimeZone(tz);
        Calendar cal = Calendar.getInstance(tz);
    

    and it all works fine:

    Current calendar time=Fri Jun 01 08:00:00 BST 2012
    In loop - current calendar time=Sun Jul 01 08:00:00 BST 2012
    In loop - current calendar time=Wed Aug 01 08:00:00 BST 2012
    In loop - current calendar time=Sat Sep 01 08:00:00 BST 2012
    In loop - current calendar time=Mon Oct 01 08:00:00 BST 2012
    In loop - current calendar time=Thu Nov 01 07:00:00 GMT 2012
    

    You may want to replace the toString() calls with formatter.format() calls so that the output is in the right timezone too (it might look OK to you if you are in EDT, but I’m in the UK timezone, as you can see).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched stackoverflow to find an answer but i can not find one.
I know this is very trivial but I can't seem to find the answer
I have searched but cannot find the answer to a frustrating problem of mine.
i have problem with my android app, i searched but cannot find answer, i
I have searched the site, but can not find the answer or how it
I've searched and searched but can't seem to find the answer I need. I'm
I've searched around for this, but cannot find a direct answer so here it
I definitely searched for this, but I can't find the answer, even though it
I have searched for a general solution to this but only find answers to
I have searched for an answer but couldn't find it anywhere. My question is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.