Date-problem
Hej! Jag har problem med datumhantering i en klass. I klassen hämtar jag dagens datum och dagens tid. Tiden räknar jag upp i en for-loop med en minut per varv. Koden ser ut som följer:
String dateOut;
DateFormat formaterare=DateFormat.getDateInstance(DateFormat.SHORT); //denna rad hör till den övre
Date idag=new Date();
dateOut=formaterare.format(idag);
String[]datum=new String[50];
for(int i=0;i<datum.length;i++)
{
datum[i]=dateOut;
}
int timma=idag.getHours();i
nt minuter=idag.getMinutes();
String [] tid=new String[50];
for(int i=0;i<tid.length;i++)
{
minuter=minuter + 1;
if(minuter>59)
{
timma=timma+1;
minuter=0;
}
tid[i]=timma+"."+minuter;
}
Problemet är att jag i samma klass har kontakt med en databas. Jag har därför importerat java.sql och java.util.
När jag så kompilerar klagar kompilatorn på att:"reference to Date is ambigious, both class java.sql.Date in java.sql and class java.util.Date in java.util match Date idag = new Date;".
Kan någon förklara vad jag skall göra åt detta?
Amatören
Svara
Sv: Date-problem
Hello.
Jag antar att du borde kunna göra så här:
String dateOut;
DateFormat formaterare=DateFormat.getDateInstance(DateFormat.SHORT); //denna rad hör till den övre
java.util.Date idag=new java.util.Date();
dateOut=formaterare.format(idag);
String[]datum=new String[50];
for(int i=0;i<datum.length;i++)
{
datum[i]=dateOut;
}
int timma=idag.getHours();i
nt minuter=idag.getMinutes();
String [] tid=new String[50];
for(int i=0;i<tid.length;i++)
{
minuter=minuter + 1;
if(minuter>59)
{
timma=timma+1;
minuter=0;
}
tid[i]=timma+"."+minuter;
}
Alltså importera java.util.Date när du skapar Date objektet. istället för att importera Date-klassen i början av filen.
Jag vill minnas att classen List har samma dilemma... men jag är inte säker.
Vet inte om det finns en bättre/smartare lösning.
Men det bör funka :-Þ
Peter.
Svara
Sv: Date-problem
Har sagt det förr och säger det igen - använd inte klassen Date!
Använd klassen:
com.ms.wfc.app.Time
Den har funktioner för att lägga till och dra ifrån olika tidsenheter, förutom
det så kan man använda alla metoderna. Klassen Date verkar vara på
väg ut i periferin för där är över hälften av metoderna ej rekommenderade
att användas. (se nedan:)
/Emma
MSDN om com.ms.wfc.app.Time
Time Methods
Constructors Description
Time Creates a Time object.
Methods Description
add Creates a new Time object that represents the number of time units (100 nanoseconds) after the time that this Time object represents.
addDays Creates a new Time object that represents the number of days after the time that this Time object represents.
addHours Creates a new Time object that represents the number of hours after the time that this Time object represents.
addMillis Creates a new Time object that represents the number of milliseconds after the time that this Time object represents.
addMinutes Creates a new Time object that represents the number of minutes after the time that this Time object represents.
addMonths Creates a new Time object that represents the number of months after the time that this Time object represents.
addSeconds Creates a new Time object that represents the number of seconds after the time that this Time object represents.
addYears Creates a new Time object that represents the number of years after the time that this Time object represents.
compare Compares two Time objects.
compareTo Compares this Time object to another Time object.
equals Tests for equality.
formatLongDate Retrieves the Time object as a String using the long date format.
formatLongTime Retrieves the Time object as a String using the long time format.
formatShortDate Retrieves the Time object as a String using the short date format.
formatShortTime Retrieves the Time object as a String using the short time format.
fromFileTime Creates a Time object based on the specified value.
getDate Creates a Time object that represents the same date as this Time object but without a time of day specified.
getDay Retrieves the day of the month that this Time object represents.
getDayOfWeek Retrieves the day of the week that this Time object represents.
getDayOfYear Retrieves the day of the year that this Time object represents.
getHour Retrieves the hour of the day that this Time object represents.
getMillis Retrieves the milliseconds of the second that this Time object represents.
getMinute Retrieves the minutes of the hour that this Time object represents.
getMonth Retrieves the month of the year that this Time object represents.
getSecond Retrieves the seconds of the minute that this Time object represents.
getTimeOfDay Creates a Time object that represents the same time of day as this Time object, but without a date specified.
getYear Retrieves the year that this Time object represents.
save Stores the Time object in persisted storage.
setDate Creates a new Time object that contains the date from another Time object and the time of day from this Time object.
setTimeOfDay Creates a new Time object that contains the date from this Time object and the time of day from another Time object.
toDouble Retrieves the Time object as an OLE-formatted double value.
toFileTime Retrieves the Time object as a Win32 FILETIME value.
toLocalTime Creates a Time object that represents this Time object in the user's local time zone.
toLong Retrieves the number of time units (100 nanoseconds) units that are stored in this Time object.
toString Retrieves this Time object as a String object.
toSystemTime Retrieves this Time object as a Win32 SYSTEMTIME structure.
toUniversalTime Creates a new Time object that represents this Time object in the coordinated universal time (UTC) time zone.
toVariant Retrieves this Time object as a com.ms.com.Variant object.
Svara