aspose file tools*
The moose likes Beginning Java and the fly likes Date  Conversion query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date  Conversion query" Watch "Date  Conversion query" New topic

Date Conversion query

Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Hi! Folks,
I have time in milliseconds,need to convert it into no-of day's.
My code :
import java.util.*;
public class dat
public static void main(String args[])
// Calendar cd=Calendar.getInstance();
Date dt=new Date();
long msec=dt.getTime();
Basically i am trying to get the difference between Sysdate and any previous date.
...What Next,...please help
[ December 17, 2002: Message edited by: Pravin Palia ]
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

There are 86400000 milliseconds in a 24-hour day; why not just take the difference and divide?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
If your time in milliseconds comes from a difference between two timestamps, before you divide, add one hour (3600000 miliseconds) to your miliseconds to round the results. This will avoid the common fractional problem that comes with the Daily Saving Time.
Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Thank's for the answer,
What i meant was i am not able to put the actual CODE which would subtract any two given dates.
Thank You Once again.
"If A is success in life, then A equals X plus Y plus Z - where work is X, Y is play, and Z (YES but people these days take undue advantage if one keeps his mouth shut...praven.)is keeping your mouth shut." - Albert Einstein
[ December 18, 2002: Message edited by: Pravin Palia ]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Consider exactly what you want to do. What if you're looking at the difference between 11 PM (2300) today and 1 AM (0100) tomorrow? That's just two hours, so it shold probably be rounded to 0 days. But for many business applications, you would want to call it a 1-day difference. Conversely, between 1 PM and 3 PM the same day, there's also two hours - but you may want to call that a 0-day difference. Some other businesses might wish to make a distinction not for midnight, but for close-of-business at 5 PM or some other time. So for some applications, the difference in milliseconds between two times is not enough information. If this is the case for you, you'll probably have to use a Calendar object (GregorianCalendar most likely) to examine the specific fields which are of interest, according to your business needs.
[ December 18, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Hi! Again ,
This is what i was able to do,Although i am still using a deprecated "Date" class function,but nevertheless i am able to get the Result.

-------Code Start's Here..
public static void main(String args[])

String sdate1 = args[0];
String sdate2 = args[1];
Calendar cal = Calendar.getInstance();
// sdate11 = java.text.DateFormat.getDateInstance().format(sdate1);
Date date1=new Date(sdate1);
Date date2=new Date(sdate2);

int cal11 = cal.get(Calendar.DATE);
System.out.println("1 "+cal11);
int caldt1 = cal.get(Calendar.DATE);
System.out.println("3 "+caldt1);
int caldt2 = cal.get(Calendar.DATE);
System.out.println("4 "+caldt2);
int caldt3 = 0 ;
caldt3 = caldt2-caldt1 ;
caldt3 = caldt1-caldt2 ;
System.out.println("no. of days = "+ caldt3);
---------Code Ends Here
Now The Only thing is Can you help me in replacing the deprecated class function.
Thank's In advance.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
In general - compile using the -deprecated option to get information about exactly which methods or constructors are deprecated. (It's not the whole Date class - just certain parts of it.) Consult the API for those methods or constructors to see suggestions about what to replace the deprecated call with.
In this specific case, you may actually find it's easier to create a SimpleDateFormat and use the parse() method to replace the deprecated code. (Unfortunately this isn't suggested in the API, but it should be.)
Also, try testing what your code does if you use the following pairs of dates:
Nov 30, 2002
Dec 1, 2002
Dec 31, 2002
Jan 1, 2003
Feb 28, 2003
March 1, 2003
Feb 28, 2004
March 1, 2004
Do you get the correct results? (Are you sure?) If not, can you find other methods in Calendar which will help?
[ December 19, 2002: Message edited by: Jim Yingst ]
Navi Singh
Ranch Hand

Joined: Jul 24, 2002
Posts: 80
Hi! ,
Nope they don't.And i knew that,my main concern was not to use deprecated methods/function.
It was just a tutorial kind of q.I shall make the changes to incorporate any dates(I will try)
Thank's for all the answer.
It is sorta covered in the JavaRanch Style Guide.
subject: Date Conversion query