如何在Java中初始化日期类型的变量? [英] How to initialize a variable of date type in java?

查看:690
本文介绍了如何在Java中初始化日期类型的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Date;
Date firstDate; 

我不知道如何初始化firstDate,例如说String

I don't know how to intialize the firstDate for example for String you say

String line1="First line" 

但是日期的格式是什么?您能举个例子吗?

but what is the format for date can you give me an example?

推荐答案

以下是Oracle网站中日期类的Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

如果向下滚动到构造函数摘要,则会看到有关如何实例化Date对象的不同选项。像Java中的所有对象一样,您可以使用以下内容创建一个新对象:

Here's the Javadoc in Oracle's website for the Date class: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
If you scroll down to "Constructor Summary," you'll see the different options for how a Date object can be instantiated. Like all objects in Java, you create a new one with the following:

Date firstDate = new Date(ConstructorArgsHere);

现在您可以选择。如果您不传递任何参数,只需执行此操作,

Now you have a bit of a choice. If you don't pass in any arguments, and just do this,

Date firstDate = new Date();

它将代表您调用它的确切日期和时间。以下是一些您可能要使用的其他构造函数:

it will represent the exact date and time at which you called it. Here are some other constructors you may want to make use of:

Date firstDate1 = new Date(int year, int month, int date);
Date firstDate2 = new Date(int year, int month, int date, int hrs, int min);
Date firstDate3 = new Date(int year, int month, int date, int hrs, int min, int sec);

这篇关于如何在Java中初始化日期类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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