Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to display date in Daylight Savings

Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to transfer the date in 'yyyymmdd' 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;

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.
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.
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.
Do not set lab on fire. Or this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic