Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create a Data variable

 
Edwin Davidson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class daysold
{
public static void main(String x[])
{
//import and format the date to start with
// String birthdate=x[0];
String birthdate="1961-01-18";
int birthyear =Integer.valueOf( birthdate.substring(0,4)).intValue();
int birthmonth =Integer.valueOf( birthdate.substring(5,7)).intValue();
int birthday =Integer.valueOf( birthdate.substring(8)).intValue();
}
}
Above is my code. Now I have 3 int variables that I wish to combine into a Date variable such as:
Date mybirthday=new Date(int,int,int);
I know already that the Date line right above is dead wrong. I hear that Java really screwed up on the whole date/time class thing anyway. Now you need GregorianCalendar to create a Date object. So, how do I make a birthday Date variable that I can do date arithmetric with ? And is there a really good tutorial on date and time creatio/manipulation sonewhere ?
Thanks - Javaranch rocks !
Edwin Davidson
freezing my behind on in winter in Halifax, Nova Scotia in Canada !
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, you have to use a GregorianCalendar. I *think* (this is kinda guesswork) that it's designed to replace java.util.Date (there's also java.sql.Date). It handles I18N much better and has the constructor you want. java.util.Date also has a constructor that takes parameters like you want, but it's been deprecated.
Anyway, this is the sort of thing you'll want to do:

Cheers,

--Tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic