注册 登录
编程论坛 JAVA论坛

照书上打的,不能运行

ehszt 发布于 2018-02-18 10:45, 3630 次点击
import java.util.Date;
public class hello

{
      
       public static void main(String[] args)

       {
          Date date=new Date();
          String hour=String.format("%tH",date);
          String minute=String.format("%tM", date);
          String second=String.format("%tS",date);
          System.out.println("现在是:"+hour+"时"+minute+"分"+second+"秒");

       }

}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
    The method format(String, Object[]) in the type String is not applicable for the arguments (String, Date)
    The method format(String, Object[]) in the type String is not applicable for the arguments (String, Date)
    The method format(String, Object[]) in the type String is not applicable for the arguments (String, Date)

    at hello.main(hello.java:10)
13 回复
#2
疯狂的小a2018-02-18 10:59
package com.xiaoa.demo;
import java.util.Date;

/**
 * @author niyite
 *
 */
public class Hello {
    public static void main(String[] args)

    {
        Date date = new Date();
        String hour = String.format("%tH", date);
        String minute = String.format("%tM", date);
        String second = String.format("%tS", date);
        System.out.println("现在是:" + hour + "时" + minute + "分" + second + "秒");

    }
}
现在是:10时58分12秒
#3
疯狂的小a2018-02-18 11:00
类名首字母要大写
#4
疯狂的小a2018-02-18 11:04
给你一个简单的方法
package com.xiaoa.demo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author niyite
 *
 */
public class Hello2 {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("现在是:hh时mm分ss秒");
        String str_date = sdf.format(date);
        System.out.println(str_date);
    }
}
#5
ehszt2018-02-18 15:34
以下是引用疯狂的小a在2018-2-18 11:00:41的发言:

类名首字母要大写

好像不是这个问题。
#6
疯狂的小a2018-02-18 17:21
回复 5楼 ehszt
那为什么我运行出来了
#7
疯狂的小a2018-02-18 17:30
回复 5楼 ehszt
如果确实运行不出来,就把data包装成数组,错误的意思是说方法的参数类型错误,需要(String,Object[]),你给的是(String,Date)
#8
bfmss2018-02-18 19:58
复制你的代码,我运行出来了,应该没有什么问题。我是新手,能想出的可能就是:是不是文件的名字和类名是不是没搞成一样的?
#9
疯狂的小a2018-02-18 20:33
回复 8楼 bfmss
对对对,文件名必须和类名一致,驼峰式命名
#10
ehszt2018-02-18 21:02
import java.util.Date;
这一句有个叉叉。
#11
疯狂的小a2018-02-18 22:17
回复 10楼 ehszt
依赖jdk的环境,没有环境,就引不进来包
#12
ehszt2018-02-20 18:42
以下是引用疯狂的小a在2018-2-18 22:17:24的发言:

依赖jdk的环境,没有环境,就引不进来包

在cmd下输入javac和java是有输出的,表明有jdk环境。
#13
静水且流深2018-02-20 22:24
语法问题吧,常见的是这么写的。
程序代码:


 Date date=new Date();

 String[] timeStr = new SimpleDateFormat("HH:mm:ss").format(date).split(":");

 String hour = timeStr[0];

 String minute = timeStr[1];

 String second = timeStr[2];

 System.out.println("现在是:"+hour+"时"+minute+"分"+second+"秒");
#14
ehszt2018-02-21 20:53
已解决就是那个叉叉的问题,点下那个叉叉选第二项就好了。
谢谢楼上各位。
1