Solved

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

Posted on 2002-04-30
4
191 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now