Solved

Dates in Java

Posted on 2007-11-19
5
266 Views
Last Modified: 2010-03-30
I've watched the following code execute, and every time I check the date of expiration after this executes, I get something like:

Wed Feb 01 00:00:00 CST 3905

Actually, I'm getting this same date every time it executes. The values for Year and Month are (2005, 1) respectively.
String strMonth = request.getParameter("txtMonth");

String strYear = request.getParameter("txtYear");

		

int Month = Integer.parseInt(strMonth);

int Year = Integer.parseInt(strYear);

		

		

Date expiration = new Date(Year, Month, 1,0 ,0 );

Open in new window

0
Comment
Question by:Messenger569
  • 2
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
cmalakar earned 250 total points
Comment Utility
year parameter should be substracted by 1900...

ie.,

Year = Year - 1900
Date expiration = new Date(Year, Month, 1,0 ,0 );
0
 
LVL 23

Assisted Solution

by:cmalakar
cmalakar earned 250 total points
Comment Utility
Month takes the values between 0-11

Subtract 1 from month also
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Try this.
Date expiration = new Date(Year-1900, Month, 1,0 ,0 );
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Sorry I didnt see previous responce.
0
 
LVL 1

Expert Comment

by:TECHHEAD_biz
Comment Utility
Actually, the above constructors are deprecated.  More correct would be:

Date expiration = ( new GregorianCalendar(Year, Month-1, 1) ).getTime();
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

15 Experts available now in Live!

Get 1:1 Help Now