• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems in converting to a Date Object.

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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:
--------------------
getName=tName.getText();
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();
dt.setYear(Nyear);
dt.setMonth(Nmonth);
dt.setDate(Nday);
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.

------------------
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the javadoc for Date.setMonth(int). It will explain your problems with both month and day.
- Peter
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Months are number as 0-11, not 1-12.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic