• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

convert from EST to GMT

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my DAO class, I have a date field start_date that comes as YYYY-MM-DD hh:mm:ss.
In the setter method, how do I convert this from EST to GMT?

public void setStart_date(String start_date){
Calendar calendar = Calendar.getInstance();

calendar.setTime(new Date());
SimpleDateFormat fmt = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");

fmt.setTimeZone(TimeZone.getTimeZone("GMT"));

this.start_date = fmt.format(calendar.getTime(start_date));   //Getting ERROR here
}

Getting Error: method getTime in class ajva.util.Calendar cannot be applied to given types;
reason: actual and formal argument lists differ in length.

Please suggest how to fix.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the new date types, not Date and Calendar.

ZonedDateTime would be the one to use.
Use the parse method to turn the String into a ZonedDateTime, then you can use the withZoneSameInstant method to change the time zone used.
 
Marshal
Posts: 79964
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Use the new date types, not Date and Calendar.

ZonedDateTime would be the one to use..



Before you do anything else, STOP and consider the above advice very seriously. Unless this is the only DAO you have in your application, you're already setting yourself and/or other teammates to write duplicate code. Think about all the other places in your app that need to work with dates with a time zone. Are you going to need the same logic in all those other parts? If so, then this DAO is not where that conversion logic should be. Think about where this code will run and what the default time zone will be (usually determined by the system's default time zone). Time zone conversion is complicated and if you try to do it yourself, you will probably not get it quite right. It's best to use a library class from a reliable source, such as the one suggested.
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please be aware that java.util.Date and java.sql.Date are EXPECTED to be UTC (GMT) values. If you want to apply timezones, heed the advice you were just given.
 
Kum Sundarah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need my time in GMT only. So, I infer I can use java.util.Date itself.
Also, looked at the ZonedDateTime. There it uses LocalDateTime to convert timezone.

My question primarily is how to take the resultset from a DAO- the start_date values from the database comes as EST. I need to convert each record with start_date from EST to GMT.
All examples I see show how to get the zone for Localtime. I want to pass the value from the database and convert that.

If you can modify my code and show the usage, that will help my understanding.

Thanks!
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A lot of databases offer 2 different types of time storage: zoneless time and time with zone. Unless you're running a purely local application (not serving the Internet), then it's better to use the data format that includes timezone. Or, if none is available, to store times in the database in GMT. Because if the database is backing a website like the JavaRanch, which covers at least 4 US time zones, not to mention the world, keeping track of who logged in what timezone can get really messy if you're storing local times without zones.

If, for whatever reason you are stuck with a database full of local times, then you're just going to have to construct time objects using the timezone locale. When you do that, the underlying absolute value will, in fact be UTC, and it will be easy to extract. I'm not going to go into any more detail because I think at this point we have 3 different generations of time services in Java and I've never bothered to look at the fine details of the latest stuff. Others here can help on that.

Note also that EST is tricky enough itself. Indiana has (or at least had) sections of the state that did not go to Daylight Savings, Florida is split with most of the state being in EDT at the moment, except the far West, which is CDT. And we're waiting for Congress to approve moving the entire state to EDT year-round. It's already been approved at the state level.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say that the start date comes from the database in EST, but the value passed into the method is a String?
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:You say that the start date comes from the database in EST, but the value passed into the method is a String?



I would bet there's a good chance that the app server's system time is Eastern Time and there's a mix of EST and EDT values because current date/time was captured using new Date() and saved to the database as a String value.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Dave Tolls wrote:You say that the start date comes from the database in EST, but the value passed into the method is a String?



I would bet there's a good chance that the app server's system time is Eastern Time and there's a mix of EST and EDT values because current date/time was captured using new Date() and saved to the database as a String value.



Unless I've been wrong all these years (and my servers run a hardware date of UTC, as do many, so this is what's been working), then new Date() constructs using the current UTC time.

That was part of the problem with the original Date class. No sensitivity to locale.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I would bet there's a good chance that the app server's system time is Eastern Time and there's a mix of EST and EDT values because current date/time was captured using new Date() and saved to the database as a String value.



Unless the database in question has no DATE/TIMESTAMP type then that's not a very good choice of datatype for it...
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Y'all are assuming the best. I was assuming the worst. I had to go through a painful data conversion once precisely because our host machine was using the local time zone and the date/time values saved in the DB were all US Pacific time. Just a couple of months ago, a client of mine had to go through a similar exercise. I've learned never to underestimate the extent of boneheadedness in the IT world.
 
Kum Sundarah
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While extracting from the database, all the fields are taken as string, start_date is formatted with to_char to be in this format. YYYY-MM-dd hh:mm:ss.

Based on your responses, I got that I need to convert my string to date, then convert the timezone.

I did this and it worked fine.

Date date1= new SimpleDateForat("YYYY-MM-dd hh:mm:ss").parse(StartDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);

SimpleDateForat estdate = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");

estdate.setTimeZone(TimeZone.getTimeZone("GMT"));

System.out.println(estdate.format(calendar.getTime()));  

Thanks all.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kum Sundarah wrote:While extracting from the database, all the fields are taken as string, start_date is formatted with to_char to be in this format. YYYY-MM-dd hh:mm:ss.


Dave Tolls wrote:

Junilu Lacar wrote:
I would bet there's a good chance that the app server's system time is Eastern Time and there's a mix of EST and EDT values because current date/time was captured using new Date() and saved to the database as a String value.

Unless the database in question has no DATE/TIMESTAMP type then that's not a very good choice of datatype for it...


I rest my case.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic