日期构造函数java [英] Date constructor java

查看:153
本文介绍了日期构造函数java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图在我创建的类中获取java的当前日期,但一切都失败了。我在许多网站上看到了
,例如 http://www.mkyong.com/java/java-date-和-calendar-examples /
日期构造函数没有参数
例如日期日期=新日期();

Hello I am trying to get the current date at java at a Class I created but everything fails. I've seen in many sites e.g. http://www.mkyong.com/java/java-date-and-calendar-examples/ that the date constructor has no arguments e.g. Date date = new Date();

现在在我的项目中我试着像这样使用它我得到错误

Now in my project I try to use it like this and I get the error


构造函数Date()未定义

that The constructor Date() is undefined

这怎么可能?到目前为止,我给你完整的代码

How is this possible? I give you the full code so far

import java.sql.Date;
import java.text.SimpleDateFormat;


public class Utility {


        String title;
        int ID;
        Date date;

        Utility(String t,int ID){
            this.ID=ID+1;
            title=t;
            SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
            Date a=new Date();// I get the error here
            String date = sdf.format(a);
            System.out.print(date);


        }
}

我在Eclipse IDE。你能帮助我吗?

I work at Eclipse IDE. Can you help me?

推荐答案

你找到的例子是 java.util.Date 当你使用< a href =http://docs.oracle.com/javase/7/docs/api/java/sql/Date.html =noreferrer> java.sql.Date

The examples you found are for java.util.Date while you are using java.sql.Date


  • java.sql.Date

有两个构造函数


  • 日期(长) date):使用给定的毫秒时间值构造Date对象。

  • Date(int year,int month,int day):已弃用

  • Date(long date): Constructs a Date object using the given milliseconds time value.
  • Date(int year, int month, int day): which is deprecated

且无默认日期()构造函数。

java.util.Date

等等都有一个没有参数的默认构造函数

among others has a default constructor without arguments


  • Date():分配一个Date对象并对其进行初始化,使其代表i的时间t被分配,测量到最接近的毫秒。

  • Date(): Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

导入类时,Eclipse将帮助你罚款可能的候选人,但总是检查第一个建议是否真的是你想要的。

When importing classes, Eclipse will help you fining possible candidates but always check if the first suggestion is really what you want.

这篇关于日期构造函数java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