Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 114
ejb example issues 3 19
What's wrong with this code? 4 26
web project error add remove 1 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 video teaches viewers about errors in exception handling.

790 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