Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Problems in converting to a Date Object. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems in converting to a Date Object." Watch "Problems in converting to a Date Object." New topic

Problems in converting to a Date Object.

Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
I have got a peculiar pblm. From a textfield I am getting a user input date. Eg(31/10/2001) through a String. Through SubString function,I seperate the day,month and the year. I create a Date Object(Dateob) and set the Year,month,day to the new values which I have got. Using the getmethods of the Date,I try to print the values but the values are a bit different ie(the previous month's values are displayed).
I am enclosing the code and below the code is the output I am getting.
the part of the code:
String date = getFirstDate.substring(0,2);
System.out.println( " day is "+day);
String month = getFirstDate.substring(3,5);
System.out.println(" Month is "+month);
String Year = getFirstDate.substring(6,10);
System.out.println(" Year is "+Year);
int Nday = Integer.parseInt(day);
int Nmonth=Integer.parseInt(month);
int Nyear = Integer.parseInt(Year);
Date dt = new Date();
System.out.println(" Date is "+dt.getDate());
System.out.println(" Month is "+dt.getMonth());
System.out.println(" Year is "+dt.getYear());
output of the program:
day is 31
Month is 10
Year is 2001
Date is 1
Month is 11
Year is 2001
Please kindly look into my solution.
with warm regards ,
Arun J Martin.

Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Arun,
It seems like a lot of work to perform for something that Java already handles for you! What about SimpleDateFormatter? Sounds like just want you need ...

Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
Hi Manfred,
Thanks for the solution.I was under the impression that Months were numbered from 1 to 12 but actually they are from 0 to 11. Now my pblm is solved.
with warm regards,
Ana Mihailescu

Joined: Apr 26, 2001
Posts: 29
In the latest time, the Calendar object is used instread of Date which contains many deprecated methods.
Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
Hi Ana,
Thanks for the information. I have got a small question. Will the inclusion of deprecated methods play a significant role in the coding process?.
with warm regards,
mick barry

Joined: Apr 30, 2001
Posts: 16
Use of deprecated methods should not affect your coding. Most deprecated methods work fine.
What you need to be aware of if that the fact that they are deprecated implies that those methods may not be available in a future jdk releases.

I agree. Here's the link:
subject: Problems in converting to a Date Object.
Similar Threads
Problems in converting to a Date Object.
Months and years
Precise age calculation..
validating day and month using SimpleDateFormat
add one day to current date