Solved

Joda Time problem with Time Zones

Posted on 2010-09-22
17
541 Views
Last Modified: 2012-05-10
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
Comment
Question by:Murali Murugesan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 3
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 33740564
what are the value of timeA and timeB
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33740607
time A = 1291046400000
time B = 1284307200000
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33740639
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
Industry Leaders: 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!

 
LVL 92

Expert Comment

by:objects
ID: 33740688
thats an old version of joda, have you tried running it on the latest?
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33740695
tried with 1.5 and its the same result... not working
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33740705
even with 1.6 nothing changed :(
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33741369
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
 
LVL 92

Expert Comment

by:objects
ID: 33741658
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33742147
Try
        Period period = new Period(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis(), PeriodType.hours());
        System.out.println("Period Hours --> " + period.getHours());

Open in new window

0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33743589
@CEHJ

It works with hours. Even days work in this.. but not for the default one...
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33743622
@objects,

 tz updater tool doesn't work either.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33752532
>>but not for the default one...

What do you mean by that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33763355
My last question not answered
0
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33766489
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 33769390
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
 
LVL 13

Author Comment

by:Murali Murugesan
ID: 33775313
@ 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 33777429
:-)
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question