• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert Date String To Date Object

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this question has been asked many times before, but I can't seem to be able to ge my head wrapped around this one.

I have a String such as this


that I am trying to convert to a Date Object with this code below:



Unfortunately my output of the Date object is this



my objective here is to have a Date object from that original String. That Date Object should have the same parameters as the date string. In this case, the original hours of day (13) is turned to 15, but the desired is for it to stay at 13. I need this because in my program I will need to compare two different date objects.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
13:51:35 UTC is 15:51:35 CEST. Everything works fine.
Also, note that Date object doesn't carry any information about time zone.

You might try this:
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two formatted versions you displayed there represent the same time, even though they look different. They just display the date in different time zones ("UTC" versus "CEST").

Don't confuse a formatted version of an object with the contents of the object; there are often many ways to format one set of data.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Don't confuse a formatted version of an object with the contents of the object; there are often many ways to format one set of data.



Indeed, and what's actually inside an object may change from one version of the object's class to another, even if the formatted version of an object's contents remains the same from one version of the object's class to another. That's yet another reason why it is better to use getters and setters than it is to make class member variables directly accessible.

For example:



You can see how the area method returns a value it computes, rather than stores. But a later implementation of that class might look like this:

While the usefulness of this class might be slight, you can see how, if the only attribute of a Rectangle object your code needs to obtain is its area, then either implementation works. The first one, however, calls for a computation on each query (the compiler might be smart enough to notice that the calculation is computed entirely from final member variables, and only do it once, but it might not, and it might not behave the same way on all platforms). The second one only does the computation once, when the object is constructed. That might be a worthwhile performance enhancement.

But, the point is, code that creates Rectangle objects and that queries them for their area neither knows nor cares how those objects store their values internally. That's as it should be. Your code should only rely on the interface available through a class's methods, and remain blissfully unaware of any internal details.

For C/C++ programmers, that can take a little getting used to, but I have found it's worth it.
 
Davide Scrimieri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the clarification. It was really helpful!
reply
    Bookmark Topic Watch Topic
  • New Topic