Help with Date and SimpleDateFormat

K

Kyote

I'm very new to Java and can't seem to figure this out. I'm running
the below code to try and understand Date and and formatting. But once
I run it, then try changing what the string is, it still outputs the
first strings value, although it is formatted. Why does it keep
displaying the value that test equaled when I first ran it? I'm using
1.3.1 API in WebSphere on a Windows XP machine if that helps any.





import java.text.SimpleDateFormat;
import java.util.Date;


public class TestDate {

public static void getMyDate(String dd) {

SimpleDateFormat sdf = new
SimpleDateFormat("M/d/yyyy");
Date dt = new Date();
dd = sdf.format(dt);
System.out.println(dd);
}

public static void main(String[] args) {

String test = null;
test = "10162003";

getMyDate(test);


}

}




Kyote
 
V

VisionSet

Kyote said:
...
once I run it, then try changing what the string is, it still outputs the
first strings value, although it is formatted. Why does it keep
displaying the value that test equaled when I first ran it?
...
public class TestDate {

public static void getMyDate(String dd) {

SimpleDateFormat sdf = new
SimpleDateFormat("M/d/yyyy");
Date dt = new Date();
dd = sdf.format(dt);
System.out.println(dd);
}

public static void main(String[] args) {
String test = null;
test = "10162003";
getMyDate(test);
}
}




I don't understand why you are passing a String to the method getMyDate()
It is serving no purpose since you do nothing with it and then:
dd = sdf.format(dt);

makes dd point to a different String.

what are you trying to achieve?
Your code perhaps does not fully demonstrate your misunderstanding.
Why does it keep
displaying the value that test equaled when I first ran it?

The way you have the code you supplied, it wall always display the current
date, irrespective of the value you pass to getMyDate()
 
E

Emanuel Bulic

you need to re-compile the code. since it's a standalone app,
websphere has no bearing.. just keep track of your classpath, clean
up the old .class files. consider passing the string as a command
line argument... here's a quick&dirty way to do that:

public static void main(String[] args) {

//String test = null;
//test = "10162003";
if (args.length>0)
getMyDate(args[0]);


}

Kyote said:
I'm very new to Java and can't seem to figure this out. I'm running
the below code to try and understand Date and and formatting. But once
I run it, then try changing what the string is, it still outputs the
first strings value, although it is formatted. Why does it keep
displaying the value that test equaled when I first ran it? I'm using
1.3.1 API in WebSphere on a Windows XP machine if that helps any.





import java.text.SimpleDateFormat;
import java.util.Date;


public class TestDate {

public static void getMyDate(String dd) {

SimpleDateFormat sdf = new
SimpleDateFormat("M/d/yyyy");
Date dt = new Date();
dd = sdf.format(dt);
System.out.println(dd);
}

public static void main(String[] args) {

String test = null;
test = "10162003";

getMyDate(test);


}

}




Kyote
 
K

Kyote

Kyote said:
...
once I run it, then try changing what the string is, it still outputs the
first strings value, although it is formatted. Why does it keep
displaying the value that test equaled when I first ran it?
...
public class TestDate {

public static void getMyDate(String dd) {

SimpleDateFormat sdf = new
SimpleDateFormat("M/d/yyyy");
Date dt = new Date();
dd = sdf.format(dt);
System.out.println(dd);
}

public static void main(String[] args) {
String test = null;
test = "10162003";
getMyDate(test);
}
}




I don't understand why you are passing a String to the method getMyDate()
It is serving no purpose since you do nothing with it and then:
dd = sdf.format(dt);

makes dd point to a different String.

what are you trying to achieve?
Your code perhaps does not fully demonstrate your misunderstanding.
Why does it keep
displaying the value that test equaled when I first ran it?

The way you have the code you supplied, it wall always display the current
date, irrespective of the value you pass to getMyDate()


