on 11-06-2013 3:32 PM
Hi All,
we have issue with the dayligh saving time.
i need to calculate the time difference(in Seconds) between the starttime and endtime.
we have starttime and endtime in the input and both recieve the value in the format "yyyy-MM-dd' 'HH:mm:ss"
i have written the UDF as below:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
Date d1 = sdf.parse(time1);
Date d2 = sdf.parse(time2);
long l1 = d1.getTime(); " converts into milliseconds
long l2 = d2.getTime();
if (l1 > l2){
l2 = l2 + (1000 * 60 * 60 * 24);
}
long diff = (l2 - l1) / 1000; " Convert into Seconds
result = "" + diff;
for the above code the result for the below input values(normal & daylight) the time diff should be 8 hours(28800 seconds).
Code is working for the normal time but for the daylight saving time i getting 9 hours(32400 seconds) as difference.
i need to get the 8 hours diff for the daylight saving time as well.
Normal Time:
time1 = 2013-04-05 22:00:00
time2 = 2013-04-06 06:00:00
result = 28800 " seconds
daylight saving time:
time1 = 2013-04-06 22:00:00
time2 = 2013-04-07 06:00:00
result = 32400 "seconds
i have also tried with below method to get the time in milliseconds , but the result is same for the daylight saving input values i am getting 9 hours diff
Calendar cal = Calendar.getInstance();
cal.getTimeInMillis();
please help me on this.
Thanks
Sai
Some more inputs please...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sai
Try this code
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
long diff = 0;
try{
Date d1 = sdf.parse(time1);
Date d2 = sdf.parse(time2);
Calendar c = Calendar.getInstance();
c.setTime(d1);
int offset1 = c.get(Calendar.DST_OFFSET);
c.setTime(d2);
int offset2 = c.get(Calendar.DST_OFFSET);
long l1 = d1.getTime();
long l2 = d2.getTime();
if (l1 > l2){
l2 = l2 + (1000 * 60 * 60 * 24);
}
if ( offset1 == 0 | offset2 == 0 )
{
diff = (l2 - l1) /1000;
}
else
{
diff = (l2 - l1) /1000-3600;
}
}
catch(Exception ex)
{
}
return String.valueOf(diff);
This will give u the desired result.
Hi Sai,
can you please let us know which time your SAP system referring to, if it is GMT; generally the clock change will happen on every last Sunday of October.
i have also tried with below method to get the time in milliseconds , but the result is same for the daylight saving input values i am getting 9 hours diff
on that particular day you will get 1 hour time difference from normal time.
you can handle it by getting the clock change date.
Hope this helps!
Thanks,
Srikanth
Hi Indrajit,
i have tested with the given code ,but same result
i have tested with several options , finally DST is getting executed successfully by changing the TimeZone
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
String result = "0";
try {
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
cal.setTime(sdf.parse(time1));
cal1.setTime(sdf.parse(time2));
long l1 = cal.getTimeInMillis();
long l2 = cal1.getTimeInMillis();
if (l1 > l2) l2+= (1000 * 60 * 60 * 24);
long diff = (l2 - l1) / 1000;
result = "" + diff;
} catch (ParseException e) {
}
return result;
please let me know if you find and differnce in the above code.
Regards
Sai
Hi Sai
I have used the below line to check the DST is active or not.
int offset = c.get(Calendar.DST_OFFSET);
The offset will be 0 if there is no DST else it will return the time difference in miliseconds.
Therefore I have used the below condition
if ( offset1 == 0 | offset2 == 0 )
{
diff = (l2 - l1) /1000;
}
else
{
diff = (l2 - l1) /1000-3600;
}
so when DST is on , you are subtracting 1 hour more (3600) in difference so that the output will be 8 hours.
This section u have not used in your code.
Can you try by setting the timezone?
String userTz = “America/Los_Angeles”;
sdf.setTimeZone(TimeZone.getTimeZone(userTz));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
11 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.