This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Working with SimpleDateFormat and DAte classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Working with SimpleDateFormat and DAte classes" Watch "Working with SimpleDateFormat and DAte classes" New topic
Author

Working with SimpleDateFormat and DAte classes

Ilh Oleo
Ranch Hand

Joined: Feb 07, 2007
Posts: 57
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Feb 07, 2007
Posts: 57
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Ilh Oleo
Ranch Hand

Joined: Feb 07, 2007
Posts: 57
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Feb 07, 2007
Posts: 57
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

Joined: Feb 07, 2007
Posts: 57
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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;


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ilh Oleo
Ranch Hand

Joined: Feb 07, 2007
Posts: 57
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Feb 07, 2007
Posts: 57
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Feb 07, 2007
Posts: 57
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Feb 07, 2007
Posts: 57
Thanks now i know how to work with dates...awesome work from the responder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working with SimpleDateFormat and DAte classes
 
Similar Threads
time conversion
Unparseable date exception....!!!!
Time Zone Issue
Array function similar to Excel Match?
Time Zone problem