Okay, what I'm trying to do is to send a string and convert it to a
date and then format it. Apparently I don't understand the API at
ALL.... :(

Now I understand that I pointed to a new string with
dd = sdf.format(dt);

Could someone show me a simple way to do this so I can play around
with it?
 
V

VisionSet

Kyote said:
public class TestDate {

public static void getMyDate(String dd) {

SimpleDateFormat sdf = new
SimpleDateFormat("M/d/yyyy");
Date dt = new Date();
dd = sdf.format(dt);
System.out.println(dd);
}

public static void main(String[] args) {
String test = null;
test = "10162003";
getMyDate(test);
}
}

Okay, what I'm trying to do is to send a string and convert it to a
date and then format it. Apparently I don't understand the API at
ALL.... :(

Now I understand that I pointed to a new string with
dd = sdf.format(dt);

Could someone show me a simple way to do this so I can play around
with it?

Well I'm still not sure exactly what you want.

The inbuilt parsing ability requires Strings like this to succeed:

25/12/03 9:9 PM, GMT

I think the separators are flexible.

If all you have is 10162003 then you will have to write your own parser.

This can be very straightforward if it is always 8 digits.

just use String.substring to lift out the relevent bits and convert to ints
like this:

int year = Integer.parseInt(myDateString.substring(4,8));

then construct a GregorianCalendar:

GregorianCalendar cal = new GregorianCalendar(int year, int month, int day);

and format as before like:

SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
String dd = sdf.format(cal.getTime());
System.out.println(dd);

A faster alternative if you only want the date for formatting, is to convert
the ints for year, month, day - directly to milliseconds since 'January 1,
1970, 00:00:00 GMT'

There may be some handy utility methods to help I've missed out.
 
K

Kyote

public class TestDate {

public static void getMyDate(String dd) {

SimpleDateFormat sdf = new
SimpleDateFormat("M/d/yyyy");
Date dt = new Date();
dd = sdf.format(dt);
System.out.println(dd);
}

public static void main(String[] args) {
String test = null;
test = "10162003";
getMyDate(test);
}
}

Okay, what I'm trying to do is to send a string and convert it to a
date and then format it. Apparently I don't understand the API at
ALL.... :(

Now I understand that I pointed to a new string with
dd = sdf.format(dt);

Could someone show me a simple way to do this so I can play around
with it?

Well I'm still not sure exactly what you want.

The inbuilt parsing ability requires Strings like this to succeed:

25/12/03 9:9 PM, GMT

I think the separators are flexible.

If all you have is 10162003 then you will have to write your own parser.

This can be very straightforward if it is always 8 digits.

just use String.substring to lift out the relevent bits and convert to ints
like this:

int year = Integer.parseInt(myDateString.substring(4,8));

then construct a GregorianCalendar:

GregorianCalendar cal = new GregorianCalendar(int year, int month, int day);

and format as before like:

SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
String dd = sdf.format(cal.getTime());
System.out.println(dd);

A faster alternative if you only want the date for formatting, is to convert
the ints for year, month, day - directly to milliseconds since 'January 1,
1970, 00:00:00 GMT'


What I was trying to do was learn to understand Date. Now, instead of
being passed a string and converting it into a date, I find that I am
being passed a Calendar object. They changed things on me.

So now I have some different problems. Here's what I have so far.

public void setRequestedDate(Calendar param) {
String c =
param.get(Calendar.MONTH)
+ "/"
+ param.get(Calendar.DAY_OF_MONTH)
+ "/"
+ param.get(Calendar.YEAR)
+ " "
+ param.get(Calendar.HOUR_OF_DAY)
+ ":"
+ param.get(Calendar.MINUTE);

}

I have no clue if I'm doing this right. I'd really like to be able to
create a main to test this out so I can continue to experiment, but,
I'm at a loss as to how to do so. Please, any help would be highly
appreciated!!
 
V

VisionSet

What I was trying to do was learn to understand Date. Now, instead of
being passed a string and converting it into a date, I find that I am
being passed a Calendar object. They changed things on me.

So now I have some different problems. Here's what I have so far.

public void setRequestedDate(Calendar param) {
String c =
param.get(Calendar.MONTH)
+ "/"
+ param.get(Calendar.DAY_OF_MONTH)
+ "/"
+ param.get(Calendar.YEAR)
+ " "
+ param.get(Calendar.HOUR_OF_DAY)
+ ":"
+ param.get(Calendar.MINUTE);

}

I have no clue if I'm doing this right. I'd really like to be able to
create a main to test this out so I can continue to experiment, but,
I'm at a loss as to how to do so. Please, any help would be highly
appreciated!!

At one point you said this:
Okay, what I'm trying to do is to send a string and convert it to a
date and then format it. Apparently I don't understand the API at
ALL.... :(

this is the only time you have really tried to say what you want to do. Is
this still what you want? Because if it is, it seems that you now have it.
If you have a Calendar object ie param, then you just get a Date object with
getTime(). If you want to format it then just do:

You can do all that manual concatenation you are doing, but the Format
classes make this unnecessary, hence the new pattern above: "M/d/yyyy hh:mm"

With the main you require to test just do:

public static void main(String[] args) {

ThisClass a = new ThisClass();
a.setRequestedDate(new GregorianCalendar());
}

but your setRequestedDate method is not setting anything!
Is this what you want to do? Set a Calendar object to a certain date?

Perhaps you could try to express clearly what you want to do!
 
K

Kyote

Thanks for all your Help Mike.
At one point you said this:


this is the only time you have really tried to say what you want to do. Is
this still what you want? Because if it is, it seems that you now have it.

No, I no longer need to convert a string to a date and format it. But
your right that I have learned a little bit, thanks to your help.
Though I have to admit I don't fully understand it all but I know
enough to work on it if I get the time. Thank you!

If you have a Calendar object ie param, then you just get a Date object with
getTime(). If you want to format it then just do:

Ahhh!!! So I set up the reference to my format ie sdf then use it to
say sdf.format my object.getTime... That makes sense. I don't know
why, maybe it's my difficulty with the API, but java seems to keep me
off balance...

You can do all that manual concatenation you are doing, but the Format
classes make this unnecessary, hence the new pattern above: "M/d/yyyy hh:mm"

No, the concatenation was because I wasn't understanding this
properly. Thank you again.

With the main you require to test just do:

public static void main(String[] args) {

ThisClass a = new ThisClass();
a.setRequestedDate(new GregorianCalendar());
}

Thank you for this example of a main. I haven't tried anything from
this post I'm replying to, yet, but I can see what I need to do now.
for this.

but your setRequestedDate method is not setting anything!
Is this what you want to do? Set a Calendar object to a certain date?

Perhaps you could try to express clearly what you want to do!


What I'm needing to do is a little of both. Some of the fields will be
sending me a date that the User is inputting and I'll have to set it
with setRequestedDate. But there are a few other instances where I
just need the current date and time. Maybe I can get it now. Thanks
Mike, your a life saver.


Scott
 
K

Kyote

Sure there is, read the friggin' API already!


I did look in the API. I couldn't find it there and I'd really
appreciate it if someone can tell me how to find it there?

But I found it in Websphere's context sensitive help.
I simply use capital H's instead of h.

fwiw I always check the API first. In case you hadn't caught that
particular part of my earlier post, I don't understand the API at ALL.
:) Well, that's not completely true. I understand very little of it...
In fact if there's any web site anyone knows of that could possibly
help me understand it better I'd REALLY appreciate that.


Thanks for all the help.
 
M

Michael Borgwardt

Kyote said:
I did look in the API. I couldn't find it there and I'd really
appreciate it if someone can tell me how to find it there?

But I found it in Websphere's context sensitive help.
I simply use capital H's instead of h.

Exactly, and there's a nice clean table in the API doc of
SimpleDateFormate where this is decribed.
fwiw I always check the API first. In case you hadn't caught that
particular part of my earlier post, I don't understand the API at ALL.
:) Well, that's not completely true. I understand very little of it...
In fact if there's any web site anyone knows of that could possibly
help me understand it better I'd REALLY appreciate that.

Why exactly do you have problems understanding it? You definitely
should do everything you can to amend that, understanding the API
docs is an absolutely crucial skill for all but the most trivial
use of Java.
 
K

Kyote

Exactly, and there's a nice clean table in the API doc of
SimpleDateFormate where this is decribed.

OUCH!!! I see the table now... How I missed it earlier I can't
explain. If I'd have seen it I wouldn't have had to use Websphere to
find it.... After I read your post I went straight to SimpleDateFormat
and started scrolling down to look for the alleged table....
*sniffles* I apologize !! I really did look through the API,
repeatedly....
Why exactly do you have problems understanding it? You definitely
should do everything you can to amend that, understanding the API
docs is an absolutely crucial skill for all but the most trivial
use of Java.

I would in a heartbeat if I knew how. That's why I was asking for a
url in my last post. I'd love to find out why it is I keep having
trouble with the API.
 
M

Michael Borgwardt

Kyote said:
I would in a heartbeat if I knew how. That's why I was asking for a
url in my last post. I'd love to find out why it is I keep having
trouble with the API.

No URL is going to be able to tell you *why* you have that problem.
The Java API docs are generally considered to be quite well-structures
and helpful.

Again: what problems exactly do you run into when using them? Where
do you look first, where and why do you give up?
 
K

Kyote

No URL is going to be able to tell you *why* you have that problem.
The Java API docs are generally considered to be quite well-structures
and helpful.

Again: what problems exactly do you run into when using them?

Misunderstanding for the most part. There are 3 panes (Top Left,
Bottom Left, Right) I think the top left are the packages. The bottom
left are the classes. My trouble comes in the pane on the right.

When I choose say, java.text (top left), SimpleDateFormat (bottom
left), I get the class description to the right. I understand that
much, I think..?? When I click on Method ie

SUMMARY: INNER | FIELD | CONSTR | METHOD

It takes me down to the Method area of the page. In fact, I think I
read the SimpleDateFormat class description in Websphere, so I kept
skipping straight to methods, fields, and constructors, without
scrolling down on the description, in the java API.

The layout of the methods table is what seems to toss me for a spin.
Here's an example:

void applyLocalizedPattern(String pattern)
Apply the given localized pattern string to this date
format.

void

Okay, void is in the left hand column of the table:

applyLocalizedPattern(String pattern)

applyLocalizedPattern(String pattern) is in the right hand along with
a description. I can't for the life of me remember what's what. I
think void is the return type? Not knowing for sure gets me frustrated
rather quickly I'm sorry to say. Not to mention that I'm not too sure
of the right hand column... Is there a help file that explains all of
it in detail? Something I can refer to when I forget, which is likely,
at least at first?
Where do you look first, where and why do you give up?

I usually give up when I can't understand where to go after looking at
the methods. Well, I will actually look at several methods before
actually giving up. But when I try something from the API that I think
I understand only to get an exception from the attempt, it makes me
think that my understanding of the API is flawed. THAT's when I give
up. But I still keep going back to the API each time I need to look up
something hoping that maybe I had a typographical error.....

Please, feel free to straighten me out. I need to understand the API.

Thank you for any help you can provide.
 
D

David Postill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

10:33:49 -0500, Kyote <[email protected]>
wrote:

<snip />

| I usually give up when I can't understand where to go after looking at
| the methods. Well, I will actually look at several methods before
| actually giving up. But when I try something from the API that I think
| I understand only to get an exception from the attempt, it makes me
| think that my understanding of the API is flawed. THAT's when I give
| up. But I still keep going back to the API each time I need to look up
| something hoping that maybe I had a typographical error.....
|
| Please, feel free to straighten me out. I need to understand the API.
|
| Thank you for any help you can provide.

Have you run through the java tutorial yet?

Well worth doing to increase your general understanding of how java works and
how various bits fit together.


[[http://java.sun.com/docs/books/tutorial/index.html][The Java Tutorial]]

Also try Dick Baldwin's tutorial. There are 3 different ones depending on your
level of understanding.

[[http://www.dickbaldwin.com/tocint.htm][Introductory Java Programming]]
[[http://www.dickbaldwin.com/tocmed.htm][Intermediate Java Programming]]
[[http://www.dickbaldwin.com/tocadv.htm][Advanced Java Programming]]

And if you get stuck on understanding a particular keyword or concept then try
Roedy Greens Java Glossary. Another
excellent resource.

[[http://mindprod.com/jgloss/jgloss.html][mindprod.com]] Java Glossary

Finally there is Peter Lindens' Java Programmers Faq

[[http://www.afu.com/intro.html][Java Programmers]]

And the JGuru FAQs

[[http://www.jguru.com/faq][jguru.com]]

Regards,

<davidp />

- --
David Postill




-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.2 - not licensed for commercial use: www.pgp.com
Comment: Get key from pgpkeys.mit.edu:11370

iQA/AwUBP5a6IHxp7q1nhFwUEQIlnwCgxVwqKVGk3tiXnBTbqHVy1Ohtlw8AoNHK
kIgdfBfGLuLOc93E3vKkMezg
=+v+F
-----END PGP SIGNATURE-----
 
M

Michael Borgwardt

Kyote wrote:
[API docs]
Again: what problems exactly do you run into when using them?
[]
The layout of the methods table is what seems to toss me for a spin.
Here's an example:

void applyLocalizedPattern(String pattern)
Apply the given localized pattern string to this date
format.

void

Okay, void is in the left hand column of the table:

applyLocalizedPattern(String pattern)

applyLocalizedPattern(String pattern) is in the right hand along with
a description. I can't for the life of me remember what's what. I
think void is the return type?

What else could it be? I really don't see how this could pose a problem.

rather quickly I'm sorry to say. Not to mention that I'm not too sure
of the right hand column... Is there a help file that explains all of
it in detail? Something I can refer to when I forget, which is likely,
at least at first?

To me, it was all nearly obvious right from the beginning. It sounds like
you just need to get used to the structure. Maybe it's just confusing to you
because you keep jumping into the middle of huge classe descriptions.
Try browsing through the API docs for some classes that have only
a couple of methods. The structure should be easier to understand there.

I usually give up when I can't understand where to go after looking at
the methods. Well, I will actually look at several methods before
actually giving up. But when I try something from the API that I think
I understand only to get an exception from the attempt, it makes me
think that my understanding of the API is flawed. THAT's when I give
up.

OK, that part I can understand. Note that the detailed method descriptions
also often list the exceptions that can be throw, and what the reason would
be. Unfortunately, these descriptions are often missing or incomplete, and
of course the can't cover stuff like NullPointerException or
ArrayIndexOutOfBoundException that can crop up almost everywhere.

In a case where an API library method throws an unexplainable exception,
it can help to look at the source code where the exception is thrown.
The SDK comes with the complete API library source, and a good IDE
should be able to include it so that you can jump directly to it from
the method call in your code. The sources also include the documentation
as JavaDoc comments, and nowadays I rarely look at the HTML docs anymore.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top