• 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

Help with setting the time to a Date

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to add a day and set the time to the midnight of the added day.

My Code....


My Output



Why is the time not setting to MidNight. Please help!!

Thanks in Advance
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using the get milliseconds method, you may be changing the time zone.
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell - I am sorry, I did not understand your statement
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using milliseconds, which count how many milliseconds have passed since 1st January 1970, not the time shown on the clock in your office. Now, you set a date for 29th October in West Brazil (print it out to verify its details before line 20). Now you extract the milliseconds, which are 23:59:59 West Brazilian time. Then you create a Timestamp object, not for 23:59:59, but for however long that has been since 1/1/1970. Is that Timestamp object going to be in West Brazilian time? I do not know. Maybe it is in GMT. Now, if 23:59:59 in West Brazil is 9:29:59 in Britain, it suggests West Brazil is in a time zone 9½ hours west of Britain.

What you are doing is choosing a second when it is 23:59:59 where you are. But the Timestamp may interpret that time as GMT/UTC, so you get 9:29:59 on 30th October.

Anybody else like to explain it better than I did?
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your explanation is quite clear to me. Date calculation is always iffy. My heart's been skipping a beat everyone I hear someone asking for date calculations ever since a project manager tried to strongarm me into adding 3600000ms to a Date so it would be in the next time zone and I had to go over his head and jeopardize my job to prevent it. Masters of Science who've had basic java and think they know development. Dangerous.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:Your explanation is quite clear to me. . . .

It was clear to me, too (thank you:wink:‍) but the important thing is whether it is clear to Hrithik khurana.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to this site, Brazilian time is only up to 4 hours west of GMT. Maybe HK is using a time zone 5½ hours east of GMT.
 
Kishen Singh Punjabi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome and I hope our explanations helped
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is with the constuction of the timestamp on the last line:
Look at the javadocs for this constructor:


Constructs a Timestamp object using a milliseconds time value. The integral seconds are stored in the underlying date value; the fractional seconds are stored in the nanos field of the Timestamp object.

Parameters:
time milliseconds since January 1, 1970, 00:00:00 GMT. A negative number is the number of milliseconds before January 1, 1970, 00:00:00 GMT.


The important bit here is the number of milliseconds before January 1, 1970, 00:00:00 GMT

As you have specified a timezone of "Brazil/West", the value of cal.getTimeInMillis() is 1383105599000. If you change the timezone to "GMT", the value of cal.getTimeInMillis() is 1383177599000.

This explains why the output is not 2013-10-30 23:59:59.0
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hrithik khurana wrote:Why is the time not setting to MidNight. Please help!!


It is setting it to midnight, but midnight in the timezone Brazil/West is not midnight in your local timezone. Apparently, the time is 09:29:59 in your local timezone when it is 23:59:59 in the timezone Brazil/West.

If you want to see the time in the Brazil/West timezone, you need to format it with a DateFormat object that is set to that timezone. Note that a Timestamp object by itself doesn't have (explicit) timezone information. If you print a Timestamp object directly, it will be formatted with a default date formatter that will show it in your local timezone, so the time it will show is not midnight if your local timezone is not Brazil/West.

reply
    Bookmark Topic Watch Topic
  • New Topic