aspose file tools*
The moose likes Java in General and the fly likes How to display date in Daylight Savings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to display date in Daylight Savings" Watch "How to display date in Daylight Savings" New topic
Author

How to display date in Daylight Savings

srinadh penugonda
Greenhorn

Joined: Feb 13, 2007
Posts: 13
I need to transfer the date in 'yyyymmdd hh.mm.ss' format to "Tue Jun 12 13:54:42 EDT 2007" format.

I have the following code:
private String formatTimeStamp(String timeStamp) {
StringBuffer tempDate = new StringBuffer();
int year;
int day;
int month;
int hour;
int minute;
int second;

tempDate.append(timeStamp);
year = Integer.parseInt(tempDate.substring(0, 4));
month = Integer.parseInt(tempDate.substring(4, 6));
day = Integer.parseInt(tempDate.substring(6, 8));
hour = Integer.parseInt(tempDate.substring(9, 11));
minute = Integer.parseInt(tempDate.substring(12, 14));
second = Integer.parseInt(tempDate.substring(15, 17));

return (new Date((year-1900), (month-1), day, hour, minute, second).toString());
}

However, this gives date as: Tue Jun 12 13:54:42 EST 2007

Is there something I should initialize or follow different way to code?
Thanks in advance.


A 1.0 [92%]<br />P 6.0 [preparing]
Oscar Gonzalez
Ranch Hand

Joined: Mar 28, 2006
Posts: 63
Yes, you should be using the SimpleDateFormat class or any of the other Date Formatters you already have with Java. Take a look at SimpleDateFormat and you will get a better solution.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
First, note that the Date constructor which you've used has been deprecated. The API suggests a way to use a Calendar to do this, but personally I would usually use a DateFormat. Something like this:

However I suspect that the real problem here is in how your computer's time zone has been set. If you're on Windows, go to Control Panel -> Date/Time -> Time Zone, select "Eastern Time", and select "Automatically adjust clock for daylight savings". If that doesn't work, please describe what operating system you're using, and what the time zone is currently set to.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display date in Daylight Savings