プロが教えるわが家の防犯対策術!

以下のプログラムをコンパイルしようとすると
java.lang.Error: Unresolved compilation problem:
The method setTime(java.util.Date) in the type Calendar is not applicable for the arguments (Date)

at Date.main(Date.java:23)
というエラーが帰ってきてしまいます。
どこがおかしいのか教えてください。
http://java.sun.com/j2se/1.4/ja/docs/ja/api/
には
public final void setTime(Date date)
というのが載ってるんですがねぇ。

------------------------------------------------
import java.util.*;

public class Date {

public static void main(String[] args) {
// create a GregorianCalendar with the Japan time zone
// and the current date and time
Calendar calendar = new GregorianCalendar(Locale.JAPAN);
Date trialTime = new Date();
calendar.setTime(trialTime);

// print out a bunch of interesting things
System.out.print(calendar.get(Calendar.YEAR) + "年");
System.out.print((1 + calendar.get(Calendar.MONTH)) + "月");
System.out.println(calendar.get(Calendar.DATE) + "日");
System.out.println("曜日: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
}
}

A 回答 (3件)

> public class Date {


こんな紛らわしい名前を付けるのがよくないんですね。
> Date trialTime = new Date();
とした時に、java.util.Date のつもりで書いていても、
コンパイラは上記の自前のDateだと思ってしまいます。
java.util.Date trialTime = new java.util.Date();
と書く手もあるにはありますが。
    • good
    • 0
この回答へのお礼

それです!まさにそれでした。
名前を変えたら一発でした。
ありがとうございました。

お礼日時:2004/07/14 16:55

っていうか、なにをしたいプログラムか意図がわからないので、あれなんですが、↓じゃだめなんですか?



import java.util.*;

public class Date {

public static void main(String[] args) {

Calendar calendar = Calendar.getInstance();

System.out.print(calendar.get(Calendar.YEAR) + "年");
System.out.print((1 + calendar.get(Calendar.MONTH)) + "月");
System.out.println(calendar.get(Calendar.DATE) + "日");
System.out.println("曜日: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.print(calendar.get(Calendar.HOUR) + ":");
System.out.print(calendar.get(Calendar.MINUTE) + ":");
System.out.println(calendar.get(Calendar.SECOND));
}
}

これをコンパイルして実行しておもったような結果が出ればこれを使ってください。Calendarクラスは一般的にこんな使い方しかしないような気がするんですが。。
    • good
    • 0
この回答へのお礼

おっしゃる通り、意図が分かってらっしゃらないですようですね。
分からないときは無理にお答えいただかなくて結構ですよ。

お礼日時:2004/07/14 17:04

プログラムを斜め読みしただけですので


外しているかもしれませんが・・・

public class Date {
 public static void main(String[] args) {

 }
}

で Date クラスを新たに定義してません?

> The method setTime(java.util.Date)
> in the type Calendar is not applicable for the arguments (Date)

これって、「あなたの作ったオリジナルの Date クラスを引数に持つような setTime 関数は存在しません」ってことで、「setTime は java.util.Date クラスを
引数にとります」ってエラーが出ているのではないでしょうか?

外していたらすみません
    • good
    • 0
この回答へのお礼

#1の方とともに大正解です。
ありがとうございました。

お礼日時:2004/07/14 16:58

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!