Solved

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 102
ejb stateless example 2 44
Html Table Looping (part 2) 5 35
batch vs regular insert in spring DAO 2 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

730 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