Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

22 Experts available now in Live!

Get 1:1 Help Now