Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Joda Time problem with Time Zones

Hi Experts,

I m having this piece of code, i use Joda time 1.4jar and the code produces wrong results for one time zone and the correct results for other time zone.

Any ideas why? Attached code

Calendar startCalendar = Calendar.getInstance();
		      Calendar endCalendar = Calendar.getInstance();
		    
		      SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
		      try{
		    	  startCalendar.setTime(sdf.parse("Mon Sep 13 00:00:00 GMT+08:00 2010"));	    	  
		    	  endCalendar.setTime(sdf.parse("Tue Nov 30 00:00:00 GMT+08:00 2010"));
	      
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }

 long timeA = endCalendar.getTimeInMillis();
		      long timeB = startCalendar.getTimeInMillis();
		      int daysInYear = startCalendar.getActualMaximum(Calendar.DAY_OF_YEAR);

  Period period = new Period(timeA - timeB,PeriodType.days());
		      System.out.println("Period Hours --> "+ period.getHours());
		      System.out.println("period.getDays()=[" + (period.getDays()+1) +']');

Open in new window


period.getDays() appear to be 0.

-Murali*
0
Murali Murugesan
Asked:
Murali Murugesan
  • 9
  • 5
  • 3
1 Solution
 
objectsCommented:
what are the value of timeA and timeB
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
time A = 1291046400000
time B = 1284307200000
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
Period period = new Period(timeB,timeA,PeriodType.days());

Changing the code as this works.. But the problem is without changing any code and when executed from a US time zone the first code works.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
thats an old version of joda, have you tried running it on the latest?
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
tried with 1.5 and its the same result... not working
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
even with 1.6 nothing changed :(
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
ok finally got some insight by getting into the source code. My Time Zone value has a problem.
Its Asia/Singapore, but for some reason the Joda doesn't consider this as a valid time zone for converting milliseconds into days.

By setting the Timezone as Mexico or China , i can make the code work without any other changes.

0
 
objectsCommented:
0
 
CEHJCommented:
Try
        Period period = new Period(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis(), PeriodType.hours());
        System.out.println("Period Hours --> " + period.getHours());

Open in new window

0
 
Murali MurugesanFull stack Java developerAuthor Commented:
@CEHJ

It works with hours. Even days work in this.. but not for the default one...
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
@objects,

 tz updater tool doesn't work either.
0
 
CEHJCommented:
>>but not for the default one...

What do you mean by that?
0
 
CEHJCommented:
My last question not answered
0
 
Murali MurugesanFull stack Java developerAuthor Commented:
new Period(timeA - timeB,PeriodType.days())

this the default constructor that we are using across our code. I was mentioning that this was not working.

new Period(a,b,[days,hours,months]); -- etc... seems to work correctly. But new Period(a,[days]) has a problem, since it does some evaluation based on the local time zone.

-Murali*

0
 
CEHJCommented:
It's nothing to do with timezones - it's to do with how you use the API (not the most intuitive). You should fine the following works fine:

	Period period = new Period(timeA - timeB,PeriodType.standard());
	System.out.println("Period Std Hours --> "+ period.toStandardHours().getHours());
	System.out.println("Period Std Days --> "+ period.toStandardDays().getDays());

Open in new window

0
 
Murali MurugesanFull stack Java developerAuthor Commented:
@ CEHJ:

That worked perfectly. But the problem is we are using JODA 1.4 and the one you shown above is from JODA 1.5.

I have a given a try with 1.5 and it works perfectly.

Thanx
-Murali*
0
 
CEHJCommented:
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now