AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Printing out dates

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Discussions
View previous topic :: View next topic  
Author Message
KenStahl
Guest





PostPosted: Thu Apr 26, 2007 3:21 am    Post subject: Printing out dates Reply with quote



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





PostPosted: Thu Apr 26, 2007 7:10 am    Post subject: Re: Printing out dates Reply with quote



"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





PostPosted: Thu Apr 26, 2007 4:53 pm    Post subject: Re: Printing out dates Reply with quote



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






PostPosted: Sat Apr 28, 2007 7:10 am    Post subject: Re: Printing out dates Reply with quote

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





PostPosted: Sun Apr 29, 2007 11:37 pm    Post subject: Re: Printing out dates Reply with quote

Thanks. I'll give that a try.
Back to top
KenStahl
Guest





PostPosted: Mon Apr 30, 2007 3:11 am    Post subject: Re: Printing out dates Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Discussions All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.