• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working with SimpleDateFormat and DAte classes

 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all of you, I�m happy for being here and learn to program in Java
I�m have a problem understanding the SimpleDateFormat and Date classes
when applying them to a project from my school.
I have to create a kind of password with the birthdate typed by the user in the format ddMMyyyy.
I�ve declared a Date birthDate to store it but there is until i �ve arrived because i don�t know how works the DAte class.If i declared this [B]persona.setFechaNacimineto("04221985") i get this error:"The method setFechaNacimineto(Date) in the type Persona is not applicable for
the arguments (String)"
Could someone explain how works that classes
Also i found this code



It will work??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure what your 'setFechaNacimineto' is, but here's a simple example
of parsing a date format, and outputting in another format

 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
not sure what your 'setFechaNacimineto' is, but here's a simple example
of parsing a date format, and outputting in another format




Plus there is no a preview window or something to see how it looks?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> What it this for? --> Calendar cal = Calendar.getInstance();

if you look at the Date() api docs, you'll see that many of the constructors
and methods are deprecated, with instructions to use Calendar...


> Whit that you are creating two objects with parameters to give it a format?

Yes, one to read in the string (e.g.04121985) in a certain format, and to
set a calendar time from that format (instead of Date()).
Another to output the calendar time in the format you want.


> Plus there is no a preview window or something to see how it looks?

not sure what you mean by this
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[IX]: What it this for? --> Calendar cal = Calendar.getInstance();

[MD]: if you look at the Date() api docs, you'll see that many of the constructors
and methods are deprecated, with instructions to use Calendar...


But that's only if you need Date's deprecated methods. Date is still a perfectly good class for other things, and it's all you need here. All you did with the Calendar was call setTime() and getTime(), which just retrieves the same Date you started with. We don't need Calendar for anything here:

[ February 08, 2007: Message edited by: Jim Yingst ]
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my late reply but i�m on school and have another homeworks , so that�s why i post until today...
So the Date.getinstance() method give to me the data system on which was created ??

And whit this :
cal.setTime(formato.parse(date));
i chnage the format from ddMMyyyy to EEEE, dd MMMM yyyy or what?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> So the Date.getinstance() method give to me the data system on which was created ??

Basically, yes - a calendar object is created, which is initially set to the
date/time it was created.


> i chnage the format from ddMMyyyy to EEEE, dd MMMM yyyy or what?

yes - run the program, see the output.
change 04121985 to some other date e.g. 01012001, recompile/run the program again.
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn i can�t with this problem...
If i have this
Date birthDate;
....some code....
and there is my Main Class where i create my object to work with it
Persona.birthdate=�? what kind of variable it wolud go there??



And i want to create a method to work with my birthDate variable, what kind of
�variable? �field? can i add to work with?
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this :
public String fechaConvertida(){
Calendar quedaConvertida= new Calendar();
quedaConvertida.getTime(fechaNacimiento);
}

but Eclipse says:"Calendar cannot be resolved to a type"
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to import class Calendar, which is in package java.util. Add this to the top of your source file:

import java.util.Calendar;
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i�ve already done that, but i�m talking about this
int oneparameter=1;
float otherparameter=22.2
String myname="Ilhui";
Date mybirthdate=�what kind of variable must be here?
That�s my question...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was responding to your question about the error message "Calendar cannot be resolved to a type". That means that you didn't tell the Java compiler where it should look for class Calendar - you need to add the import statement to tell the compiler that it's in the package java.util.

Date mybirthdate=�what kind of variable must be here?

The type of the variable 'mybirthdate' is Date, so after the "=" you need to put have a reference to a Date object (or a subclass of Date) ofcourse.
[ February 15, 2007: Message edited by: Jesper Young ]
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m already understanding but here is another question:



Sorry for the format of the code format...so what i watn to do is to print my actual date with a format but i always get this output:

"Thu Feb 15 18:53:18 GMT 2007
error"
The toString method serves in that way or it just a local transformation?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> cal.setTime(formato.parse(date.toString()));

you are trying to parse something based on the format SimpleDateFormat("ddMMyyyy")

add this line to your code
System.out.println(date.toString());

do the formats match?
 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(date.toString()) it�s equal to this what i did System.out.println(date);...isn�t?

But what i�m trying to do is from the date printed:Fri Feb 16 16:01:09 GMT 2007 i want it on the format "070216" (the format yyMMdd previously decalred) but it give me always error.

With this cal.setTime(formato.parse(date.toString())); i thought it will change my variable date to a String , so i could work with it ti give it the format but doesn�t work....
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> what i�m trying to do is from the date printed:Fri Feb 16 16:01:09 GMT 2007
> i want it on the format "070216"

then you just need to use format()

 
Ilh Oleo
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks now i know how to work with dates...awesome work from the responder
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic