Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a variable of date type in Java?

import java.util.Date;
Date firstDate; 

I don't know how to initialize 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?

like image 949
owlprogrammer Avatar asked Dec 06 '15 15:12

owlprogrammer


1 Answers

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 documentation 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

like image 136
Scirocco Avatar answered Sep 23 '22 00:09

Scirocco