File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Strange problem in date calculation (IS IT BUG!!!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strange problem in date calculation (IS IT BUG!!!)" Watch "Strange problem in date calculation (IS IT BUG!!!)" New topic
Author

Strange problem in date calculation (IS IT BUG!!!)

Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
i am surprise to see it .



the output is Sun Mar 08 01:30:00 PST 2009 , but when i try this



the output is Sun Mar 08 03:00:00 PDT 2009

WHY this is happening when i am adding only 30 minute .
I need help. It's urgent

Regards
Arshad
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
You're not adding anything, you're setting something. Maybe you meant to do something like
cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 1800)


[ UD: Edited to reflect the fact that 1800 seconds is not actually 3 hours, but 30 minutes. What was I smoking? ]


Ping & DNS - my free Android networking tools app
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are not adding time to your Calendar, you are setting the second property to be 1800 seconds. If you want to add time use the add method. (see the JavaDocs)


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Its working on my system, My local is India, and its showing in IST format.

When I set SECONDS field by 1800, it shows

Sun Mar 08 15:30:00 IST 2009

The problem may be the 24 hour clock settings


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
I seem to be suffering from foot-in-the-mouth disease. But anyways here it goes.
It works fine for me.


Sun Mar 08 09:30:00 GMT 2009
Sun Mar 08 10:00:00 GMT 2009


Effective 2007, the local time changes from PST to PDT at 02:00 LST to 03:00 LDT on the second Sunday in March and returns at 02:00 LDT to 01:00 LST on the first Sunday in November, except in Mexico, where the previous dates are still in effect.


In your example, 8th March is the second sunday, so adding 30 min makes it 2:00 PST which is actually 3:00 PDT due to daylight offset. If you notice your output, it changes from PST to PDT.

And Ulf, 1800 seconds is 30 min.
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
it is not working
take this example

i set the time Sunday, March 08, 2009 1:00:00 AM and add 1800 seconds it gave me
Sun Mar 08 01:30:00 PST 2009 , but when i set date to Sunday, March 08, 2009 1:30:00 AM
and add 1800 seconds it gave me Sun Mar 08 03:00:00 PDT 2009 . i need help really.

Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
so wat is the solution my friend
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Arshad,

I think my post explains your output. The solution will depend on what you want the output to be.
If you let it be, it will be in PDT. If you dont want it so, You probably should do some formatting youself.

Otherwise, a simple message could be passed to the user alerting him about the Time change.
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
give me solution in proper code my friend
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
give me solution in proper code my friend


Um, Like i said, the solution would depend on what you want. And you should probably rephrase your post before CR or the other bartenders tell you to do so.
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
ok i try
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
i tried this and other thing too but no result



any help
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Which version of the JDK are you running this in?

Also, why are you using the deprecated three letter timezone labels?
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
1.6 update 10 so what shall i use
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Just what is it that you are expecting.

As discussed by Arshad, this is working as expected (at least for us).

Please elicitate what you are expecting and what you want as a final result.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

Arshad Ali wrote:i tried this and other thing too but no result

What exactly does "no result" mean? What did you expect to get, and what did you really get - and what's exactly the difference between what you expected and what you really got?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
Steve, actually i am using long date in date calculation and passing to mysql DB but i also have to display it properly.
have you any idea?
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Could you please be specific and let us know what you mean by properly?

thanks.

in my opinion, how it is displayed in your original posting is proper.

-steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

Arshad Ali,

People here don't understand what you think is wrong. Simply stating "the output is wrong" doesn't help anyone. You have to tell us EXACTLY what output you are seeing, AS WELL AS what output you EXPECT to see.

Without BOTH those pieces of information, NOBODY CAN HELP YOU.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
ok, i am using long numeric date in application and performing calculation. now i want to convert date fron PDT to PST format but it is not working.

Regards,
Arshad
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

I will repeat:

What is the output you see, and what output do you expect to see? Why is this wrong?
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
So you want to see

Sun Mar 08 02:00:00 PST 2009

instead of

Sun Mar 08 03:00:00 PDT 2009

That is, you do not want to take the daylight offset into account. Am i right?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Arshad Ali wrote:ok, i am using long numeric date in application and performing calculation. now i want to convert date fron PDT to PST format but it is not working.

Regards,
Arshad


If PST stands for what I think it does (Pacific Standard Time) then PDT would be Pacific Daylight Time, which is converting the time for the Daylight Savings Time adjustment. If your going to be doing addition on times that cross that 2 AM time period that Sunday you are going to hit the problem. If you convert PDT to PST (which I'm not sure can be done with the date being the DST transition) then the time your program outputs will not be the correct time. If you are really trying for it, you could write an if statement so if the dates are in between the start of DST and the end of DST, subtract one hour from the time. Other than that, however, I don't know that you can force the program to run PST instead of PDT, since it seems like PST is the same as PDT when PDT is in effect.

If that made no sense to anyone else please say so and I will try to explain more clearly.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
the problem is i try to convert from PDT to PST but no result , so i think i need to check if date is in daylight time then perform calculation. let's see.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange problem in date calculation (IS IT BUG!!!)