How to initialize a variable of date type in java?

前端 未结 5 1692
借酒劲吻你
借酒劲吻你 2021-02-20 02:57
import java.util.Date;
Date firstDate; 

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

String line1=\"Fir         


        
5条回答
  •  不思量自难忘°
    2021-02-20 03:27

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

提交回复
热议问题