Solved

Java Calendar And SimpleDateFormat Month Problem

Posted on 2009-04-08
4
1,688 Views
Last Modified: 2013-11-23
The month seems to come out wrong when using SimpleDateFormat on my Calendar object.  
What am I doing wrong?  When I run my code my results are ...

Sent Date: 05/08/2009
cal.get(Calendar.YEAR): 2009
cal.get(Calendar.MONTH): 4
cal. get(Calendar.DAY_OF_MONTH): 8
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
 
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2009);
cal.set(Calendar.MONTH, 4);
cal.set(Calendar.DAY_OF_MONTH, 8);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 10);
cal.set(Calendar.SECOND, 10);
cal.set(Calendar.MILLISECOND, 222);
cal.setTimeZone(TimeZone.getTimeZone("GMT-05:00"));
 
String sentDate = "";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
if (cal.getTime() != null) {
   sentDate = sdf.format(cal.getTime());
}
	    	
System.out.println("Sent Date: " + sentDate);
System.out.println("cal.get(Calendar.YEAR): " + cal.get(Calendar.YEAR));
System.out.println("cal.get(Calendar.MONTH): " + cal.get(Calendar.MONTH));
System.out.println("cal.get(Calendar.DAY_OF_MONTH): " + cal.get(Calendar.DAY_OF_MONTH));

Open in new window

0
Comment
Question by:jeffman38
[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
  • 2
4 Comments
 
LVL 1

Assisted Solution

by:isarasoo
isarasoo earned 25 total points
ID: 24098497
what values are you getting back fro the month?
tell us what your inputting and the output

Just so you know the months start from 0 i.e.:
jan=0
0
 
LVL 3

Accepted Solution

by:
NahThenLad earned 100 total points
ID: 24098640
Java sets the months internally with January being 0. When you format the date, Java will display the date in a friendly format, so the numbering gets sorted out to what would be expected from a date, with January being 1 and so on.

You are outputting the date you have formatted as a SimpleDateFormat then outputting the internal value that is being held in the data fields of the calender object you have created, so the values appear to be different, but they are the same.

HTH

Tony
0
 

Author Closing Comment

by:jeffman38
ID: 31568069
Thanks, months start at zero is silly, but what was causing my problem.
0
 
LVL 1

Expert Comment

by:isarasoo
ID: 24099720
in order to fix your issue whatever month you send i.e. 05/08/2009
you will need to dectrement the month field by one
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert from xls to xlsx using java 7 95
Java syntax, or is it Selenium 6 42
Java 8 to Java 6 8 33
How to log java errors in tomcat 8 24
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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