Howto: comparing 2 dates and getting the seconds
By comparing 2 dates I need to extract or get somehow the elapsed seconds. I need to get only
the seconds.
Dates are in this format "hh:mm:ss"
I tried with Date and GregorianCalendar but I couldnt get the desired result.
Can someone provide me with a sample code please.
remus.dragos wrote:
Its not broken, nor absurd or complicated. I only need resolution only for minutes. The method that I use does not need to know more than 20 seconds.
But I added minutes knoledge aniway. As you can see its easy to add hour resolution if needed. But I didnt needed it so thats why I took that approach.
This looks cleaner for you? I just wrote it here, untested, but should work.
public int returnSeconds(Calendar c1, Calendar c2)
int mins = c1.get(Calendar.MINUTES) - c2.get(Calendar.MINUTES);
int secs = c1.get(Calendar.SECONDS) - c2.get(Calendar.SECONDS);
if (mins > 0) return secs+=mins;
else return secs;
}And why should I treat all the time as miliseconds since the epoch. Thats an absurd afirmation without a detailed explanation.
Edited by: remus.dragos on Apr 30, 2008 6:18 AM- Unnecessary creation of expensive Calendar objects.
- Significantly slower
- Breaks across Timezones
- Unnecessary decision making introduces complexity
- Introduces a bug by assuming c1 represents a time greater than c2
- Introduces a bug by not multiplying minutes by 60 to obtain seconds.
- Makes assumptions about the calendar system in use
- Less maintainable because it limits the scope of time unit resolution handled by the logic
I probably missed some more too. None of this is needed, and it is very brittle. Your problem can be solved by subtracting one fixed-point number from another. Go with es5f's approach.
Similar Messages
-
Compare 2 schemas and get the difference with .sql file.
Hi,
I am using ORACLE DATABASE 11g R2 and ORACLE Linux 5.
I want to perform a very lengthy process and want to make it automated.
I am having a software named as SVN. In which all the developers keep their updated scripts.
We have 2 schema's one is used for developement and when all the development seems good we implement the scripts on the final schema which is used by testing people also.
Now every day we need to check the modifications in the development schema, that we do by observing the updated scripts in the SVN software. Now we get the scripts which are modified.
We will fire this scripts to a sample schema then we will compare the objects in this schema and the same objects in our final schema. If the objects in sample schema is different than the final schema it should give me the
script to create the same type object in the final schema.
Below is a proper example to explain :-
Developement Schema :- 'DEV'
SVN Script Schema :- 'SVNscript'
Main Schema :- 'MAIN'
1) On Monday developers modified/added 4 tables in the development schema names 'DEV' after working for a day they found that the changes are necessary and so checked-in in the SVN software.
2) On tuesday morning we found 4 scripts which has been modified/added in the SVN.
The Scripts were as follows :-
1St table :- An extra column was added.
2nd table :- An index was created on it.
3rd table :- 2 Columns were dropped from it.
4th table :- A new table is added in the schema.
Now taking these 4 scripts i am going to execute it in a sample schema named :- SVNscript
SVNscript schema will have now 4 tables with proper properties(columns,indexes,...). Now this is the final table structure as we want in our 'MAIN' schema .
Coming to MAIN schema which is our most important schema and all the development work is finally posted here.Considering MAIN schema is having around 1000 tables,500 function/procedure/package and many more DB objects.
I want to compare(one way compare) from SVNscript schema --> MAIN schema :- That the objects present in the SVNscripts schema is same or not in the MAIN schema. If its not same then this code should generate a .sql script for me which i should be able to fire on the MAIN schema.
The output .sql scripts should be something like this :-
1st Table :- Alter table add...
2nd Table :- Create index on table...
3rd Table :- Alter table drop...
4th table :- Create table tablename...
I found a link while trying this but it is not the perfect fit to my requirement
http://www.dbspecialists.com/files/scripts/compare_schemas.sql
Please let me know the best code to compare 2 schemas and get a .sql file as output of the difference.
Thanks in advance.Yes I followed the tutorial properly this time. Still i have a few queries :-
1) This is returning me the 'ALTER TABLE...' statement but how to make it more efficient to return all the DDL dependent on the table, Like index,trigger,view...
2) I want to compare objects in a different schema on a different db. I have created an DBLINK but how to use it ... can i use it like
DBMS_METADATA.OPEN('TABLE'," Network_link_name " );Can you please give me some clear about it.
3) I created a table 'TAB1' with which i want to compare a 'TAB1' in other schema.I was not getting how to compare on remote dblink so,I created the same table with the following code in the same schema
CREATE TABLE TAB1_OLD as select * from TAB1@DBLINK where 1=2;Now when i fire the below query to get the alter difference I get the following error :-
SQL> SELECT get_table_alterddl('TAB1','TAB1_old') FROM dual;
SELECT get_table_alterddl('TAB1','TAB1_old') FROM dual
ORA-31603: object "TAB1_old" of type TABLE not found in schema "SVNCHECK"
ORA-06512: at "SYS.DBMS_METADATA", line 5225
ORA-06512: at "SYS.DBMS_METADATA", line 5189
ORA-06512: at "SVNCHECK.GET_TABLE_SXML", line 17
ORA-06512: at "SVNCHECK.COMPARE_TABLE_SXML", line 12
ORA-06512: at "SVNCHECK.GET_TABLE_ALTERXML", line 11
ORA-06512: at "SVNCHECK.GET_TABLE_ALTERDDL", line 11
Can you please guide me why i am getting this error?
I have made sure that TAB1_OLD table has been created and the entries are also present in the data dictionary tables.
SQL> select * from tab1_old;
EMPNO ENAME MGR DEPTNO
----- -------------------- ----- ------The only difference is that this table was created with CREATE TABLE AS SELECT statement ....
Thanks. -
Function to compare tow dates and set the value
Hi All,
I have two dates with close date and Due date.And i want to set the value as 1 if close date is less then or equal to to due date else 0 .And my other condition is to set 1 when close date is greater the due date.Can any one help me how can I
achieve it.
Thanks, SidHello Sid,
For this you can use a simple IF Statement with a compare on the Dates:
= If(Tabelle1[CloseDate] <= Tabelle1[DueDate]; 1; 0)
= IF(Tabelle1[CloseDate] > Tabelle1[DueDate]; 1; 0)
Olaf Helper
[ Blog] [ Xing] [ MVP] -
How to Check the date and get the latest date?
Hi Everyone,
I've an internal table which holding few records which contain date field. How I can check this internal table by getting the record which contain "latest date" only? Kindly advise. Thank you.
eg:
I should only get the record which contain lastest date. (eg: I should picked up Record3)
Table i_test
Field 1 Field 2(date)
Record1 20090322
Record2 20090101
Record3 20090601
Moderator message - Please search before asking and do not ask basic questions - post locked
Edited by: Rob Burbank on Jun 17, 2009 9:21 AMIts easy...sort the internal table using the field i.e. date field in DESCENDING manner
SORT gt_final BY date_field DESCENDING.
Read the first entry of the internal table..it will have what you require
READ gt_final INDEX 1. -
Substacting two dates and get result in months
Hi Friends
I have to subtract two dates and get the difference in months. Here's the sample data:
select DATE '2009-10-01' Date_value, 111 Cust_id, DATE '2009-10-22' Open_date from dual
union all
select DATE '2009-10-01', 112 , DATE '2009-10-15' from dual
union all
select DATE '2009-10-01', 113 , DATE '2009-10-5' from dual
union all
select DATE '2009-11-01', 111 , DATE '2009-10-22' from dual
union all
select DATE '2009-11-01', 112 ,DATE '2009-10-15' from dual
union all
select DATE '2009-11-01', 113 ,DATE '2009-10-22' from dualAnd here’s my query to get the difference in month:
SELECT cust_id, date_value, open_date,
ROUND (ABS (date_value - open_date) / 365 * 12) diff_in_months
FROM tBut the output is unexpected:
CUST_ID DATE_VALUE OPEN_DATE DIFF_IN_MONTHS
111 1/10/2009 22/10/2009 1
112 1/10/2009 15/10/2009 0
113 1/10/2009 5/10/2009 0
111 1/11/2009 22/10/2009 0
112 1/11/2009 15/10/2009 1
113 1/11/2009 22/10/2009 0Ideally, the output should be as per the following:
CUST_ID DATE_VALUE OPEN_DATE DIFF_IN_MONTHS
111 1/10/2009 22/10/2009 0
112 1/10/2009 15/10/2009 0
113 1/10/2009 5/10/2009 0
111 1/11/2009 22/10/2009 1
112 1/11/2009 15/10/2009 1
113 1/11/2009 22/10/2009 1What is wrong in my query?Hi Centinul
Found the resolution. When I use the following query, I get the perfect result:
SELECT cust_id, date_value, open_date,
ROUND (ABS (MONTHS_BETWEEN (open_date, date_value))) months,
ROUND (ABS (date_value - open_date) / 365 * 12) diff_in_months,
MONTHS_BETWEEN (date_value,ADD_MONTHS (LAST_DAY (open_date), -1) + 1 ) final_val
FROM tAnd here's the output of all three columns:
CUST_ID DATE_VALUE OPEN_DATE MONTHS DIFF_IN_MONTHS FINAL_VAL
111 1/10/2009 22/10/2009 1 1 0
112 1/10/2009 15/10/2009 0 0 0
113 1/10/2009 5/10/2009 0 0 0
111 1/11/2009 22/10/2009 0 0 1
112 1/11/2009 15/10/2009 1 1 1
113 1/11/2009 22/10/2009 0 0 1 -
Date fields and compare and get the later date of the fields
I am trying to compare these date fields and compare and get the later date of the fields
Tables are
TABCASER
TABCASER1
EVCASERS
Field
Are
TABCASER1.CASER_no
The dates are to be compared and then get the records with the highest or latest date value.
TABCASER1.CASERRECIEVEDDATE
EVCASERS.FINALEVDATES
EVCASERS.PUBLICATIONDATE
EVCASERS.PUBLICATIONDATE
TABCASER.COMPAREACCEPDATE
I have this code but I am trying to figure out what it all means.
I have several questions.
1.
1. greatest it is used here to compare right? How do I then output this ? do I store it to a var (coldfusion) ultimately , I wish to send it to a page of records
2. is it necessary to use todate? And to_date? What does this do?
3. decode, is this necessary too. What does this do? NULL?
4.
5.
6. when I do get the query results how do I send it to coldsuion and out put to a display.
Someone sent me this code.
is there abetter way of doing this? To compare the dates and store in a var to display. thanks
Here is my code below:
Greatest(
CASE
WHEN INSTR(TABCASER1.CASER_no,'-CE') > 0 THEN
decode(TABCASER1.CASERRECIEVEDDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),TABCASER1.CASERRECIEVEDDATE)
WHEN INSTR(TABCASER1.CASER_no,'-ERNIE') > 0 THEN
decode(EVCASERS.FINALEVDATES,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),EVCASERS.FINALEVDATES)
WHEN INSTR(TABCASER1.CASER_no,'-MONIE') > 0 THEN
decode(EVCASERS.PUBLICATIONDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),EVCASERS.PUBLICATIONDATE)
WHEN INSTR(TABCASER1.CASER_no,'-NADINE') > 0 THEN
decode(EVCASERS.PUBLICATIONDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),EVCASERS.PUBLICATIONDATE)
ELSE
decode(TABCASER.COMPAREACCEPDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),TABCASER.COMPAREACCEPDATE)
END
,decode(TABCASER.COMPAREACCEPDATE,NULL,TO_DATE('01/01/1900','mm/dd/yyyy'),TABCASER.COMPAREACCEPDATE))
between TO_DATE('#dateformat(form.startDate,"mm/dd/yyyy")#','MM/DD/YYYY') and TO_DATE('#dateformat(form.endDate,"mm/dd/yyyy")#','MM/DD/YYYY')
is there abetter way of doing this? To compare the dates and store in a var to display. thanksHi
If you have date datatypes than:
select
greatest(TABCASER1.CASERRECIEVEDDATE, EVCASERS.FINALEVDATES, EVCASERS.PUBLICATIONDATE, EVCASERS.PUBLICATIONDATE, TABCASER.COMPAREACCEPDATE)
from TABCASER, TABCASER1, EVCASERS
where ...-- join and other conditions
1. greatest is good enough
2. to_date creates date dataype from string with the format of format string ('mm/dd/yyyy')
3. decode(a, b, c, d) is a function: if a = b than return c else d. NULL means that there is no data in the cell of the table.
6. to format the date for display use to_char function with format modell as in the to_date function.
Ott Karesz
http://www.trendo-kft.hu -
I purchased an iPad mini on October 10th. I want to return it and get the new ipad mini retina when it comes out. I am currently still within the 14 day return/exchange window but how can I exchange it if the new ipad mini release date isn't until "later in November", which would be beyond the 14 days from my purchase date? If apple announces a new version of a product you just purchased but won't actually release it for more than 14 days, how can you possibly exchange the one you just bought? Is my only option to return the one I bought on the 10th (so return by 10/24) and then not have an iPad until the new one is available for purchase? Or would it make any sense to return the one I bought, get a new one, which would presumably have another 14 day return window, and then exchange THAT one for the new ipad mini w/ retina when it comes out (hopefully within that second 14 day window)?
Call the apple store you got it from and ask them. Sometimes in the past they've extended the 'no questions asked return' but only Apple can tell you for sure.
-
How can I get the second group and report totals in Matrix report?
Hi,
I have created a matrix report with 2 break Groups.
Iam getting columns totals for the first group, but not for the second group and the report totals.
How can I get the second group and report totals for each month?
Thanks.
Ram.Hi Ram,
If you want a total at any group-level, go to the particular group in the data model, insert a summary column in the group, and select
Reset At > appropriate Group Name
For report level summaries, you must create the summary column outside of all groups.
Navneet. -
I downloaded and installed Airport Express Utility 5.6 to get my old Airport Express to work with the latest Mac OSX 10.7.4, but when I install and run it, it still says my Airport Express is out-of-date and needs the 5.6 utility. What's up?
It is likely that you downloaded AirPort Utility 5.6.1. If you checked the requirements, this only runs on Leopard or Snow Leopard Macs.
You have Lion, so you need AirPort Utility 5.6 for Mac OS X Lion -
I recently tried to update my iPhone 3 software via my mothers Mac book comp - during the upgrade I lost all my music and calendar dates and now the iTunes on my phone has my mothers iTunes detail - how do I get all my info back on the phone?
Restore it using the computer you normally sync with.
-
What a have is a VI that uses the following SUbvi's, starts with FP OpenVI, then FP Create TagVI, into a "While Loop" which contains a FP ReadVI outputting data into a Index ArrayVI outputting to a Display (DBL). This shows the output of a FP-AI-100 monitoring a 9v battery. I have to monitor this battery for a 4 hour period my problem is storing the 4 hours of data and getting it out of the "while loop" into a "Write to Spreadsheet File VI" all I seem to accomplish is just one data sample which I get into a spreed ship file with no problem. I just can't get 4 hours worth. By the way this is my first VI and I'm self
trained so have mercy.I figured it out thanks.
John Morris
Glendinning Marine -
What a have is a VI that uses the following SUbvi's, starts with FP OPENvi, then FP Create Tagvi, into a "While Loop" which contains a FP READvi outputting data into a INDEX ARRAYvi outputting to a Display (DBL). This shows the output of a FP-AI-100 monitoring a 9v battery. I have to monitor this battery for a 4 hour period my problem is storing the 4 hours of data and getting it out of the "while loop" into a "Write to Spreadsheet File vi" all I seem to accomplish is just one data sample which I get into a spreed ship file with no problem. I just can't get 4 hours worth. By the way this is my first VI and I'm sel
f trained so have mercy.I figured it out Thanks.
John Morris
Glendinning Marine -
Hi All,
I have 2 tables
table1: col1 sequenceid, col2 code
table2:col1 studentID , col2 code, col3 joindate
in table 1 i have values as
1 Q
2 W
3 E
4 R
5 T
6 Y
Table2:
A1 Q
1-04-2015
A1 W
2-04-2015
A3 Q
1-04-2015
A2 Q
1-04-2015
A3 W
2-04-2015
A2 W
2-04-2015
A2 E
2-04-2015
A3 E
2-04-2015
A1 E
2-04-2015
A2 R
2-04-2015
Now i want ouput as
A3 E
A2 R
A1 E
I want to group the values in table2 based on studentid order by the sequence in the table1 and get the latest which is having higher sequence (Note: the date field cannot be used to order the values)from @table2 a inner join @table1 b on a.col2=b.col2 collate database_default)
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
How can I wait till data is loaded and get the latest entities in HTML CLient
This is my first try using a JavaScript component. I found a nice timeline-Scheduler on github:
Timeline Javascript Scheduler
I made an nearly empt new Screen and I am using LS only to get the data and to switch to the next date.
This works fine for the first date I set in the created.
But if I change to the next day, there are still the just loaded records. When I switch again to the following date I get the records from the day before...
myapp.dispo.divcontainer_render = function (element, contentItem) {
// We need this div for the timeline
$(element).append('<div class="calendar"></div><div class="realtime-info"></div>');
//Bind to the date
contentItem.dataBind("screen.suchDispodat", function (value) {
var querydat = value;
var items = [];
var sections = [];
//The querydat is set to the right date but the records I get are from the last executed query.
contentItem.screen.getQuerydispobydatfahrerorderfahrer(querydat).then(function (result) {
var dispos = result.data;
dispos.forEach(function (dispo) {
How can I bind the refresh to something like data loaded?
HelmutFrom:
http://blogs.msdn.com/b/lightswitch/archive/2012/12/19/enhancing-lightswitch-controls-with-jquery-mobile-michael-zlatkovsky.aspx
"You could use setTimeout (which is sometimes necessary – see Joe’s Custom Controls post
for an example)"
Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book
http://LightSwitchHelpWebsite.com -
Get line that contains and then get the second and fourth line after that.
So I have a text file and I want it to find all the lines in the file that contain an @ symbol and then I want it to get that line and the line second and fourth from that line. Then I would export it like this:
$linewith@ + ":" + $secondline + ":" + $fourthline
Finding the lines with an @ symbol is easy, but how would I tell it to get the second and fourth line after the line with an @ symbol?
So I would get the lines with something like this:
foreach ($line in [System.IO.File]::ReadLines($filename)) {
if($line.contains("@")){}That worked perfectly thanks man! Here is the entire code. I'm sure it could been cleaned up. I'm still learning powershell.
$username = "Username"
$password = "Password"
$url = "http://URL"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$false
$ie.navigate($url)
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("username").value = "$username"
$ie.document.getElementById("password").value = "$password"
$ie.document.getElementById("click").click()
start-sleep 20
$ie.Document.body.innertext | Out-File -FilePath c:\web.txt
$links = $ie.Document.getElementsByTagName('A')
$logout = $links | where {$_.innerText -eq 'logout'}
$logout.click()
start-sleep 5
(gc C:\web.txt) | ? {$_.trim() -ne ""} | set-content C:\web.txt
( Get-Content C:\web.txt | Select-Object -Skip 2 | ForEach-Object { $_.TrimStart( " " ) } ) | Set-Content C:\web.txt
start-sleep 5
$fileContents = Get-Content C:\web.txt
foreach ($line in $fileContents) {
if ($line -like '*@*') {
$line + ":" + $($fileContents[$line.ReadCount+0]) + ":" + $($fileContents[$line.ReadCount+3]) | Out-File -FilePath c:\finish.txt -Append
Maybe you are looking for
-
Cycle Counting (Ref: Q3-4)
Dear Sirs, In Cycle counting, how to save ABC analysis for a material ? / How to choose the function of ABC analysis ? Many Thanks, KH Fong
-
How do I share photos from iPhone to Mac over wifi?
I have a 2014 MacBook Air newly upgraded to Yosemite, and a 5S also from earlier this year. I would like to send files (photos) from my own phone so they are files -- rather than sending them to iPhoto. I have been trying to figure out how to do this
-
After Windows XP reinstall....
I tried searching the forum but merely became more confused! I have a 30gig ipod (purchased around dec '04)which is currently full of songs and play lists. After formatting my pc I am unaware of how to restore the song files (including purchased song
-
3G will not connect to any wi-fi, secured or unsecured
I'm cheap, I know, but I still have an iPhone 3G. Suddenly one day, it decides it will not join any wi-fi network whether it's secured or not. If unsecured, I get "Cannot join network." At home, I have an AT&T 2Wire router that successfully connects
-
Lightroom 5 Download file faulty
Downloaded a trial of lightroom two days ago, but it won't work. I have only just bought a Mac so do not understand how it works so looking at the work arounds is useless. If I leave downloading again for a few day is this fault going to be rectified