| View previous topic :: View next topic |
| Author |
Message |
KenStahl Guest
|
Posted: Thu Apr 26, 2007 3:21 am Post subject: Printing out dates |
|
|
I have written a small program that looks like this:
import java.lang.*;
import java.util.*;
import java.text.*;
class datetest {
public static void main(String args[]){
Date nd = null;
String ds = "01/01/1970 00:00:00 GMT";
String df = "MM/dd/yyyy HH:mm:ss zzz";
SimpleDateFormat f = new SimpleDateFormat(df);
try {
nd = f.parse(ds);
}
catch (Exception e){
System.out.println(e.toString());
}
System.out.println("Date: " + nd);
}
}
When I run this program the output is: Date: Wed Dec 31 19:00:00 EST 1969
I want it to print out the literal time that I supplied so it would be: Thr
Jan 1 00:00:00 GMT 1970
I can't set the timezone in the date object, so what would I have to do to
make it come out right. I suspect that it would involved a Calendar object,
but how do I move the time from the f.parse(ds) into a Calendar object that
is set up for GMT? |
|
| Back to top |
|
 |
Dirk Michaelsen Guest
|
Posted: Thu Apr 26, 2007 7:10 am Post subject: Re: Printing out dates |
|
|
"KenStahl" <ktsahl (AT) yahoo (DOT) com> wrote:
| Quote: | I have written a small program that looks like this:
[...]
When I run this program the output is: Date: Wed Dec 31 19:00:00 EST 1969
|
on my system the output of your class is:
Date: Thu Jan 01 01:00:00 CET 1970
cu
Dirk |
|
| Back to top |
|
 |
Odin Guest
|
Posted: Thu Apr 26, 2007 4:53 pm Post subject: Re: Printing out dates |
|
|
The Date class follows the locale for the system where the program is run.
That's part of the problem. Date should be locale neutral. If you think
about it, the time in milliseconds that is stored internally would be the
same for both you and I - but when it is just printed out as I did in the
program it is the same as if I had written it as nd.toString(), so somewhere
the assumption is being made that whenever toString() is used, it should
output the value for the current locale.
What I need is a way to make it print out for the locale/timezone of my
choice. |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Apr 28, 2007 7:10 am Post subject: Re: Printing out dates |
|
|
Just try :
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
class DateTest2 {
public static void main(String args[]){
Date nd = null;
String ds = "01/01/1970 00:00:00 GMT";
String df = "MM/dd/yyyy HH:mm:ss zzz";
SimpleDateFormat f = new SimpleDateFormat(df);
f.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
nd = f.parse(ds);
}
catch (Exception e){
System.out.println(e.toString());
}
System.out.println("Date: " + f.format(nd));
}
}
that should do the trick.
Tom. |
|
| Back to top |
|
 |
KenStahl Guest
|
Posted: Sun Apr 29, 2007 11:37 pm Post subject: Re: Printing out dates |
|
|
| Thanks. I'll give that a try. |
|
| Back to top |
|
 |
KenStahl Guest
|
Posted: Mon Apr 30, 2007 3:11 am Post subject: Re: Printing out dates |
|
|
I just tried it. That did the trick. Now I know how to solve the rest of my
problems. Thanks for the assist. |
|
| Back to top |
|
 |
|