How to initialize a variable of date type in java?

前端 未结 5 2150
萌比男神i
萌比男神i 2021-02-20 03:04
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:29

    java.util.Date constructor with parameters like new Date(int year, int month, int date, int hrs, int min). is deprecated and preferably do not use it any more. Oracle docs prefers the way over java.util.Calendar. So you can set any date and instantiate Date object through the getTime() method.

    Calendar calendar = Calendar.getInstance();
    calendar.set(2018, 11, 31, 59, 59, 59);
    Date happyNewYearDate = calendar.getTime();
    

    Notice that month number starts from 0

提交回复
热议问题