Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display date in Daylight Savings

 
srinadh penugonda
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Oscar Gonzalez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic