java 构造日期,日期构造函数java

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

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);

}

}

I work at Eclipse IDE. Can you help me?

解决方案

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

java.sql.Date

has two constructors

Date(long date): Constructs a Date object using the given milliseconds time value.

Date(int year, int month, int day): which is deprecated

and no default Date() constructor.

java.util.Date

among others has a default constructor without arguments

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值