Solved

Date problem!!!!!!Plz help.....

Posted on 2002-04-30
4
195 Views
Last Modified: 2010-03-31
Hi All,
I know that the Date class has 2 constructors:-
1- Date(long date)
 Constructs a Date object using a milliseconds time value.
 (Parameters:
  date - milliseconds since January 1, 1970, 00:00:00    
  GMT.A negative number indicates the number of
  milliseconds before January 1, 1970, 00:00:00 GMT.
 )
2- Date(int year, int month, int day)
 Constructs a Date object initialized with the given year, month, and day.
 (Parameters:
      year - year-1900
      month - 0 to 11
      day - 1 to 31
 )

If i have the number of days of some date but from 1900..
Example:-
The number of days from 1/1/1900 to 27/2/1978 == 28548 days.
I need to know if there is any method in the Date class that can take this number of days and returns the corressponding date?
I need this method because the calaulation to convert this number to milliseconds will not be very accurate!!!

Any comment will be very helpfull...
Thanx in advance.
samjav
0
Comment
Question by:samjav
[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
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 100 total points
ID: 6980227
try this:

java.util.GregorianCalendar cal = new java.util.GregorianCalendar(1900, 1, 1);
cal.add(cal.DATE, 28548);
System.out.println("" + cal.getTime().toString());

I am not sure that 27/2/1978 is 28548 after 1/1/1900!
0
 

Author Comment

by:samjav
ID: 6980419
Hi Venci75
i really didn't understand the code!!!
will u please explain it to me?
Thanx in advance
samjav
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6980444
First - you create an instance of GregodianCalendar. This is needed, because this object cares of the Gregorian date change. The Calendar objects contain the current date. So - you create your gregorian calendar with the 01/01/1900 as a current date. The calendar object has a method add() to which you must supply 2 parameters:
1. type of the field - years, days (date), months, weeks
2. the amount
This line:
cal.add(cal.DATE, 28548);
will change the current date by adding 28548 days to it.

cal.getTime() returns the calendar current date as a Date object. I used cal.getTime().toString() to display the result, but you can convert the date to String by using the SimpleDateFormat class (for example).
0
 

Author Comment

by:samjav
ID: 6980608
I tried the code & had the following date:
Sat Apr 01 00:00:00 GMT+03:00 1978
but my date 27/2/1978!!
Do you think the number of days that was added is wrong??

Thanx Venci75
samjav
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

696 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